+ 2

I don't understand Arguments

To be exact, I don't understand how this code outputs number 8! >>> def function(variable): variable += 1 print(variable) >>>

16th Mar 2018, 8:49 PM
Lilek
Lilek - avatar
4 odpowiedzi
+ 7
It is a function definition. It adds 1 to its argument and prints the augmented value. The above will print 8 only if you call the function with 7 as argument.
16th Mar 2018, 9:18 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
16th Mar 2018, 9:28 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
Could you write an example code? The one in my course is confusing to me.
16th Mar 2018, 9:25 PM
Lilek
Lilek - avatar
0
Let me try to give you an illustrative example (it is a general one, regardless of the programming language) A function acts on its arguments in some specific manner, which you can define. For example: f(x) = x + 2 This function acts on the argument x by taking the value of x and adding to that value 2. Well.. x as argument is just arbitrary choosen: f(anything) = anything + 2 is the same thing. All what a function does is acting on arguments. How those arguments are named is arbitrary. In your case what the function does is to take the value of the argument "variable" and adds to that value 1. As mentioned above "variable" is just arbitrary. Names like "someValue" or "cheese" instead of "variable" would work the same way. By writing for example function(7) you are telling the program that the argument "variable" has the value 7. After the function has acted on the argument it will output "7+1" which equals 8. Hope that helps somehow :).
17th Mar 2018, 8:36 AM
Giuseppe T
Giuseppe T - avatar