WEBVTT 1 00:00:06.448 --> 00:00:09.923 Game Basics Functions 2 00:00:27.658 --> 00:00:29.509 Hello, everyone 3 00:00:29.509 --> 00:00:33.599 This is Lee Yeong-hoon, in charge of C# object-oriented programming lecture 4 00:00:33.599 --> 00:00:36.507 Today, we'll talk about 5 00:00:36.507 --> 00:00:39.000 concept and coding of functions 6 00:00:39.000 --> 00:00:41.900 We’ll explore the concept of functions and coding methods 7 00:00:41.900 --> 00:00:44.000 We’ll learn how they are processed 8 00:00:44.000 --> 00:00:47.850 as Call by Value and Call by Reference 9 00:00:47.850 --> 00:00:49.878 depending on the parameter type 10 00:00:49.878 --> 00:00:53.331 We’ll also cover function overloading 11 00:00:53.331 --> 00:00:55.232 and how functions are called 12 00:00:56.119 --> 00:00:59.476 Function Concept, Syntax, Parameters, and Overloading 13 00:01:00.327 --> 00:01:03.399 If you look up function in a dictionary like Naver, 14 00:01:03.399 --> 00:01:04.800 you’ll find that it translates 15 00:01:04.800 --> 00:01:07.300 to Function in English 16 00:01:07.300 --> 00:01:10.199 Looking up Function again shows 17 00:01:10.199 --> 00:01:13.000 that it means feature or functionality 18 00:01:13.000 --> 00:01:14.745 When we learned about functions 19 00:01:14.745 --> 00:01:17.400 in math class as kids, 20 00:01:17.400 --> 00:01:20.709 we understood them as mathematical concepts 21 00:01:20.709 --> 00:01:24.095 But in reality, the word function also 22 00:01:24.095 --> 00:01:26.599 means feature or functionality 23 00:01:26.599 --> 00:01:29.252 So, in computer programming, 24 00:01:29.252 --> 00:01:30.500 a function 25 00:01:30.500 --> 00:01:34.199 refers to a specific functionality 26 00:01:34.199 --> 00:01:36.003 Now, let’s represent a function 27 00:01:36.003 --> 00:01:37.105 with a diagram 28 00:01:38.699 --> 00:01:40.460 A function looks like this 29 00:01:40.460 --> 00:01:42.718 This is how it looks like 30 00:01:47.599 --> 00:01:50.599 You’ve probably seen something like this before 31 00:01:50.599 --> 00:01:52.800 Does it remind you of a juicer? 32 00:01:52.800 --> 00:01:55.970 If you put a lemon in here, 33 00:01:55.970 --> 00:01:58.300 something happens inside, right? 34 00:01:58.300 --> 00:02:03.099 Then, juice comes out 35 00:02:03.099 --> 00:02:05.606 Similarly, a function 36 00:02:05.606 --> 00:02:07.065 has an input section, 37 00:02:09.600 --> 00:02:12.600 performs some process inside, 38 00:02:12.600 --> 00:02:15.018 and then outputs a result 39 00:02:17.800 --> 00:02:20.018 In C#, a function is expressed 40 00:02:20.018 --> 00:02:21.731 with a return type, 41 00:02:21.731 --> 00:02:22.978 followed by the function name, 42 00:02:26.899 --> 00:02:28.217 and parentheses 43 00:02:28.217 --> 00:02:29.100 Inside the parentheses, 44 00:02:29.100 --> 00:02:31.800 we can include some information, 45 00:02:31.800 --> 00:02:33.600 such as variables 46 00:02:35.600 --> 00:02:38.310 These are called parameters 47 00:02:40.250 --> 00:02:42.637 In English, they are called parameters 48 00:02:44.449 --> 00:02:46.899 A function may or may not have parameters 49 00:02:46.899 --> 00:02:49.600 It also has its own scope 50 00:02:49.600 --> 00:02:51.399 The fact that it has a return type 51 00:02:51.399 --> 00:02:53.199 means it returns a value 52 00:02:53.199 --> 00:02:54.330 In English, returning a value 53 00:02:54.330 --> 00:02:55.597 is called return 54 00:02:58.399 --> 00:03:00.325 So, we say return type 55 00:03:03.929 --> 00:03:05.637 Inside the function, we use the return keyword, 56 00:03:07.458 --> 00:03:11.050 followed by the return value 57 00:03:12.950 --> 00:03:15.199 This is the general structure 58 00:03:15.199 --> 00:03:17.199 of a function 59 00:03:17.199 --> 00:03:19.968 Now that we’ve talked 60 00:03:19.968 --> 00:03:21.837 about structure, 61 00:03:21.837 --> 00:03:24.199 let’s cover the concept 62 00:03:24.199 --> 00:03:25.857 A function 63 00:03:28.699 --> 00:03:30.849 has a defined space 64 00:03:32.929 --> 00:03:35.085 It may or may not return a value 65 00:03:39.699 --> 00:03:42.600 When a function starts, it may or may not receive input values 66 00:03:42.600 --> 00:03:45.818 These are the parameters we just discussed 67 00:03:45.818 --> 00:03:47.506 So this is the parameter 68 00:03:51.249 --> 00:03:53.679 Likewise, a function may have parameters 69 00:03:53.679 --> 00:03:54.930 or none at all 70 00:03:54.930 --> 00:03:56.409 It may have one only, 71 00:03:56.409 --> 00:03:59.249 or many at once 72 00:03:59.249 --> 00:04:03.214 And since we talked about variables, 73 00:04:03.214 --> 00:04:06.069 we can define variables 74 00:04:06.069 --> 00:04:08.436 inside the function as well 75 00:04:11.050 --> 00:04:14.031 A variable declared inside a function 76 00:04:14.031 --> 00:04:15.399 is called a local variable 77 00:04:17.399 --> 00:04:22.214 In C#, there are also variables outside the function 78 00:04:22.214 --> 00:04:26.100 We’ll cover this in the next lesson about classes, 79 00:04:26.100 --> 00:04:27.829 but functions can also have external variables, 80 00:04:27.829 --> 00:04:32.458 which are called fields 81 00:04:32.458 --> 00:04:34.386 Fields are sometimes called member variables 82 00:04:34.386 --> 00:04:35.899 or methods 83 00:04:35.899 --> 00:04:38.500 If a variable is outside a function but isn’t a field, 84 00:04:38.500 --> 00:04:40.067 it is considered a global variable 85 00:04:43.800 --> 00:04:46.100 This is how these concepts are categorized 86 00:04:46.100 --> 00:04:47.759 A function consists of internal 87 00:04:47.759 --> 00:04:51.170 and external components 88 00:04:51.170 --> 00:04:52.510 Now, let’s talk about their different types 89 00:04:52.510 --> 00:04:55.439 There are three types: fields, 90 00:04:55.439 --> 00:04:56.840 parameters, and local variables 91 00:04:56.840 --> 00:04:58.399 These are the three 92 00:04:58.399 --> 00:05:00.399 In fact, parameters 93 00:05:00.399 --> 00:05:04.000 behave similarly to local variables 94 00:05:04.000 --> 00:05:07.700 For example, say 95 00:05:07.700 --> 00:05:10.559 we declare an integer variable int, 96 00:05:10.559 --> 00:05:11.908 called A 97 00:05:13.849 --> 00:05:17.313 Then, inside a function, we can declare another variable A 98 00:05:20.669 --> 00:05:24.799 If we assign 99 00:05:24.799 --> 00:05:28.383 a value within that, 100 00:05:28.383 --> 00:05:31.540 such as A = 10 inside the function, 101 00:05:31.540 --> 00:05:34.600 which function will be affected? 102 00:05:34.600 --> 00:05:36.399 Which function is being referred to here? 103 00:05:36.399 --> 00:05:37.744 The variable A 104 00:05:39.100 --> 00:05:41.735 refers to the closest function 105 00:05:41.735 --> 00:05:43.399 to itself 106 00:05:43.399 --> 00:05:46.659 That’s why it is considered a local variable 107 00:05:46.659 --> 00:05:49.100 Functions have these characteristics like this 108 00:05:49.100 --> 00:05:51.755 We’ve discussed the concept 109 00:05:51.755 --> 00:05:53.500 and structure of functions 110 00:05:53.500 --> 00:05:56.273 Now, let’s write some code 111 00:05:56.273 --> 00:05:57.630 to implement it 112 00:05:59.630 --> 00:06:00.894 Let’s go here 113 00:06:00.894 --> 00:06:03.089 and create a new project 114 00:06:03.089 --> 00:06:07.209 Right-click on the solution, 115 00:06:07.209 --> 00:06:09.417 click Add, 116 00:06:09.417 --> 00:06:12.100 then select New Project 117 00:06:12.100 --> 00:06:13.818 Again, choose Console App 118 00:06:13.818 --> 00:06:17.258 or NET Framework, 119 00:06:17.258 --> 00:06:18.181 click Next, 120 00:06:18.181 --> 00:06:21.689 and name it Function 121 00:06:21.689 --> 00:06:24.041 since we are working with functions 122 00:06:36.070 --> 00:06:38.600 Now, let’s look at the Main function 123 00:06:38.600 --> 00:06:39.839 It's a function too 124 00:06:39.839 --> 00:06:42.700 Here, we have a return type, 125 00:06:42.700 --> 00:06:44.272 which we haven’t covered yet, 126 00:06:44.272 --> 00:06:46.200 but it’s called void 127 00:06:46.200 --> 00:06:49.600 This means nothing or empty 128 00:06:49.600 --> 00:06:51.400 Think of it as the void character in Chinese 129 00:06:51.400 --> 00:06:53.200 So, void means 130 00:06:53.200 --> 00:06:55.339 there is no return value 131 00:06:55.339 --> 00:06:57.700 So it does not return anything 132 00:06:57.700 --> 00:07:02.579 That’s why there is no explicit 133 00:07:02.579 --> 00:07:04.069 return statement here, 134 00:07:04.069 --> 00:07:05.689 as you see here 135 00:07:07.630 --> 00:07:12.500 The Main function also has parameters 136 00:07:12.500 --> 00:07:14.563 If you hover over them, 137 00:07:14.563 --> 00:07:16.230 you will see that they are labeled as parameters 138 00:07:20.309 --> 00:07:24.007 Before implementing a function, 139 00:07:24.007 --> 00:07:28.650 let’s create a simple calculation 140 00:07:28.650 --> 00:07:30.799 using two numbers 141 00:07:30.799 --> 00:07:32.899 Let's begin 142 00:07:32.899 --> 00:07:39.500 First, let’s declare an int num1 variable 143 00:07:39.500 --> 00:07:43.399 We’ll set its value to 10 144 00:07:43.399 --> 00:07:45.470 Next, let’s declare int num2 145 00:07:45.470 --> 00:07:47.377 We’ll set its value to 20 146 00:07:53.140 --> 00:07:55.540 Now, I want to add these two numbers 147 00:07:55.540 --> 00:08:01.530 Let’s perform the four basic 148 00:08:01.530 --> 00:08:03.324 arithmetic operations 149 00:08:11.809 --> 00:08:14.339 We’ll start with addition 150 00:08:18.537 --> 00:08:22.571 We’ll declare a result variable of type int 151 00:08:22.571 --> 00:08:28.500 Then, we’ll assign it the sum of num1 and num2 152 00:08:28.500 --> 00:08:29.779 Shall we print this out? 153 00:08:30.868 --> 00:08:33.900 Console WriteLine(result); 154 00:08:33.900 --> 00:08:35.529 If we run this, 155 00:08:38.360 --> 00:08:39.599 let's do this too 156 00:08:39.599 --> 00:08:42.000 We should set Function 157 00:08:42.000 --> 00:08:46.099 as the startup project first 158 00:08:46.099 --> 00:08:48.134 Then, let’s run it 159 00:08:50.599 --> 00:08:52.461 Now, we can see 30 160 00:08:52.461 --> 00:08:54.987 as the output 161 00:08:54.987 --> 00:08:56.327 Adding these two numbers gives 30 162 00:09:01.000 --> 00:09:02.725 Now, let’s try subtraction 163 00:09:06.566 --> 00:09:08.799 We cannot declare int result again, 164 00:09:08.799 --> 00:09:13.200 because variables must have unique names 165 00:09:13.200 --> 00:09:17.400 within the same scope 166 00:09:17.400 --> 00:09:19.628 You cannot declare the same variable name 167 00:09:19.628 --> 00:09:21.469 twice within the same space 168 00:09:21.469 --> 00:09:24.370 If you want a new variable, 169 00:09:24.370 --> 00:09:26.208 you can add a number 170 00:09:26.208 --> 00:09:28.599 or use a different name 171 00:09:28.599 --> 00:09:30.930 But I will just reuse it 172 00:09:30.930 --> 00:09:33.930 I can reuse this, 173 00:09:33.930 --> 00:09:37.210 the existing result variable 174 00:09:37.210 --> 00:09:38.059 Since this is subtraction, 175 00:09:38.059 --> 00:09:41.113 I’ll assign num2 - num1 to result 176 00:09:44.400 --> 00:09:46.400 Let’s print the result 177 00:09:46.400 --> 00:09:48.940 like so 178 00:09:48.940 --> 00:09:52.289 I’ll do the four basic arithmetic operations in the same way 179 00:09:54.200 --> 00:09:57.824 I’ll quickly copy and modify it 180 00:09:59.220 --> 00:10:01.515 For multiplication, 181 00:10:01.515 --> 00:10:02.979 we multiply the two numbers 182 00:10:04.910 --> 00:10:07.969 I changed it from num2 to num1 183 00:10:07.969 --> 00:10:09.750 For subtraction, I adjusted the order 184 00:10:09.750 --> 00:10:13.410 so that the larger number is first, 185 00:10:13.410 --> 00:10:14.529 like this 186 00:10:14.529 --> 00:10:17.920 Now I’ll also perform division 187 00:10:17.920 --> 00:10:18.816 For division, 188 00:10:25.281 --> 00:10:27.350 I’ll divide the larger number by the smaller number, 189 00:10:27.350 --> 00:10:29.598 like 20 divided by 10 190 00:10:29.598 --> 00:10:31.113 Let's run it 191 00:10:34.172 --> 00:10:37.158 Now, we can see addition, subtraction, multiplication, 192 00:10:37.158 --> 00:10:39.251 and division printed out 193 00:10:46.024 --> 00:10:49.120 Now, imagine 194 00:10:49.120 --> 00:10:51.707 that we repeat this process multiple times 195 00:10:51.707 --> 00:10:55.331 We wrote quite a bit of code just now 196 00:10:55.331 --> 00:10:58.509 If I want to do this twice, 197 00:10:58.509 --> 00:11:02.321 I would need to copy and paste everything again, 198 00:11:02.321 --> 00:11:03.459 something like this 199 00:11:03.459 --> 00:11:06.469 This approach would 200 00:11:06.469 --> 00:11:08.984 make the code repetitive, 201 00:11:08.984 --> 00:11:10.598 executing the same operation twice 202 00:11:10.598 --> 00:11:12.677 Like this 203 00:11:12.677 --> 00:11:15.400 We use a function 204 00:11:15.400 --> 00:11:16.400 for various reasons, 205 00:11:16.400 --> 00:11:18.118 one of which is this 206 00:11:18.118 --> 00:11:21.113 It's to group multiple operations 207 00:11:21.113 --> 00:11:23.855 into one reusable unit, 208 00:11:23.855 --> 00:11:25.212 making it easier to manage 209 00:11:25.212 --> 00:11:27.152 That's one of the reasons 210 00:11:27.152 --> 00:11:30.925 So, I’ll delete the duplicate code 211 00:11:33.271 --> 00:11:37.063 and instead, I’ll create a function 212 00:11:37.063 --> 00:11:39.578 to handle all four arithmetic operations 213 00:11:43.301 --> 00:11:47.296 A function cannot be declared 214 00:11:47.296 --> 00:11:48.796 inside another function 215 00:11:48.796 --> 00:11:52.628 It must be outside 216 00:11:52.628 --> 00:11:54.430 Since this function performs calculations, 217 00:11:54.430 --> 00:11:56.687 I’ll name it Calculate 218 00:11:56.687 --> 00:11:58.311 I’ll write void Calc() to define it 219 00:11:58.311 --> 00:12:01.350 Let’s write void Calc() 220 00:12:06.311 --> 00:12:09.826 Now, select all the code 221 00:12:09.826 --> 00:12:12.400 we’ve written so far 222 00:12:12.400 --> 00:12:15.588 Click the top line, hold Shift, 223 00:12:15.588 --> 00:12:18.731 and click the last line to select everything 224 00:12:18.731 --> 00:12:20.558 Now we have everything 225 00:12:20.558 --> 00:12:22.914 Press Ctrl+X 226 00:12:22.914 --> 00:12:24.733 or right-click 227 00:12:24.733 --> 00:12:26.143 and choose Cut 228 00:12:26.143 --> 00:12:29.692 Ctrl+X is the hotkey for this 229 00:12:29.692 --> 00:12:31.568 Then, paste it inside the function 230 00:12:37.103 --> 00:12:40.123 Now, we have successfully moved the code 231 00:12:40.123 --> 00:12:42.060 As I explained earlier, 232 00:12:42.060 --> 00:12:45.341 a function has a return type, 233 00:12:45.341 --> 00:12:48.400 a name, and parameter 234 00:12:48.400 --> 00:12:51.544 Right now, I created 235 00:12:51.544 --> 00:12:53.865 a function without parameters 236 00:12:53.865 --> 00:12:54.806 To summarize, 237 00:12:58.291 --> 00:13:00.756 a function’s structure consists of: 238 00:13:05.410 --> 00:13:09.717 Return type → Name → 239 00:13:11.638 --> 00:13:15.044 Parentheses, 240 00:13:15.044 --> 00:13:18.133 with or without parameters 241 00:13:21.083 --> 00:13:23.049 There could be many parameters 242 00:13:23.049 --> 00:13:25.925 or none at all 243 00:13:25.925 --> 00:13:29.786 For example, this is no parameters 244 00:13:29.786 --> 00:13:33.756 Then, we use curly braces 245 00:13:38.400 --> 00:13:40.707 Inside, we can include return, 246 00:13:41.994 --> 00:13:45.516 but if the return type is void, 247 00:13:45.516 --> 00:13:47.747 we can omit return 248 00:13:47.747 --> 00:13:50.582 In our Calc() function, 249 00:13:50.582 --> 00:13:52.697 there’s no return 250 00:13:52.697 --> 00:13:54.707 This is a valid way to write it 251 00:13:57.034 --> 00:14:01.133 Now, let’s try running the function 252 00:14:01.133 --> 00:14:03.342 So we write it 253 00:14:03.342 --> 00:14:04.786 like this here 254 00:14:06.291 --> 00:14:08.648 However, this will cause an error 255 00:14:08.648 --> 00:14:10.717 because Main() is static, 256 00:14:10.717 --> 00:14:14.331 which means it exists from the beginning, 257 00:14:14.331 --> 00:14:16.663 but Calc() 258 00:14:16.663 --> 00:14:17.935 does not have static 259 00:14:17.935 --> 00:14:20.185 This relates 260 00:14:20.185 --> 00:14:23.261 to classes, which we’ll cover next time 261 00:14:23.261 --> 00:14:25.816 It's because it belongs to a class 262 00:14:25.816 --> 00:14:28.616 If you hover over Calc(), 263 00:14:28.616 --> 00:14:32.113 you’ll see it belongs to Program Calc 264 00:14:32.113 --> 00:14:34.558 This means it is part of the Program class, 265 00:14:34.558 --> 00:14:37.657 so it's included in this class 266 00:14:39.311 --> 00:14:42.455 This may look like 267 00:14:42.455 --> 00:14:45.133 it's inside the same thing 268 00:14:45.133 --> 00:14:46.697 This Main 269 00:14:46.697 --> 00:14:49.014 may look like it belongs to Program 270 00:14:49.014 --> 00:14:50.758 But because it’s static, 271 00:14:50.758 --> 00:14:52.608 it cannot access Calc() directly 272 00:14:53.806 --> 00:14:56.383 To fix this, 273 00:14:56.383 --> 00:14:58.400 we have two solutions 274 00:14:58.400 --> 00:15:01.291 One is to create an instance 275 00:15:01.291 --> 00:15:05.069 of Program 276 00:15:05.069 --> 00:15:06.974 and call Calc() 277 00:15:06.974 --> 00:15:08.377 Two is to make Calc() static, 278 00:15:08.377 --> 00:15:09.885 so it belongs to the class itself 279 00:15:14.103 --> 00:15:15.450 See this? 280 00:15:17.242 --> 00:15:19.073 That's how you can do it 281 00:15:19.073 --> 00:15:23.093 Now, you can see that the error disappears 282 00:15:23.093 --> 00:15:24.798 Since both functions are static, 283 00:15:24.798 --> 00:15:27.737 they exist from the beginning, 284 00:15:27.737 --> 00:15:29.400 allowing them to be called directly 285 00:15:29.400 --> 00:15:31.697 Let's run thi 286 00:15:33.321 --> 00:15:37.469 As expected, it prints the results correctly 287 00:15:37.469 --> 00:15:40.400 Now, let’s try something different 288 00:15:40.400 --> 00:15:43.950 The Calc() function we created earlier 289 00:15:43.950 --> 00:15:47.370 combines multiple operations, 290 00:15:47.370 --> 00:15:50.098 but this time, I’ll create a function dedicated 291 00:15:50.098 --> 00:15:51.281 solely to addition 292 00:15:53.469 --> 00:15:57.162 I’ll write this function below 293 00:15:57.162 --> 00:15:58.748 So this is addition 294 00:15:58.748 --> 00:16:02.044 Since Calc() 295 00:16:02.044 --> 00:16:03.625 didn’t need to return anything, 296 00:16:03.625 --> 00:16:05.143 I didn’t specify a return type 297 00:16:05.143 --> 00:16:08.010 But now, because this is an addition function, 298 00:16:08.010 --> 00:16:10.796 it needs to combine these two 299 00:16:10.796 --> 00:16:13.750 and it should return a value, 300 00:16:13.750 --> 00:16:15.966 process it, 301 00:16:15.966 --> 00:16:17.935 and do things like that 302 00:16:17.935 --> 00:16:21.954 Let's create this part 303 00:16:21.954 --> 00:16:25.093 I’ll set the return type to int 304 00:16:25.093 --> 00:16:28.278 This function will take two integers, 305 00:16:28.278 --> 00:16:30.737 add them, 306 00:16:30.737 --> 00:16:33.691 and return the result 307 00:16:33.691 --> 00:16:36.311 That's the gist of this function 308 00:16:36.311 --> 00:16:38.821 I’ll name the function 309 00:16:38.821 --> 00:16:42.420 Add(), a common convention for addition functions 310 00:16:42.420 --> 00:16:46.445 I’ll define it as int Add 311 00:16:46.445 --> 00:16:48.024 and int A, int B 312 00:16:48.024 --> 00:16:50.328 For one parameter, 313 00:16:50.328 --> 00:16:52.192 this is how I write it 314 00:16:52.192 --> 00:16:54.292 If I needed to pass more numbers, 315 00:16:54.292 --> 00:16:56.093 I could separate them with commas 316 00:16:57.390 --> 00:16:58.766 Same with three numbers 317 00:16:58.766 --> 00:17:01.806 Use comma like int A, int B, int C 318 00:17:01.806 --> 00:17:05.192 For now, I’ll stick to two numbers 319 00:17:05.192 --> 00:17:07.390 Now, I need to define the function’s scope 320 00:17:07.390 --> 00:17:08.826 by adding this 321 00:17:08.826 --> 00:17:12.004 Like so 322 00:17:12.004 --> 00:17:13.798 But you’ll notice an error 323 00:17:13.798 --> 00:17:14.707 We see the red line 324 00:17:14.707 --> 00:17:19.143 The issue is that I defined a return type 325 00:17:19.143 --> 00:17:21.585 but didn’t specify what to return 326 00:17:21.585 --> 00:17:23.222 That's why we're seeing an error 327 00:17:23.222 --> 00:17:27.232 So, I’ll write return here 328 00:17:29.053 --> 00:17:31.080 Then, I’ll return the result of A and B 329 00:17:31.080 --> 00:17:32.974 Let's return it, but instead of 330 00:17:32.974 --> 00:17:34.070 just returning it directly, 331 00:17:34.070 --> 00:17:36.816 I’ll store it in a variable first 332 00:17:36.816 --> 00:17:40.707 I’ll create int result = A + B 333 00:17:40.707 --> 00:17:44.242 and then return result 334 00:17:47.123 --> 00:17:48.281 Now, the error disappears 335 00:17:58.885 --> 00:18:00.994 So that's how it looks like 336 00:18:04.727 --> 00:18:07.539 I will not make this into a comment 337 00:18:07.539 --> 00:18:11.420 Next, I’ll call the Add function 338 00:18:11.420 --> 00:18:12.860 I just did it the last time, 339 00:18:12.860 --> 00:18:14.796 but let's now really talk about this 340 00:18:14.796 --> 00:18:16.863 When you create a function like this, 341 00:18:16.863 --> 00:18:18.766 it’s called 342 00:18:20.400 --> 00:18:26.756 defining or implementing a function 343 00:18:28.271 --> 00:18:29.846 Defining 344 00:18:29.846 --> 00:18:34.717 or implementing a function 345 00:18:34.717 --> 00:18:36.547 But just defining a function 346 00:18:36.547 --> 00:18:38.319 doesn’t mean 347 00:18:38.319 --> 00:18:40.747 that it will run automatically 348 00:18:40.747 --> 00:18:43.611 To execute it, you need 349 00:18:43.611 --> 00:18:47.024 to call the function 350 00:18:47.024 --> 00:18:49.697 So, we call the function using its name, like this 351 00:18:52.499 --> 00:18:53.544 Same thing, 352 00:18:53.544 --> 00:18:55.588 to use it here, we need to add static 353 00:18:57.172 --> 00:19:00.271 So let's go ahead and add static here 354 00:19:00.271 --> 00:19:03.410 Now we need a value here 355 00:19:03.410 --> 00:19:06.034 This is how it'll work 356 00:19:06.034 --> 00:19:10.113 Let's write 10 and 20 here 357 00:19:10.113 --> 00:19:12.796 By doing so, 358 00:19:12.796 --> 00:19:15.129 notice how I used a comma 359 00:19:15.129 --> 00:19:16.717 between 10 and 20 360 00:19:16.717 --> 00:19:19.389 So now, we need to find 361 00:19:19.389 --> 00:19:22.350 a function that has a same structure of Add 362 00:19:22.350 --> 00:19:25.400 See this? 363 00:19:25.400 --> 00:19:27.801 So the parameter 364 00:19:27.801 --> 00:19:29.034 This is the parameter 365 00:19:30.836 --> 00:19:35.063 Those values are called arguments 366 00:19:35.063 --> 00:19:37.981 Those who receive those arguments, are called parameters 367 00:19:37.981 --> 00:19:42.400 When you pass values into a function, those are argument 368 00:19:42.400 --> 00:19:46.618 So, when we have two argument values, 369 00:19:46.618 --> 00:19:48.400 like here, 10 and 20, 370 00:19:48.400 --> 00:19:50.954 these values get passed into the Add function 371 00:19:50.954 --> 00:19:54.202 The values 10 and 20 are copied into the function parameters 372 00:19:54.202 --> 00:19:56.045 So, 10 is assigned to a, 373 00:19:56.045 --> 00:19:57.380 and 20 is assigned to b 374 00:19:58.915 --> 00:20:01.063 Now, inside the function, a holds 10, and b holds 20 375 00:20:01.063 --> 00:20:02.786 In addition, that's 30 376 00:20:02.786 --> 00:20:05.868 That value 30 gets assigned using the assignment operator 377 00:20:05.868 --> 00:20:07.221 The right-hand side performs the calculation, 378 00:20:07.221 --> 00:20:09.558 and the result gets passed to the left-hand side 379 00:20:09.558 --> 00:20:12.648 So, 30 is stored in the variable result 380 00:20:12.648 --> 00:20:15.414 The stored value, 30, 381 00:20:15.414 --> 00:20:17.588 gets returned 382 00:20:17.588 --> 00:20:20.400 Since the Add function is returning a value, 383 00:20:20.400 --> 00:20:22.217 that means the function gives back 30 384 00:20:22.217 --> 00:20:23.648 to wherever it was called 385 00:20:23.648 --> 00:20:25.400 It called the function, 386 00:20:25.400 --> 00:20:27.945 and it returns the result 387 00:20:27.945 --> 00:20:30.430 Earlier, when I explained functions, 388 00:20:30.430 --> 00:20:32.539 I mentioned input and output 389 00:20:32.539 --> 00:20:34.350 Here, in the output section, 390 00:20:34.350 --> 00:20:36.450 this is exactly what happens 391 00:20:36.450 --> 00:20:37.816 So it prints here 392 00:20:37.816 --> 00:20:40.987 And since we’re using the assignment operator, 393 00:20:40.987 --> 00:20:43.034 we can handle it more easily 394 00:20:43.034 --> 00:20:45.541 Write this, 395 00:20:45.541 --> 00:20:51.826 and add int result to the left 396 00:20:53.400 --> 00:20:58.514 By doing this, the = assignment operator 397 00:20:58.514 --> 00:21:00.612 takes the result from the right side 398 00:21:00.612 --> 00:21:03.796 and assigns it to the left side 399 00:21:03.796 --> 00:21:06.311 In other words, the function Add(10, 20) runs first, 400 00:21:06.311 --> 00:21:09.549 calculates the sum, and then returns the result 401 00:21:09.549 --> 00:21:11.865 That returned value is stored in the variable 402 00:21:11.865 --> 00:21:14.489 I used the same name for these two variables, 403 00:21:14.489 --> 00:21:18.939 but in reality, 404 00:21:18.939 --> 00:21:21.826 they’re completely different, right? 405 00:21:21.826 --> 00:21:24.420 Earlier, I explained that 406 00:21:24.420 --> 00:21:26.450 if a local variable 407 00:21:26.450 --> 00:21:28.013 has the same name 408 00:21:28.013 --> 00:21:32.301 as a field outside, 409 00:21:32.301 --> 00:21:35.568 they’re still not the same variable 410 00:21:35.568 --> 00:21:37.400 Even though they share the same name, they’re separate 411 00:21:37.400 --> 00:21:39.073 So what does that mean? 412 00:21:39.073 --> 00:21:41.182 It means we can create multiple functions, right? 413 00:21:41.182 --> 00:21:44.588 Let’s say we have two different functions, 414 00:21:44.588 --> 00:21:46.588 one called A and another called B 415 00:21:49.311 --> 00:21:52.519 Now, if I declare int a inside both functions, 416 00:21:54.895 --> 00:21:59.321 this a, this a, and this a 417 00:21:59.321 --> 00:22:01.237 they all look the same, 418 00:22:01.237 --> 00:22:03.974 but they’re actually different 419 00:22:06.717 --> 00:22:08.007 Only the name is the same 420 00:22:08.007 --> 00:22:09.657 They exist separately 421 00:22:09.657 --> 00:22:13.014 They’re completely different variables 422 00:22:13.014 --> 00:22:15.174 To prove it, 423 00:22:15.174 --> 00:22:18.143 let’s print the values 424 00:22:18.143 --> 00:22:19.466 I’ll copy the code above 425 00:22:19.466 --> 00:22:23.717 and run it 426 00:22:23.717 --> 00:22:25.537 As expected, 427 00:22:25.537 --> 00:22:29.638 30 is printed again 428 00:22:29.638 --> 00:22:32.142 Now, let’s take it a step further 429 00:22:32.142 --> 00:22:33.222 Let's try this 430 00:22:33.222 --> 00:22:37.984 I’ll create a static int result. 431 00:22:40.549 --> 00:22:42.900 Now, look at this 432 00:22:42.900 --> 00:22:45.836 We have result here, result here, and another result here 433 00:22:45.836 --> 00:22:48.925 This can get really confusing 434 00:22:48.925 --> 00:22:51.744 That’s why it’s generally not a good coding practice 435 00:22:51.744 --> 00:22:54.242 to reuse variable names like this. 436 00:22:54.242 --> 00:22:57.459 But sometimes it happens 437 00:22:57.459 --> 00:23:00.628 So the key is to fully understand what’s happening 438 00:23:00.628 --> 00:23:02.523 If you’re using Visual Studio, 439 00:23:02.523 --> 00:23:05.192 you can hover over the variable with your mouse, 440 00:23:05.192 --> 00:23:08.786 and it will highlight where the same variable is used 441 00:23:08.786 --> 00:23:10.450 See this? 442 00:23:10.450 --> 00:23:14.029 If I remove the int declaration 443 00:23:14.029 --> 00:23:18.103 from the result inside the Main function, 444 00:23:18.103 --> 00:23:21.865 then since there’s no local declaration, 445 00:23:21.865 --> 00:23:24.313 it will now use 446 00:23:24.313 --> 00:23:26.143 the existing result 447 00:23:26.143 --> 00:23:28.199 If I hover over result, 448 00:23:28.199 --> 00:23:29.400 you can see 449 00:23:29.400 --> 00:23:31.603 that it’s now linked to the static result 450 00:23:31.603 --> 00:23:33.935 outside the Main function 451 00:23:36.509 --> 00:23:37.464 See? 452 00:23:37.464 --> 00:23:39.683 So this is a static variable 453 00:23:39.683 --> 00:23:42.360 Static means it’s fixed 454 00:23:42.360 --> 00:23:44.880 That means result is a static variable, 455 00:23:44.880 --> 00:23:47.945 specifically static int result, which belongs 456 00:23:47.945 --> 00:23:50.539 to the program itself 457 00:23:50.539 --> 00:23:51.806 And this makes it a field 458 00:23:53.380 --> 00:23:58.172 So functions 459 00:23:58.172 --> 00:23:59.926 are structured like this 460 00:23:59.926 --> 00:24:01.720 Inside them, we can 461 00:24:01.720 --> 00:24:03.103 declare variables 462 00:24:03.103 --> 00:24:06.123 But these variables need to be properly managed 463 00:24:06.123 --> 00:24:08.916 because their names matter 464 00:24:08.916 --> 00:24:11.638 and they have a scope that defines their accessibility 465 00:24:11.638 --> 00:24:14.934 For example, the result variable inside Add and the result inside Main 466 00:24:14.934 --> 00:24:17.925 are completely separate 467 00:24:17.925 --> 00:24:21.289 Main cannot directly access 468 00:24:21.289 --> 00:24:23.964 the result inside Add, and vice versa 469 00:24:23.964 --> 00:24:26.063 They are completely isolated 470 00:24:26.063 --> 00:24:28.590 Even visually, 471 00:24:28.590 --> 00:24:30.756 you can see they’re completely isolated 472 00:24:30.756 --> 00:24:33.796 They can never access each other 473 00:24:33.796 --> 00:24:35.878 However, the field we created outside, 474 00:24:35.878 --> 00:24:36.885 the one I labeled as static, 475 00:24:36.885 --> 00:24:38.506 for this field, 476 00:24:38.506 --> 00:24:40.469 this can be accessed from both 477 00:24:40.469 --> 00:24:43.400 A and B functions 478 00:24:43.400 --> 00:24:44.885 That mean 479 00:24:44.885 --> 00:24:47.105 if two different functions 480 00:24:47.105 --> 00:24:51.697 need to share data, 481 00:24:51.697 --> 00:24:54.677 defining a field outside of them is a useful approach 482 00:24:56.370 --> 00:25:00.123 That's a way of thinking about this 483 00:25:00.123 --> 00:25:03.212 Right now, 484 00:25:06.994 --> 00:25:09.034 we have this Add function, 485 00:25:09.034 --> 00:25:11.103 and I'll try something new 486 00:25:11.103 --> 00:25:13.766 Let's remove this part 487 00:25:13.766 --> 00:25:15.499 We have this Add function, 488 00:25:17.251 --> 00:25:20.212 but what if we create another one? 489 00:25:20.212 --> 00:25:21.846 The same static, 490 00:25:21.846 --> 00:25:25.301 but instead of returning an int, I’ll use float, 491 00:25:25.301 --> 00:25:26.251 a decimal number type 492 00:25:33.806 --> 00:25:35.181 I’ll keep the function logic exactly the same, 493 00:25:35.181 --> 00:25:36.935 just change the return type 494 00:25:41.222 --> 00:25:43.430 and parameter types to float 495 00:25:45.469 --> 00:25:46.846 Here we have it 496 00:25:50.014 --> 00:25:51.587 Now, look at this We now have two functions 497 00:25:51.587 --> 00:25:53.350 with the exact same name, Add 498 00:25:54.776 --> 00:25:58.657 So, what happens when we call Add? 499 00:25:58.657 --> 00:26:01.162 The system is smart, 500 00:26:01.162 --> 00:26:03.529 which means this 501 00:26:03.529 --> 00:26:07.499 It automatically figures out 502 00:26:07.499 --> 00:26:10.756 which version to call 503 00:26:10.756 --> 00:26:12.319 based on the parameter types 504 00:26:12.319 --> 00:26:16.341 and number of parameters 505 00:26:16.341 --> 00:26:19.865 before it calls the function 506 00:26:19.865 --> 00:26:22.895 This feature is called function overloading 507 00:26:22.895 --> 00:26:24.024 Function overloading 508 00:26:27.935 --> 00:26:28.578 Again 509 00:26:31.014 --> 00:26:35.568 The function name is the same 510 00:26:38.687 --> 00:26:41.469 But if the parameters 511 00:26:46.232 --> 00:26:47.796 are different, 512 00:26:47.796 --> 00:26:50.024 or even the return type, 513 00:26:55.182 --> 00:26:58.628 If the return type and parameters are different, 514 00:26:58.628 --> 00:26:59.657 then when 515 00:27:07.618 --> 00:27:10.578 calling the function, 516 00:27:12.816 --> 00:27:14.648 the compiler decides which function to use 517 00:27:17.311 --> 00:27:19.479 This is called 518 00:27:19.479 --> 00:27:20.519 function overloading, 519 00:27:25.143 --> 00:27:28.697 or simply overload 520 00:27:28.697 --> 00:27:30.077 Some people just call it 521 00:27:30.077 --> 00:27:31.321 duplication, 522 00:27:31.321 --> 00:27:32.143 function duplication 523 00:27:38.440 --> 00:27:40.826 That's the overload 524 00:27:40.826 --> 00:27:47.063 So right now, we have 10 and 20, both integers 525 00:27:47.063 --> 00:27:49.697 Since they’re both integers, this is what's called in 526 00:27:49.697 --> 00:27:52.628 You can check this by hovering your mouse over Add() 527 00:27:52.628 --> 00:27:54.974 in Visual Studio, it will show which function is being used 528 00:27:56.836 --> 00:28:01.291 I’ll add an f at the end 529 00:28:01.291 --> 00:28:05.558 Then, I’ll also change the result variable to a float 530 00:28:05.558 --> 00:28:08.103 Now, when I click on Add(), 531 00:28:08.103 --> 00:28:11.281 you’ll see that this time, 532 00:28:11.281 --> 00:28:13.400 the floating-point version 533 00:28:13.400 --> 00:28:15.400 of Add is called 534 00:28:15.400 --> 00:28:18.687 This is what we call function overloading, 535 00:28:18.687 --> 00:28:19.984 overloading 536 00:28:25.400 --> 00:28:30.044 Inside the function parameters, 537 00:28:30.044 --> 00:28:34.539 we can also set default values 538 00:28:34.539 --> 00:28:36.806 For example, I’ll give b a default value of 100 539 00:28:36.806 --> 00:28:39.024 So now, 540 00:28:39.024 --> 00:28:41.558 when you hover over the function, 541 00:28:41.558 --> 00:28:43.271 it says overload 542 00:28:43.271 --> 00:28:47.717 You’ll see that b is displayed inside square brackets, 543 00:28:47.717 --> 00:28:50.836 showing that it has a default value of 100 544 00:28:50.836 --> 00:28:52.400 That's what it gives us 545 00:28:52.400 --> 00:28:54.578 You can also press Ctrl + Shift + Space 546 00:28:54.578 --> 00:28:56.420 to see the overloaded versions of the function 547 00:28:56.420 --> 00:28:59.826 So now, we have two versions 548 00:28:59.826 --> 00:29:01.400 that are overloaded 549 00:29:01.400 --> 00:29:02.717 That's what's happening 550 00:29:04.727 --> 00:29:07.628 Now, if I write the function this way, 551 00:29:07.628 --> 00:29:10.202 even if I delete this part, 552 00:29:12.974 --> 00:29:15.657 the second function 553 00:29:15.657 --> 00:29:18.083 will be called automatically 554 00:29:18.083 --> 00:29:22.638 Since this is a float, it’s better to add an f 555 00:29:22.638 --> 00:29:25.152 Now, let’s run this 556 00:29:25.152 --> 00:29:28.400 You can see that the result 110 is printed 557 00:29:28.400 --> 00:29:30.539 Why? Because I only passed one value, 10, 558 00:29:30.539 --> 00:29:34.677 but the second parameter b automatically received its default value of 100, 559 00:29:34.677 --> 00:29:36.765 so the function added them 560 00:29:36.765 --> 00:29:38.727 together to get 110 561 00:29:38.727 --> 00:29:42.816 This is called a default parameter 562 00:29:42.816 --> 00:29:44.519 or a default argument 563 00:29:44.519 --> 00:29:46.400 The parameter that 564 00:29:46.400 --> 00:29:50.103 gets a default value 565 00:29:50.103 --> 00:29:52.093 is called a default parameter 566 00:29:52.093 --> 00:29:54.182 Those are the terms for these 567 00:29:54.182 --> 00:29:56.707 This is a really useful feature, 568 00:29:56.707 --> 00:29:59.222 so it’s good to know 569 00:30:00.989 --> 00:30:04.721 Call by Value, Call by Reference 570 00:30:05.341 --> 00:30:11.053 Now, when we pass values into a function like this, 571 00:30:11.053 --> 00:30:13.024 the parameters receive them 572 00:30:13.024 --> 00:30:15.749 In C#, data types are broadly 573 00:30:15.749 --> 00:30:18.291 categorized into two types 574 00:30:18.291 --> 00:30:20.281 One is value type 575 00:30:23.261 --> 00:30:26.073 Another is reference type 576 00:30:30.499 --> 00:30:31.677 Value means an actual value 577 00:30:33.261 --> 00:30:36.974 So we call these value types 578 00:30:36.974 --> 00:30:38.578 Reference means it refers to something, 579 00:30:45.024 --> 00:30:46.618 so we call these reference types 580 00:30:46.618 --> 00:30:49.717 So these are the two types 581 00:30:49.717 --> 00:30:53.578 In C#, we use class a lot 582 00:30:53.578 --> 00:30:55.400 We’ll cover it in the next lesson, 583 00:30:55.400 --> 00:30:57.648 but basically, a class is also a data type 584 00:30:57.648 --> 00:31:00.737 A class is what we call a user-defined type 585 00:31:09.261 --> 00:31:10.480 A type 586 00:31:10.480 --> 00:31:11.865 simply means a data type 587 00:31:11.865 --> 00:31:14.895 So when we say user-defined type, 588 00:31:14.895 --> 00:31:16.499 we’re talking about a type that the user creates 589 00:31:16.499 --> 00:31:17.776 If it’s a class, 590 00:31:17.776 --> 00:31:21.232 then it’s a reference type 591 00:31:21.232 --> 00:31:25.133 If it’s anything else, 592 00:31:25.133 --> 00:31:27.994 like an enum, 593 00:31:27.994 --> 00:31:29.400 and other things, 594 00:31:29.400 --> 00:31:30.662 those are all this 595 00:31:30.662 --> 00:31:31.875 It's something simple 596 00:31:31.875 --> 00:31:36.192 If class, reference If else, it’s a value type 597 00:31:36.192 --> 00:31:37.707 That's how it works 598 00:31:41.697 --> 00:31:46.251 Let's see this in action 599 00:31:48.024 --> 00:31:50.846 Write 20 again 600 00:31:50.846 --> 00:31:53.846 Let's reset this part 601 00:31:56.162 --> 00:31:59.875 So this passed 10 and 20 as arguments, 602 00:31:59.875 --> 00:32:01.756 and they got received here 603 00:32:01.756 --> 00:32:03.172 to be processed 604 00:32:03.172 --> 00:32:05.400 See here, 605 00:32:05.400 --> 00:32:08.222 let me change this 606 00:32:08.222 --> 00:32:10.727 Int num, no, not an int 607 00:32:10.727 --> 00:32:13.717 Float num1 608 00:32:13.717 --> 00:32:15.271 Add 10 to this 609 00:32:17.747 --> 00:32:20.707 And 20 to num2 610 00:32:22.311 --> 00:32:24.073 Let's just work with this 611 00:32:24.073 --> 00:32:26.786 Now, when we pass arguments into a function, 612 00:32:26.786 --> 00:32:30.152 we can directly input literal values, like this 613 00:32:30.152 --> 00:32:34.053 But we can also use variables instead 614 00:32:34.053 --> 00:32:38.529 So the values stored in those variables 615 00:32:38.529 --> 00:32:41.350 are what get passed into the function 616 00:32:41.350 --> 00:32:43.786 Since we’re passing the actual value, 617 00:32:43.786 --> 00:32:47.053 this method is called Call by Value 618 00:32:48.885 --> 00:32:49.994 Call by Value 619 00:32:49.994 --> 00:32:52.925 Call by Value means we’re passing the value itself 620 00:32:52.925 --> 00:32:56.945 So, when the function is called, it’s handling a copy of the original value 621 00:32:56.945 --> 00:33:03.400 So the term for this 622 00:33:03.400 --> 00:33:08.450 is Call by Value 623 00:33:10.945 --> 00:33:13.103 Now, what if we don’t want to pass a copy 624 00:33:13.103 --> 00:33:15.885 but instead pass a reference? 625 00:33:15.885 --> 00:33:19.667 Earlier, I mentioned that only classes are reference types, 626 00:33:19.667 --> 00:33:21.648 while everything else 627 00:33:21.648 --> 00:33:23.578 is a value type 628 00:33:23.578 --> 00:33:26.459 That means basic data types like int and float 629 00:33:26.459 --> 00:33:29.895 are value types as well 630 00:33:29.895 --> 00:33:33.004 For example, if you press F12 631 00:33:33.004 --> 00:33:35.063 on int or float, 632 00:33:35.063 --> 00:33:36.737 you can see that these types are actually structs 633 00:33:36.737 --> 00:33:39.053 So they are all structs 634 00:33:43.034 --> 00:33:44.370 Let's go back 635 00:33:44.370 --> 00:33:46.321 Since structs and enums 636 00:33:46.321 --> 00:33:47.638 are value types, 637 00:33:47.638 --> 00:33:49.289 and all basic data types 638 00:33:49.289 --> 00:33:51.192 are based on structs, 639 00:33:51.192 --> 00:33:52.756 as you see here, struct, 640 00:33:52.756 --> 00:33:54.657 they’re also value types 641 00:33:54.657 --> 00:33:57.539 However, we can force 642 00:33:57.539 --> 00:34:00.469 value types 643 00:34:00.469 --> 00:34:03.568 o act like reference types 644 00:34:03.568 --> 00:34:06.737 To do that, 645 00:34:06.737 --> 00:34:09.241 we use the ref keyword 646 00:34:11.379 --> 00:34:12.676 I’ll add ref here in both places 647 00:34:12.676 --> 00:34:15.637 So ref stands for reference 648 00:34:15.637 --> 00:34:19.557 By adding ref, parameters a and b 649 00:34:19.557 --> 00:34:22.726 are now treated as references, not values 650 00:34:22.726 --> 00:34:24.845 That means instead of passing a copy, 651 00:34:24.845 --> 00:34:26.914 let's write ref here 652 00:34:29.132 --> 00:34:30.765 Now, 653 00:34:30.765 --> 00:34:33.399 even though int and float are value types, 654 00:34:33.399 --> 00:34:36.191 using ref turns them into reference types temporarily 655 00:34:38.567 --> 00:34:41.399 Now we need to know the difference 656 00:34:41.399 --> 00:34:43.201 between the two types 657 00:34:43.201 --> 00:34:44.518 To understand the difference, 658 00:34:46.726 --> 00:34:48.894 let’s create two versions of the function 659 00:34:48.894 --> 00:34:52.934 This is Add and value 660 00:34:54.122 --> 00:34:55.805 Let's remove this 661 00:34:58.528 --> 00:35:00.003 This is Add value 662 00:35:00.003 --> 00:35:02.013 This is Add reference 663 00:35:10.647 --> 00:35:12.746 Let's call each 664 00:35:15.280 --> 00:35:17.864 This is Add call by value 665 00:35:17.864 --> 00:35:19.874 This is call by reference 666 00:35:21.845 --> 00:35:23.647 Call by Value 667 00:35:23.647 --> 00:35:26.904 Let' change the function name 668 00:35:26.904 --> 00:35:28.894 So we need to remove ref now 669 00:35:32.340 --> 00:35:34.746 Now let's print this 670 00:35:34.746 --> 00:35:39.043 Let's print this num1 671 00:35:39.043 --> 00:35:41.924 We know the result already 672 00:35:41.924 --> 00:35:44.260 So this, 673 00:35:44.260 --> 00:35:47.439 we have the a that has been passed 674 00:35:47.439 --> 00:35:49.310 Let's actually start with value 675 00:35:49.310 --> 00:35:50.666 Let's move it down a little 676 00:35:53.528 --> 00:35:54.874 Let's try it with this 677 00:35:56.399 --> 00:35:57.399 At call by value, 678 00:35:57.399 --> 00:36:01.241 num1 has 10 679 00:36:01.241 --> 00:36:03.399 and we passed num1 to this 680 00:36:03.399 --> 00:36:05.280 So the variable a 681 00:36:05.280 --> 00:36:07.607 now receives the value within num1 682 00:36:07.607 --> 00:36:08.825 The parameter 683 00:36:08.825 --> 00:36:11.399 So I will now 684 00:36:11.399 --> 00:36:12.637 change the value of a 685 00:36:12.637 --> 00:36:14.191 to 100 686 00:36:22.627 --> 00:36:28.607 Let's now print 687 00:36:28.607 --> 00:36:30.983 What I did is this 688 00:36:30.983 --> 00:36:33.439 So, when we call a function, 689 00:36:33.439 --> 00:36:35.320 let’s look at how the execution flows 690 00:36:35.320 --> 00:36:37.161 First, Main function runs, 691 00:36:37.161 --> 00:36:38.775 and then the code executes line 692 00:36:38.775 --> 00:36:40.241 by line from top to bottom 693 00:36:40.241 --> 00:36:43.349 Now, when it reaches this = operator, 694 00:36:43.349 --> 00:36:45.498 the right-hand side runs first 695 00:36:45.498 --> 00:36:47.429 That means the AddValue function 696 00:36:47.429 --> 00:36:48.874 is called first 697 00:36:48.874 --> 00:36:53.102 At this point, execution jumps to the function, 698 00:36:53.102 --> 00:36:54.637 so the flow of time moves to AddValue 699 00:36:54.637 --> 00:36:56.449 Now, num1 and num2, 700 00:36:56.449 --> 00:36:58.627 are 10 and 20 701 00:36:58.627 --> 00:37:00.647 Next, a is set to 10 again 702 00:37:00.647 --> 00:37:02.617 and then we add the numbers together 703 00:37:02.617 --> 00:37:04.904 After that, the function returns the result 704 00:37:04.904 --> 00:37:07.716 Now, back in Main, 705 00:37:07.716 --> 00:37:10.151 the returned value is stored in result, 706 00:37:10.151 --> 00:37:11.795 and then we print num1 707 00:37:11.795 --> 00:37:13.666 to check its value 708 00:37:13.666 --> 00:37:16.052 So, here’s what we’re testing 709 00:37:16.052 --> 00:37:18.043 We passed num1 710 00:37:18.043 --> 00:37:19.656 into the function, right? 711 00:37:19.656 --> 00:37:21.854 The value num1, 712 00:37:21.854 --> 00:37:24.577 that's what has been passed here 713 00:37:24.577 --> 00:37:26.567 And inside the function, 714 00:37:26.567 --> 00:37:28.656 the value for a 715 00:37:28.656 --> 00:37:29.528 was changed to 100 716 00:37:29.528 --> 00:37:30.904 It used to be 10, 717 00:37:30.904 --> 00:37:32.241 but we changed it to 100 718 00:37:32.241 --> 00:37:33.528 So the question here is this 719 00:37:33.528 --> 00:37:35.765 So, does num1 also become 100? 720 00:37:35.765 --> 00:37:37.399 Let’s check the output 721 00:37:37.399 --> 00:37:39.656 for num1 722 00:37:39.656 --> 00:37:42.429 So it has not changed to 100 723 00:37:42.429 --> 00:37:43.340 See? 724 00:37:43.340 --> 00:37:44.557 This happens 725 00:37:44.557 --> 00:37:47.647 because Call by Value means 726 00:37:47.647 --> 00:37:49.250 the function only gets 727 00:37:49.250 --> 00:37:52.201 a copy of the value 728 00:37:52.201 --> 00:37:54.399 Imagine you have 729 00:37:54.399 --> 00:37:56.349 a 100-won coin in your hand 730 00:37:56.349 --> 00:37:59.349 And you give it to someone, 731 00:37:59.349 --> 00:38:00.864 but here's the twist 732 00:38:00.864 --> 00:38:03.290 Instead of giving the actual coin to someone, 733 00:38:03.290 --> 00:38:04.647 you make a duplicate 734 00:38:04.647 --> 00:38:07.221 and hand that over 735 00:38:07.221 --> 00:38:08.389 Now, whatever they do 736 00:38:08.389 --> 00:38:11.260 with that 100-won coin, 737 00:38:11.260 --> 00:38:13.548 even if they draw on it 738 00:38:13.548 --> 00:38:14.795 or scratch it, 739 00:38:14.795 --> 00:38:15.953 your original coin 740 00:38:15.953 --> 00:38:18.676 stays the same 741 00:38:18.676 --> 00:38:22.478 That's what happens here, making a copy 742 00:38:22.478 --> 00:38:24.963 That’s exactly what Call by Value does 743 00:38:24.963 --> 00:38:26.082 The function works with a copy, 744 00:38:26.082 --> 00:38:27.518 making a duplicate 745 00:38:28.993 --> 00:38:30.854 This is why 746 00:38:30.854 --> 00:38:34.538 changes don’t affect the original variable 747 00:38:34.538 --> 00:38:37.340 That's what Call by Value does 748 00:38:37.340 --> 00:38:39.112 Let's not move on 749 00:38:39.112 --> 00:38:40.567 to Call by Reference 750 00:38:40.567 --> 00:38:42.953 This time, let’s call AddReference 751 00:38:44.825 --> 00:38:47.607 and store the result in result 752 00:38:47.607 --> 00:38:50.092 We’re passing in num1 and num2 just like before, 753 00:38:50.092 --> 00:38:53.003 but now, we must add ref here, 754 00:38:53.003 --> 00:38:54.755 since this is Call by Reference 755 00:38:54.755 --> 00:38:55.953 So we need to update 756 00:38:55.953 --> 00:38:57.449 the function definition as well 757 00:39:05.399 --> 00:39:08.013 Now, let’s label this section with a comment 758 00:39:08.013 --> 00:39:14.468 Call by Reference 759 00:39:14.468 --> 00:39:25.191 So this is Call by Reference 760 00:39:27.627 --> 00:39:29.607 It's a comment 761 00:39:29.607 --> 00:39:31.221 Comment 762 00:39:31.221 --> 00:39:35.221 For a reference, the original value changes 763 00:39:35.221 --> 00:39:38.221 So, what happens is that 764 00:39:38.221 --> 00:39:41.201 when you pass a value using ref, 765 00:39:41.201 --> 00:39:43.399 the function receives a 766 00:39:43.399 --> 00:39:45.201 as num1 itself 767 00:39:47.429 --> 00:39:49.399 Technically, it’s a bit different, 768 00:39:49.399 --> 00:39:51.656 but this is the basic idea 769 00:39:51.656 --> 00:39:56.904 There is a memory space 770 00:39:56.904 --> 00:40:00.023 where the data is stored, right? 771 00:40:00.023 --> 00:40:02.528 This memory space reference 772 00:40:02.528 --> 00:40:04.647 is passed along as information 773 00:40:04.647 --> 00:40:06.785 So now, this function 774 00:40:06.785 --> 00:40:08.973 actually retains 775 00:40:08.973 --> 00:40:12.171 the memory location of num1 776 00:40:12.171 --> 00:40:15.468 So it means the original value itself is being transferred 777 00:40:15.468 --> 00:40:18.072 Now, let’s assign 778 00:40:18.072 --> 00:40:21.488 100 to a 779 00:40:21.488 --> 00:40:23.399 The same logic applies 780 00:40:23.399 --> 00:40:26.142 This num1 originally held the value 10 781 00:40:26.142 --> 00:40:29.755 But since we passed num1 as a reference, 782 00:40:29.755 --> 00:40:33.142 in the AddReference function, 783 00:40:33.142 --> 00:40:36.399 we changed a to 100 784 00:40:36.399 --> 00:40:38.538 What happens next? 785 00:40:38.538 --> 00:40:40.290 The value of num1 itself 786 00:40:40.290 --> 00:40:42.072 is updated to 100 787 00:40:44.062 --> 00:40:45.399 Let’s add an f at the end 788 00:40:45.399 --> 00:40:46.716 and run it 789 00:40:48.449 --> 00:40:49.755 See that? 790 00:40:49.755 --> 00:40:50.914 The value has changed to 100 791 00:40:53.241 --> 00:40:55.449 Call by Value 792 00:40:55.449 --> 00:40:56.805 and Call by Reference 793 00:40:56.805 --> 00:41:00.894 are two important concepts 794 00:41:00.894 --> 00:41:03.726 When you’re working on something, 795 00:41:03.726 --> 00:41:08.082 if a variable is created using a class, 796 00:41:08.082 --> 00:41:10.300 it operates under Call by Reference 797 00:41:10.300 --> 00:41:12.112 Please remember this 798 00:41:12.112 --> 00:41:15.023 Otherwise, 799 00:41:15.023 --> 00:41:17.874 if you pass such a variable as a function parameter 800 00:41:17.874 --> 00:41:21.102 and modify it inside the function, 801 00:41:21.102 --> 00:41:24.280 the original value will change 802 00:41:24.280 --> 00:41:26.250 That’s why you need to be clear on 803 00:41:26.250 --> 00:41:27.845 whether you’re working with a copy 804 00:41:27.845 --> 00:41:29.013 or directly modifying the original 805 00:41:29.013 --> 00:41:30.755 This has to be clarified first 806 00:41:33.399 --> 00:41:36.102 Now, let’s look 807 00:41:36.102 --> 00:41:38.884 at one last thing 808 00:41:38.884 --> 00:41:40.320 I’ll create another 809 00:41:40.320 --> 00:41:42.171 Add function here 810 00:41:42.171 --> 00:41:42.726 static 811 00:41:44.142 --> 00:41:46.548 I'll keep the float 812 00:41:46.548 --> 00:41:49.577 float Add 813 00:41:49.577 --> 00:41:50.835 So now we have it 814 00:42:00.399 --> 00:42:02.548 Now, return result 815 00:42:02.548 --> 00:42:04.775 That's the code we have 816 00:42:04.775 --> 00:42:07.538 But this time, let's change 817 00:42:07.538 --> 00:42:09.092 this flat to void 818 00:42:12.874 --> 00:42:15.944 And remove return result 819 00:42:19.003 --> 00:42:20.894 Instead, let's write 820 00:42:20.894 --> 00:42:23.488 add a comma, 821 00:42:23.488 --> 00:42:25.637 and use a keyword called out 822 00:42:25.637 --> 00:42:26.164 out 823 00:42:28.260 --> 00:42:30.765 And this result here, 824 00:42:30.765 --> 00:42:35.944 let's write this out here 825 00:42:35.944 --> 00:42:38.518 And remove what we have here 826 00:42:38.518 --> 00:42:40.052 Remove the data type 827 00:42:40.052 --> 00:42:41.567 Let's create the code like this 828 00:42:47.013 --> 00:42:49.379 You may have already noticed 829 00:42:49.379 --> 00:42:52.765 This keyword out is this 830 00:42:52.765 --> 00:42:56.161 It's essentially a way 831 00:42:56.161 --> 00:42:58.894 to return a value indirectly 832 00:42:58.894 --> 00:43:00.231 That's what it is 833 00:43:03.005 --> 00:43:03.528 Right? 834 00:43:03.528 --> 00:43:05.607 In a typical function, 835 00:43:05.607 --> 00:43:09.458 you specify the return type 836 00:43:09.458 --> 00:43:10.944 and return a value explicitly 837 00:43:10.944 --> 00:43:14.389 That's how it works 838 00:43:14.389 --> 00:43:18.518 However, in some cases, 839 00:43:18.518 --> 00:43:20.062 you might want to handle things differently 840 00:43:20.062 --> 00:43:23.290 For example, let’s say you want to perform addition 841 00:43:23.290 --> 00:43:26.132 but also determine 842 00:43:26.132 --> 00:43:28.607 whether one number is greater than the other 843 00:43:28.607 --> 00:43:30.845 While this might not be a typical use case, 844 00:43:30.845 --> 00:43:33.458 you could return a boolean 845 00:43:33.458 --> 00:43:34.775 That's the case 846 00:43:34.775 --> 00:43:37.250 Let's try this 847 00:43:37.250 --> 00:43:41.775 If a is greater than b, 848 00:43:41.775 --> 00:43:45.181 return True 849 00:43:45.181 --> 00:43:46.290 If not, 850 00:43:48.617 --> 00:43:50.052 return False 851 00:43:53.023 --> 00:43:55.349 That's the function 852 00:43:55.349 --> 00:43:57.696 that we may want to make 853 00:43:57.696 --> 00:44:01.320 So, for the return value, 854 00:44:01.320 --> 00:44:03.112 if a is greater than b, we return true 855 00:44:03.112 --> 00:44:07.132 a is bigger than b, it's true 856 00:44:07.132 --> 00:44:09.973 That's a way of doing this, right? 857 00:44:09.973 --> 00:44:12.310 This means 858 00:44:12.310 --> 00:44:14.449 the function is now 859 00:44:14.449 --> 00:44:16.191 structured to return two things 860 00:44:16.191 --> 00:44:21.221 First, whether a is greater than b, 861 00:44:21.221 --> 00:44:26.112 and second, the sum of a and b 862 00:44:26.112 --> 00:44:28.963 These are the two pieces of information 863 00:44:28.963 --> 00:44:31.488 we can get with this structure 864 00:44:31.488 --> 00:44:33.914 And the characteristic of the out keyword is that, 865 00:44:33.914 --> 00:44:36.023 as I explained earlier with Call by Reference 866 00:44:36.023 --> 00:44:38.488 using the keyword ref, 867 00:44:38.488 --> 00:44:40.171 the keyword out 868 00:44:40.171 --> 00:44:43.617 also works as a reference type 869 00:44:43.617 --> 00:44:44.389 A reference type 870 00:44:44.389 --> 00:44:47.132 Since it is a reference type, 871 00:44:47.132 --> 00:44:49.132 it directly affects the original value 872 00:44:50.944 --> 00:44:53.399 But here's the thing 873 00:44:53.399 --> 00:44:56.023 about ref and out 874 00:44:56.023 --> 00:44:57.290 Here's the thing 875 00:44:57.290 --> 00:45:01.607 For example, I could choose not to assign a value 876 00:45:01.607 --> 00:45:05.023 That means, if I use the out keyword 877 00:45:05.023 --> 00:45:07.407 for a parameter but do not actually 878 00:45:07.407 --> 00:45:10.438 assign a value inside the function, it will cause an error 879 00:45:10.438 --> 00:45:12.245 With out, assigning a value is mandatory 880 00:45:12.245 --> 00:45:15.458 before the function exits 881 00:45:15.458 --> 00:45:20.706 On the other hand, if we use ref, 882 00:45:20.706 --> 00:45:22.924 we don’t necessarily need to assign a new value inside the function 883 00:45:24.676 --> 00:45:27.577 It won't give us an error without the = sign 884 00:45:27.577 --> 00:45:30.399 But it will for out 885 00:45:30.399 --> 00:45:32.587 We need a value inside 886 00:45:34.854 --> 00:45:36.755 That's the difference 887 00:45:36.755 --> 00:45:38.716 So, when calling the function, you can do it like this 888 00:45:38.716 --> 00:45:40.211 If we call the Add function, 889 00:45:43.696 --> 00:45:44.487 let's say this 890 00:45:47.071 --> 00:45:49.250 Let's write it out 891 00:45:49.250 --> 00:45:50.211 Like this 892 00:45:55.746 --> 00:45:57.716 Call Add 893 00:45:57.716 --> 00:46:01.983 And we pass in num1 and num2 as usual 894 00:46:01.983 --> 00:46:04.112 Let me do it again 895 00:46:04.112 --> 00:46:06.746 We first create a variable to store the result 896 00:46:06.746 --> 00:46:16.835 Bool type, isGreater 897 00:46:21.478 --> 00:46:22.201 Is it correct? 898 00:46:22.201 --> 00:46:24.270 That's the way we can write this 899 00:46:25.825 --> 00:46:29.755 Is a greater? 900 00:46:29.755 --> 00:46:31.934 This may feel a bit forced 901 00:46:31.934 --> 00:46:33.379 but that's a way of doing it 902 00:46:33.379 --> 00:46:36.825 Now we write the keyword out 903 00:46:36.825 --> 00:46:37.587 And add it here 904 00:46:40.934 --> 00:46:42.330 Let's see what it does 905 00:46:44.587 --> 00:46:46.300 Let's print it 906 00:46:53.151 --> 00:46:56.092 Then the entire result is this 907 00:46:56.092 --> 00:46:59.637 a, b, the out keyword, result 908 00:46:59.637 --> 00:47:04.092 And the fact that a is greater than b 909 00:47:04.092 --> 00:47:06.102 is returned as a feedback 910 00:47:06.102 --> 00:47:08.151 in a Bool type like this 911 00:47:08.151 --> 00:47:12.310 So when we call this function, 912 00:47:12.310 --> 00:47:13.082 this is what happens 913 00:47:16.211 --> 00:47:19.686 We previously discussed int.TryParse 914 00:47:19.686 --> 00:47:23.320 Remember the int.TryParse function? 915 00:47:23.320 --> 00:47:25.805 This is how it looks like 916 00:47:25.805 --> 00:47:28.132 It returns a bool value 917 00:47:28.132 --> 00:47:33.072 Then, when you pass a numeric string, 918 00:47:33.072 --> 00:47:35.755 the function processes it and returns the result 919 00:47:35.755 --> 00:47:37.647 using the out keyword 920 00:47:37.647 --> 00:47:40.716 So this is usually 921 00:47:40.716 --> 00:47:42.300 how we make this 922 00:47:44.211 --> 00:47:45.415 So to the out keyword, 923 00:47:45.415 --> 00:47:47.280 add it here like so 924 00:47:47.280 --> 00:47:49.072 And print it 925 00:47:49.072 --> 00:47:54.300 Of course it will print 30, right? 926 00:47:54.300 --> 00:47:57.320 And let's check our IsGreater part 927 00:48:17.637 --> 00:48:18.706 Let's do this 928 00:48:30.250 --> 00:48:34.215 This will calculate and decide 929 00:48:34.215 --> 00:48:36.726 which of a and b is bigger 930 00:48:36.726 --> 00:48:39.795 Since a is bigger than b, it's false 931 00:48:39.795 --> 00:48:42.249 So false, and the result, 30 932 00:48:42.249 --> 00:48:45.924 This is what we get out of this 933 00:48:45.924 --> 00:48:48.060 Like this, we can do 934 00:48:48.060 --> 00:48:50.201 many thing using a function 935 00:48:50.201 --> 00:48:52.043 Let's recap the important things about a function 936 00:48:52.043 --> 00:48:56.033 It has a return type 937 00:48:56.033 --> 00:48:57.782 Now, a function name 938 00:48:57.782 --> 00:48:59.518 Next, parameters 939 00:48:59.518 --> 00:49:01.399 There are many kinds of a parameter 940 00:49:01.399 --> 00:49:04.706 This is the basic structure 941 00:49:04.706 --> 00:49:06.214 You could add keywords 942 00:49:06.214 --> 00:49:08.043 like out and ref 943 00:49:08.043 --> 00:49:10.787 Now, to the variable, 944 00:49:10.787 --> 00:49:12.706 you can add default values like this 945 00:49:16.429 --> 00:49:19.696 However, when using out, 946 00:49:19.696 --> 00:49:23.280 setting default values can be tricky 947 00:49:23.280 --> 00:49:26.786 Nonetheless, functions provide 948 00:49:26.786 --> 00:49:29.086 various ways 949 00:49:29.086 --> 00:49:30.439 to structure logic 950 00:49:31.944 --> 00:49:35.538 Let's summarize what we've learned today 951 00:49:35.857 --> 00:49:38.297 Functions: Concept, Syntax, Parameters, and Overloading Performs a specific task Return type, function name, parameter, return 952 00:49:38.297 --> 00:49:40.680 Parameters Have different types of parameters, which define relationships between function spaces Local variable: Declared inside Field: Outside 953 00:49:40.680 --> 00:49:42.689 out, ref keywords can be used or default values can be set 954 00:49:42.689 --> 00:49:45.417 Functions must be called to execute; definition alone does not run them Defining a function 955 00:49:45.417 --> 00:49:47.632 Calling a function 956 00:49:47.632 --> 00:49:49.699 Overloading When multiple functions have the same name, the compiler selects the correct one based on parameter types, count, and return type 957 00:49:49.699 --> 00:49:51.342 Call by Value and Call by Reference C# Data Types Reference type: Created using classes Value type: Everything except classes 958 00:49:51.342 --> 00:49:52.753 Call by Value Arguments are copied when passed to a function The original value remains unchanged 959 00:49:52.753 --> 00:49:54.176 Call by Reference Specify ref in parameters and arguments to force reference type conversion 960 00:49:54.176 --> 00:49:55.620 Calling AddReference accesses the data’s memory space, altering the original value Class-based variables use Call by Reference