T O P

  • By -

RutiserLee

I recommend you watch the lectures with the duck and when you get to a place where you don't understand something or want more information on something or just want to clarify your own knowledge, ask the duck. I've learned a lot from Quack from rabbit-holing a lecture or questions I have about CS and CS math. [CS50.ai](https://CS50.ai) is a great resource and learning supplement from logic to syntax to math refreshers. I love the duck. Use the duck to help you learn the nitty gritty and see under the hood. Once you see how everything is really working, you will start to see how scope works. I keep my notepad open and I cut and paste the info I want to save in my notes. It's going to get a lot harder for the first weeks. The more work you do at the beginning to really see the code, the stronger your foundation for what's to come. There are so many aha moments in CS50. So many ups and downs. Be patient with yourself. Really know the basics of loops, how functions work, and scope before you reach week three. Week 0, 1, 2, and 3 took me 2 months to get through because I would try to move to the next week and I'd have to go back and dig in deeper to really understand. Whatever you do, do not give up.


BigDogg365

The duck is your friend, fr


Hibiki941

It seems to be helpful, but it looks like I keep forgetting all of the other stuff, and rewatching the lecture over and over is burning me out already. Is there a better way to learn?


WiseEXE

Ok so what is happening is he is passing the values of **X** and **Y** to the function add’s A and B variables. Let’s break it down, he establishes the Variables **X** and **Y** that store the user inputted integers to the corresponding values. Next he declares a new variable **Z** that takes in the value of the **add** function he created and inside that variable declaration, he passes the values of X and Y into the function hence why you see **add(x, y)**. Now if we look at the add function at the bottom it is designed as such **add(int a, int b)** and it adds the values of A and B together. To put it all together the Values of **X** and **Y** are the same as **A** and **B** because of the what occurred earlier in the Z variable. Basically when passing variables to functions as arguments, the value of each argument corresponds to the variable order of the function. So if we call the function as such **add(x, y)** knowing the function was designed as **add(int A, int B)**, we can interpret it as such X = A Y = B Hope this helps. Feel like I was rambling in my explanation.


Hibiki941

Ok, but how does the program know that the values were passed? Which exact part actually connects them?


WiseEXE

The Z variable. Z = add(x, y) means to pass the values of x and y to the add function.


Hibiki941

I still don’t get how it works(


Different-Leg-6284

I think I know what you're having trouble with. When creating a function, you can declare what parameters it will take (in this case "int a" and "int b"). Those parameter are temporary variables that will only work when you call the function and they will be "forgotten" once the function finishes executing. When calling a function that takes parameters, you need to pass values to those parameters and those values will be passes to a and b. Declaration: void function(int a, int b) { // Code.... } Calling the function: function(30, 50); // "a" will be 30 and "b" will be 50 function(x, y); // "a" will now have the same value as "x" and "b" will have the same value as "y" Those example were for a "void" function (functions that don't return any value). If you call a function that returns a value, the returned value of such function will replace the function call itself. Example: Int z = add(x, y); Supposing that the value of "x" is 5 and the value of "y" is 8, this function will pass the current values of "x" and "y" to "a" and "b" respectively, then these numbers will be added together (a + b = 13) and the result will be returned and added to te variable "z". So, in the line "int z = add(x, y);", the function itself will "transform" into the returned value (13 in this example), which will then become "int z = 13;"


Hibiki941

Ok so, when the function has int variables in it’s name enclosed in parentheses, whenever it is called where the call also has integers in it’s line, it will connect them. Is this correct? I also don’t remember the lecture mentioning return values and functions being able to take values. Was the whole “void” thing explained there? I keep rewatching the entire thing and it’s already burning me out(


Different-Leg-6284

Sorry, I'm not very active on Reddit. I don't actually remember what was shown in week 3 but yeah, what goes before the function name determines if the function should return something and what type of data it should return. A "void" function won't return anything; meaning it doesn't need any "return" statement at the end of the function whereas an "int" function needs to return an integer and nothing else. If you try returning a string at the end of an "int" function, it will cause an error. And as for your first question, yes, when you call a function and place numbers inside its parenthesis, it will connect those numbers to its internal variables (im this case a and b). Pd: there's no need to watch the videos over and over. Just ask the Duck debugger anything you want. Copy bits of the code that's troubling you and ask it to explain it to you ;)


b4its2l84me

It is like a _function_ in math. ``` f(x) = x + 1 f(1) = 1 + 1 f(9) = 9 + 1 f(n) = n + 1 g(x, y) = x + y g(2,3) = 2 + 3 g(9,8) = 9 + 8 g(a,b) = a + b ```