r/pythonhelp • • Apr 26 '24
Why does this code work? NSFW
def add(num1, num2):
return num1 * num2
def main():
print(add(1,1))
print(add(2,4))
print(add(10,10))
main()
I dont understand how the math in the middle become the arguments for the first function, like how. I can not figure out.
Looking forward to find the solution
2
Upvotes
•
u/AutoModerator Apr 26 '24
To give us the best chance to help you, please include any relevant code.
Note. Do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Repl.it, GitHub or PasteBin.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/carcigenicate Apr 26 '24
What do you mean by "the math in the middle becomes the arguments"?
In
add(2, 4), 2 and 4 are the arguments. They're stored in the parametersnum1andnum2when the function is called, and multiplication is done onnum1(2) andnum2(4).1
u/Goobyalus Apr 26 '24
Does this help? https://imgur.com/a/ItUiu59
1
u/PervyNonsense Apr 27 '24
definition of "add" function: When called, take the two numbers in parentheses (num1, num2) and multiply them, then spit out the result (return) End function definition.
Program runs; add() function is called and result is printed.
Example 2: print(add(2, 4)
Num1 = 2, num2 =4 2*4 = 8 Return 8 back to function call ... so print(add(2, 4)) becomes print(8) 8 is printed to the console
Num1 and num2 are placeholders for any number (each called an argument).
Im repeating myself but the thing I think you're struggling with is how arguments work.
"Add" is an arbitrary name. It could be called anything. Num1 and num2 are what "add" needs as inputs, then inside the function is shown what happens to those numbers (they're multiplied). Return is the output of the function, or the result of running it.
As a math question this would be:
For each set of values (1,1), (2,4), and (10,10), as (x,y), what is the result of x * y?
1* 1 = 1 2*4 = 8 10 * 10= 100
Make sense?
1
u/KingOfTNT10 Apr 27 '24 edited Apr 27 '24
I saw you got confused a little by what a function call really means. So you think that print(add(2,4)) doesnt call add, but it does. First, python looks at the deepest element (i know its confusing hang on). So print(add(2,4)) gets divided into 2 parts, First is the add function call add(2,4) And then the print function call. Python first runs add(2,4), gets the value back from the function which would be 8 (idk why u did * instead of + but doesnt matter) and then gives it to the print function to print. Hope this helps. And btw dint give up on programming, right now this is the hardest part, push through this and it'll become easier. If you have any questions lmk.
P.S. Its like how you would do: var = add(2,4) add is ran, geets back the result and you can think of it as replacing the function call with the return value So you do var = add(2,4) And it will become var = 8 after the function is done. Same with your example: print(add(2,4)) Becomes print(8)