Function Calls and Definitions
00:00 Now let’s take a look at function calls and definitions.
00:05 To call a function—to use a function, or invoke, are other terms that we use—you simply give the name of the function and then, followed in parentheses, argument values—if any—that are needed.
Even if the function doesn’t take any arguments, you still have to have the parentheses. You’ve probably seen this before, using built-in functions or functions from modules you’ve imported. For example,
print("Hello!") is a function call.
We use the word
print() function is expecting.
00:43 Now let’s take a look at a function definition.
This is the syntax for defining a function. It begins with the keyword
def. This tells Python that you are about to define a function. Next is the function name, and we don’t use these angle brackets.
01:04 These are just syntax notation to indicate that you would replace the function name in this description with an actual name. The name of the function should describe what it’s supposed to do so that someone using your function knows what to expect when they use it.
Following the function name are parameters. These are the identifiers that will hold onto argument values that the function is given. If the function doesn’t take any parameters, you would still use empty parentheses. And if you have multiple parameters, they would be separated by a comma (
We will see a lot more about the use of parameters in the next few lessons. Then, the header line is concluded with a colon (
:). What follows is one or more statements, indented, which carry out the task that this function is supposed to perform. Your really useful piece of code that you wrote?
02:04 Here’s where those instructions would be written down. And again, you do want to indent them. That’s how Python knows the code that you’re typing is part of that function definition.
So, here’s a real simple example of a Python file that contains a function and uses that function. I’ve called this file
function_call.py, just to show us what a typical function definition and function call would look like.
We begin with the word
def, followed by the name of the function,
f, a nice arbitrary name for an arbitrary function. This takes no parameters, so we have an empty set of parentheses, and then the colon to indicate that this is the conclusion of the header line for this function, and what follows is going to be the statements we want the function to execute.
02:56 The statements are indented, and that’s what tells Python that they are part of this function definition. In this particular case, we’re going to do two things.
We’re going to define a variable
s, which is going to contain a string, and then we’re going to print the value of that string variable. Here, we just have a
print() statement to indicate that we are currently inside the running of this function.
03:21 Line 5 is not indented, and so this tells Python that this is not part of the function.
And so if I were to run this Python file, we would first have our function definition, but these statements wouldn’t be executed until we actually have a reference to call that function. This Python program will start running at line 5, where we have a
print() statement to say where we are in the execution of this program, and we’re in the part before we’ve called our function.
Then we’re going to call our function, and what happens is Python is going to pause the running of these three lines, come up here, and execute the statements of the function we just wrote. In this case, it’s going to display an output statement saying that we are currently in the process of executing this function. When this function is finished, program execution returns to where the function was called, which was line 6. We finished line 6, and so we’re going to see the
print() statement in line 7 will be executed last. Let’s take a look at that.
So, let’s just run this. This was
04:42 and the behavior is going to be exactly what we expected.
The first line that’s executed isn’t within the function, but it’s the first statement outside of the function, which was before calling
f(). Then, inside the function, the output statement occurred, and then the function finishes execution and we returned to line 7 and had that output statement.
05:08 Let’s see if I can put these side by side so you can see that a little bit better. There. Line 5 executed, line 6 called our function. We defined this variable, we printed out its value.
Then program execution returned to line 7, and we got the
After calling f() output statement.
05:34 Sometimes in program design, you know that you need a function in a particular place and you want to make sure the function is called correctly and that program execution gets returned to after it’s called, but you’re not actually ready to write the body of that particular function.
We call that a function stub. It’s a placeholder function. We know we need a function
f(). We don’t know what we want it to do exactly just yet but we need to have it defined as a name so that other parts of the program, we can check and make sure are working. To create a function stub in Python, you simply give the statement
pass as the body of the function.
06:16 That simply indicates we’re not going to do anything, but it allows us to write a function stub with a function body of some type.
06:25 The next series of lessons are going to be on argument passing. Python has many, many ways to provide arguments to a function when you call it, and the next series of lessons will take a look at many of those methods for passing arguments to a function.
Become a Member to join the conversation.