Function Calls and Definitions
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.
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?
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.
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.
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.
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: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: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.