Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Executing a Function

00:00 You may have seen functions like print(), len(), and 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.

00:18 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 print(), len(), and round() functions.

00:32 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 type(3) and

00:49 type("hello").

00:52 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.

01:08 So if I type type(print), it tells me that print is a built-in function or method. You can get a similar output in IDLE when you just type the name of a function.

01:24 So if I type len or round or print, Python tells me that len, round, and print are built-in functions. But if you actually want to execute a function, you must call the function. In Python, You use two parentheses to call a function, just like you saw with type() at the top of this terminal.

01:50 So if I type print followed by an opening parenthesis and a closing parenthesis, then something happens. It’s not outputting built-in function print, but an empty line. Okay, we’ll come back to this in a bit.

02:11 Let’s first try out the same with the len() function. len, opening parenthesis, and closing parenthesis. And if I run this, I get an error.

02:24 The error message that Python shows me says TypeError: len() takes exactly one argument, but 0 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.

02:50 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 "pancake",

03:05 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.

03:16 Then Python executes the function and calculates the length of the string. The length of "pancake" is the number 7. Finally, len() returns the number 7.

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.

04:04 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 7.

04:25 So again, first len() is called with the argument "pancake". The length of the string "pancake" is calculated, which is the number 7.

04:34 And then 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.

04:47 num_letters = 7.

04:53 So Python assigns the value 7 to 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.

05:57 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.

06:11 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.

06:21 The 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.

06:34 So let’s try and say return_value = 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?

06:57 Didn’t we learn that all functions return something? Well, you are right. Actually, print() returns a special value called None.

07:06 None indicates the absence of data. Let’s check the type of return_value.

07:16 Aha, it’s None type. So it’s true: all functions and Python return a value, even if that value is None. I know what you’re thinking.

07:26 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.