Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Functions, Packing, and Unpacking Getting Info In and Out of Functions Multiple Arguments and Parameters

Calling a Function Question

Hi,

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?

2 Answers

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.

Sorry boi I am still new to Python I didn't quite understand the difference, can you rephrase it in a simpler way?

Alright, first let us understand what is this print(). 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(), 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

Now I got it!! I can’t thank you enough!!