Function definitionsSlide solved ✓
In this lesson you will learn how to create your own functions.
Function definitions are a crucial tool for you to master. They are the basis of all large programs.
Function definition example
Here is an example of creating our own function:
def f(x): return x+2
The code creates a function with the name
If you call
f with an argument, Python will run the code inside the
function, which adds
2 to the argument and outputs the result.
The code above doesn't do any calculations on its own. It only defines the function. Before anything happens, you must call the function.
Try defining a function
The code below creates the function
f. It then calls
f with an argument of
1 and prints the result.
Run the code to continue. What do you predict will happen?
When you make your own function, programmers say that you "define" the function. Here are four examples of function definitions:
def g(y): return y*5 def greet(name): print("hello " + name) def my_func(a, b, c): return a + b + c def square(number): result = number * number return result
The name of a function
In this example, the function
f is defined and later used:
def f(x): return x+2 print(f(1))
The "def" keyword tells Python we're defining a function. Then comes the
name of the function,
Anything that is indented (moved four spaces in) is part of the function definition.
To continue, change the name of the function in all places from
Then run the code.
After the function name are the function parameters. These are the inputs to the function. If there are multiple parameters they are separated by commas.
Change the function definition below so the parameters are named
After the name and parameters of a function is the function body. This is the code that does all the work of a function.
In the code below we define a function named
square and then call it.
result = number * number and
return result are part of the
function definition because they are indented: they are moved in by four
Then the function body ends! The line
print(square(3)) is not part of the
function body, because it is not indented.
Call the function
Below is the function
square again, but we have removed the call to the
To continue, add to the code. Try to call the
square function with
as an argument, and print the result.
The return keyword
The example functions you have seen so far used the
When Python runs a line of code that starts with
return, then Python will
stop running code inside the current function.
Python will then return to the place in the code where the function was called.
If a value was returned, such as in
return 123, then the value
123 is the
result of the function call.
When thinking about functions as a box that you put some values into and a value comes out, the returned value is what comes out of the box.
A return example
It is important to understand exactly what happens when Python runs your code.
Take a look at the code below. Here is what happens when it is run:
- Python goes over each line in turn
- the first three lines define the function named
- this only creates the function; no code is actually run yet
- nothing is printed nor returned yet
- the next line calls the
- then the lines inside the
ffunction are run one at a time
- then the lines inside the
- when the line
return 123is reached, Python jumps back out of the function, sending the value
123out as the result of the call
- the returned value
123is stored in the
- the value of
Note that the line
print("never reached") is not run, because Python returns
out of the function when
return 123 is hit.
Run the code to continue. Make sure you understand why the text
reached" is not printed.
Change the return example
Change the code below. You should change the order of the lines of code in the
f function body so that
123 is returned.
More notes on return
return keyword only makes sense inside a function body. If you are not
defining a function, using
return will give an error.
return are completely different concepts.
return will stop the execution of a function, sending a
value out to the point where the function was called. If you forget to
What if you forget to
return a value? Then the function will implicitly
return the special value
For example, the function
say_hi below only prints text; it has no
Try to add a call to
say_hi() to the function below. You should print the
output of the function.
You should see that
None gets printed to the screen (after
Below is an example of defining a larger function.
The function is named
smallest_length. It takes two strings as parameters
and returns the length of the smallest of the strings.
In more detail,
smallest_length takes two parameters,
which are strings. It computes the length of these strings. Then it uses
to find the smallest of the two lengths, returning it.
In the example we call
smallest_length with two strings,
"BB", and print the result. This call is placed after the end of the
Try calling smallest_length
Below is the definition of
smallest_length again. We have left out the
function call, however.
To continue you must add a call to
smallest_length with the two arguments
"test". The call returns a value. Print this returned value.
Defining another function
Functions can do more than just calculate numbers. You can put any code you want inside a function -- even a print statement. Here is another example:
def greet(name): text = "Hello " + name print(text + " how are you?")
To continue, call the
greet function with the string
"Bob" as an argument.
Why define functions?
When you need to solve a large problem with Python, it helps to break it up into small pieces. If you can solve each piece in turn, you can solve the large problem too.
Function definitions let you do this. You put the code for each small piece into its own function. When you have written the functions you need, you call them all in turn. And suddenly, your large problem is solved!
Define a function from scratch
Here's an example for you to base your code on:
def g(a): return a*5
To continue, define a function named
f that takes the parameter
x by 3, and returns the result.
After defining the function, call it with an argument of
2. Then print the
Another use for functions
Function definitions are useful to avoid writing the same code many times.
If you find yourself repeating the same chunk of code a lot, you can instead move that code into a function, and just call the function several times.
This makes your code easier to read and understand, and when you have to change your code, you'll only have to do it in one place.