Can I call a function inside of a print function?

current_year = 2048

def calculate_age(birth_year):
  age = current_year - birth_year
  return age

print(current_year)

print(calculate_age(1970))

Does print(calculate_age(1970)) call the function? Or is the function not called in this example? I’m confused.

1 Like

Yes, it does. The return value is what will print.

6 Likes

yes print(calculate_age(1970)) will call the function and since in the function calculate_age(1970) we asked it to return age hence the statement print(calculate_age(1970)) will print the age of the person (ie78)

3 Likes

Yes but as a “call” they aren’t asking for a call in the context of a print function, they want you to “call” the defined parent function “calculate_age()”…

As noted above, print(calculate_age(1970)) absolutely does involve a function call.

I don’t see where calling the function is asked for in this exercise, but if you were to call the function without reference to any print() expression or assignment to a variable, the function would certainly run; its returned value would be “orphaned” (assigned to no variable or function call, thus eligible for garbage collection), and the function and its variables would disappear from the call stack.

In other words, from our standpoint as outside observers, nothing would happen (except that you might see “Run calculate_age.py” on your shell.)

5 Likes

Got the correction thank you and the call is simply in the function print as to display the function on screen. Understood.

Or better… in the console.

thx , I was searching for an answer , so thr return is what did the trick?

As the above example shows, you can call a function in Python using Print.

Python allows different ways to accomplish the same task, and beginners may be confused by using print to call a function.

We might be inclined to call and then print the calculated result from the function like this:

current_year = 2048

def calculate_age(birth_year):
  age = current_year - birth_year
  return age

print(current_year)
current_age=(calculate_age(1970))
print(current_age)

The results are the same. But either way, you would still need the Return to get the calculated result out of the function and assign to the variable: “current_age”

Yet another option is to Print the calculated result from inside the function. In that case Print would replace Return. Hope that helps.

4 Likes

Yeah thx, actually that is the way I imagined it first. I know now how both work and that print can actually call a function

Print does not call the function, it prints the return value. The call is made in the argument to the print function.

Any function may take a function as its argument.

7 Likes

The only thing I thought was odd in this example was that we did not need to convert the number to a string. Originally, when I wrote out the solution to print the result, I wrote:

print(str(calculate_age(1970)))

Then I tried it without the str() function and it worked just fine as well. Why is that?

I’m assuming that since the number is wrapped in a print() function that it automatically knows you want to print it and so it converts it to a string on the fly. Maybe that is built into Python’s built-in print() function?

Python’s print() function is a ream of code, under the hood. The net effect is to stream character data to the standard output, in this case the console. str() creates a string representation in much the same way as print() creates the stream, which means overlapping purposes.

The only need for casting to str is if we are concatenating with another str or carrying out a str method. print() is a built-in that accepts all types.


That last sentence kind of over generalizes and suggests that print will interpret every argument, which is not the case at all. My bad.

>>> print (range(10))
range(0, 10)
>>> print (map(lambda x: x ** 2, range(10)))
<map object at 0x0000028E8E30EFD0>
>>> print (filter(lambda x: x % 2, range(10)))
<filter object at 0x0000028E8E30EFD0>
>>> print (enumerate(range(10)))
<enumerate object at 0x0000028E8E2D4640>
>>> 

The only thing printed was the stream of the slug descriptor. There is an iterator sitting and waiting at some address is all that this is telling us. So, yes, we do need to recast some types to a readable or iterable type to get them to reveal their contents. Iterators that give up their contents are consumed so printing them will destroy them (empty them of their references). range() is an exception to this rule as it is not a true iterator.

1 Like