User Tools

Site Tools


learning_the_language_of_unity_-_c-sharp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
learning_the_language_of_unity_-_c-sharp [2023/02/07 10:27] nkassailearning_the_language_of_unity_-_c-sharp [2023/02/21 09:13] (current) nkassai
Line 200: Line 200:
 There are other types of loops that you might encounter such as **foreach and do-while loops**, however, you will not encounter them as much as for and while. Hence, I will not be covering them in this tutorial. There are other types of loops that you might encounter such as **foreach and do-while loops**, however, you will not encounter them as much as for and while. Hence, I will not be covering them in this tutorial.
  
-===== Functions ===== 
  
-Throughout this tutorial, we have utilized **Debug.Log()** to print out information to the terminal. Log() is known as a **function**, which is pre-defined code designed to be reused and sometimes **return** a value. Additionally, **Start()** and **Update()** are also functions which we have been using throughout this tutorial. However, Start and Update do not have any definitions, rather, they are called by Unity at specific intervals (Start is called once the scene starts, and Update is called every frame). We get to define them with our own code!  
- 
-Let's say we wanted to write a script that would print out the value of two integers, and then their sum. Let's create a new script called functions and write this code in Start.  
- 
-{{ :function_intro.png?910 }} 
- 
-Now, let's save this script, remove the Loops component from the Cube, and add our Functions script to it. Then, play the Unity scene! 
- 
-{{ :functions_active_1.png?910 }} 
- 
-Now, this script works out no problem, but what if we wanted to repeat this process again in Start? Well, we would have to basically copy and paste the code again to get the following result: 
- 
-{{ :no_function_problem.png?910 }} 
- 
-While this will still work, the code is starting to look a little messy. What if we wanted to repeat this process 10 times? 20? 40? Well, our code would start becoming hundreds of lines long, even though the same 10 lines are repeating over and over again! Let's place the repeated block of code in a function. As I've mentioned previously, functions are used to help reduce code redundancy, but they also have the additional ability to **return** a value.  Whenever you declare a function, you also have to declare it's return type: 
- 
-**dataType FunctionName(parameters){** 
- 
-reusable code 
- 
-**}** 
- 
-For this purpose, we will use the return type of **void** which just means that we do not want this function to return a value. I'll explain this more as we write more functions, but for now, write the following block of code above start: 
- 
-{{ :functions_active_2.png?910 }} 
- 
-In our function, we have defined two parameters, two integers, a and b. We then have the same block of code as before. Now, if we want to be able to use this function in our code, write the name of the function in Start, pass in the **correct** parameters and save. **Note:** When I say correct parameters, that means the number of parameters and datatypes must match the function! If you pass in three integers, or an integer and a string, our script won't be able to find what function we are talking about. 
- 
-Now, go ahead and run our Unity scene to see that we get the same output as before. The difference now, is that if you wanted to run that block of code again, all you have to do is write one line instead of the whole block!  
- 
-Let's change the return type of the function to be an int instead of a void. Once we do so, we get an error saying that this function needs to return something! Instead of printing out the value to the console, let's just return the value of sum. 
- 
-{{ :functions_active_3.png?910 }} 
- 
-If we were to run this code now, we wouldn't see what sum equals to! However, since this function returns a value of int, we can actually assign the function to an integer variable! 
  
 {{ :function_active_4.png?910 }} {{ :function_active_4.png?910 }}
learning_the_language_of_unity_-_c-sharp.txt · Last modified: 2023/02/21 09:13 by nkassai