Executing a Function
You may have seen functions like
round() already. These are called built-in functions because they are built into Python, and you can use them in your Python programs right away without adding any additional code. So they are perfect to investigate what functions actually are.
Let’s hop over to IDLE. First, I’ll show you how to use
type(), which is also a built-in function, to inspect the string and an integer. Then we’ll have a closer look at the
You can check the type of a value by typing
type and then the variable or the value of a variable in parentheses. So here I type
So you can see that the integer
3 has the type
int (integer) and the string
"hello" has the type
str. Just like integer values and string values, function values also have a type.
So if I type
type(print), it tells me that
So if I type
type() at the top of this terminal.
So if I type
built-in function print, but an empty line. Okay, we’ll come back to this in a bit.
Let’s first try out the same with the
len, opening parenthesis, and closing parenthesis. And if I run this, I get an error.
The error message that Python shows me says
TypeError: len() takes exactly
one argument, but
given. An argument is a value that gets passed to the function as input. Some functions can be called with no arguments, so like we saw with the
print() function, and some can take as many arguments as you like.
len() requires exactly one argument.
So let’s make
len() happy and let’s make me happy by using the word
"pancake". So when I type
len, opening parenthesis, and then put in the string
then I actually get the value
7 back. That’s cool. That works. So what happened there? We call the
len() function with the string
"pancake" as an argument.
Then Python executes the function and calculates the length of the string. The length of
"pancake" is the number
len() returns the number
03:30 The process for executing a function can be summarized in three steps. First, the function is called, and any arguments are passed to the function as input.
03:39 The second step is the function executes, and some action is performed with the arguments. Third, the function returns, and the original function call is replaced with the return value.
03:53 The last step is interesting. A function returns a value. That means you can assign this value to a variable. Let’s try this out.
Okay. Now I create the variable
num_letters and say
len("pancake"). if I run it, then nothing happens. But if I type
num_letters, you see that
num_letters now contains the number
So again, first
len() is called with the argument
"pancake". The length of the string
"pancake" is calculated, which is the number
len() returns the number
7 and replaces the function call with the value. You can imagine that after the function executes, the line of code looks like this.
So Python assigns the value
num_letters and waits for your next input. And while Python waits for our next input, let’s dive in a bit deeper into the topic.
05:07 You’ve learned how to call functions, and you learned that they return a value when they’re done executing. Because I like emoji just as much as I like pancakes, I created this little illustration.
05:21 The waving hand stands for the function call. Then the cogwheel represents that the function is doing something. Finally, the function returned something for you. For this, I used an envelope because envelopes usually contain something of value.
05:38 See what I did there? Sometimes, though, functions do more than just return a value. When a function changes or affects something external to itself, it is said to have a side effect.
05:51 Let’s investigate what that means.
A famous function that has a side effect is the
print() function. When you call
print() with a string as an argument, the string is displayed in the Python shell as text.
This looks like a return value, but it’s actually a side effect. When you call
print(), the text that gets displayed is not a return value.
print() function itself doesn’t return the
"Hello" string. But what does
print() return then? Well, we already learned that we can assign the return of a function to a variable.
So let’s try and say
print("Hello"). When we run it, then
Hello gets displayed in the Python shell again, but now we can check what
return_value is … and nothing is shown.
print() returns nothing?
Didn’t we learn that all functions return something? Well, you are right. Actually,
print() returns a special value called
None indicates the absence of data. Let’s check the type of
None type. So it’s true: all functions in Python return a value, even if that value is
None. I know what you’re thinking.
Wouldn’t it be cool to have a
print() function that also returns the text value? Say no more. In the next lesson, you will learn how to create your own function that can actually return something.
Become a Member to join the conversation.