Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

boi
boi
14,242 Points

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?

boi
boi
14,242 Points

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!!