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
View all comments
Show parent comment
1
u/IncognitoErgoCvm Apr 27 '24
You could call
add(num1=2, num2=4); those are called keyword arguments. However, inadd(2, 4), they're being passed as positional arguments.When evaluating the expression
add(2, 4),2, is bound to the first parameter,num1.4is bound to the second parameter,num2.return num1 * num2evaluates toreturn 2 * 4return 2 * 4evaluates toreturn 88is returned to the callerThus,
print(add(2, 4))evaluates as equivalent toprint(8)