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

19 comments sorted by

View all comments

Show parent comment

1

u/carcigenicate Apr 26 '24

Again, it is called. add(2, 4) s a function call. That line calls add.

And you can create those variables in main, but they'll have no relation to the variables of the same name in func. You need to call the function to pass data to the function.

1

u/[deleted] Apr 26 '24

thanks for answering but I still dont get it. Maybe coding is too hard

1

u/[deleted] Apr 26 '24

add(2, 4) s a function call

it is a print statement

1

u/carcigenicate Apr 26 '24

Not sure what you mean. In Python 3 at least there's not really any such thing as a "print statement" (that's from Python 2).

print(add(2, 4))

Is basically the same thing as

ret = add(2, 4)  # Function call. add is called with 2 and 4
print(ret)  # Function call. print is called with 8