Dina Samir1,990 Points
Calling a Function Question
I am little confused about when does calling a function work with and without print.
This first code:
def add_two_nums(num1, num2): val = num1 + num2 return val add_two_nums(5, 10)
Doesn't call the function.
That second code:
def add_two_nums(num1, num2): val = num1 + num2 return val print(add_two_nums(5, 10))
calls the function.
Can someone help me with this?
The first code calls the function, but sort of like behind the scenes, try calling the function, like the first code, in the REPL.
>>> add_two_numbers(5, 10) 15
Try this experiment in script not REPL.
def add_two_nums(num1, num2): val = num1 + num2 return val result = add_two_nums(5, 10) if result == 15: print("Auuuright")
Msg me here again if you're having trouble understanding.
Alright, first let us understand what is this
print() is a function, now you might ask what is a function? A function is a block of written code used to perform an action (Apparently), some examples of a function, let's suppose I want a code that prints out any given name to the screen or a function that would print each letter of a given work onto the screen.
Code that prints my name on the screen
def any_given_name(parameter): print(parameter) >>> any_given_name("boi") boi
So basically this is called a function, the
print() is also a function, it's used to print out any value or object. So now coming back to your question, you asked "when does calling a function work with and without print.", calling a function works regardless of using
print() tells the function to PRINT the result on the screen, it has nothing to do with making the function work. let's see an example 👇
def add(number1, number2):👈 #I've created a function that adds two numbers return number1 + number2👈# I then told the function to return the result not print it stored_value = add(2, 1)👈#Over here I've stored the returned value from the add function into a variable. print(stored_value)👈#Now I've used the print function to PRINT the result
This example is to show that the function
add() will work regardless of using
print(). Did this clarify? If not msg me again