5.1. Declaring functions#
Functions in Python are declared using the def
keyword.
def function():
commands to be executed
Where function
is the name which is used be the program to refer to the function. A function name must begin with a character or underscore and not a number. Also care must be taken not to use the same name as a previously defined function (e.g., if you define a new function called max
then you won’t be able to use Python’s built-in max
function).
Note that the commands within the function are indented. The commands following a function that are not indented are not part of the function so functions are ended with the next non-indented command (similar to loops and if statements).
Create a new Python file called 5_Functions.py and save it to your OneDrive folder. Lets define a function that prints hello world
to the console. Enter the following code into your file.
# Functions
# Declaring a function
def hello():
print("Hello world")
hello()
Here we have declared a function called hello
that has no input argument (there is nothing between the brackets). The function prints the hello world
string. Now that we have defined our function we can now call the function to use it. Add the following code beneath where you have declared the hello()
function.
hello()
Run your program and you should see the following added to the console.
Hello world
Important
Function declarations can appear anywhere within a Python program but must appear above any calls to the function.
5.1.1. Local and global variables#
Any variables defined within a function are known as local variables because they can only be accessed from commands within the function. It can be useful to think of a function as a black box in that any variables which are defined within a function cannot be used outside of the function unless they have been outputted using the return
command.
Variables that have been defined prior to the a function being called are known as global variables and can be access within a function, i.e., all variables defined outside of a function are global variables by default.
To demonstrate the difference between local and global variables enter the following code into your program.
# Local and global variables
def print_name():
last_name = "Williams"
print(f"Name: {first_name} {last_name}")
print()
first_name = "Ellie"
print_name()
Run your program and you should see the following added to the console.
Name: Ellie Williams
Here the variable first_name
is declared outside of the function and the variable last_name
is declared inside the function. Both variables can be access by commands inside of the function as evidenced by the print()
command. Lets try to access last_name
outside of the function. To demonstrate this, enter the following code into your program.
print(last_name)
If you try to run your program you will see the following error message printed to the console.
NameError: name 'last_name' is not defined
Python does not know what last_name
is because it was declared inside of a function and is therefore a local variable. Comment out or delete this line so that your program runs ok.
5.1.2. Exercise#
Create a new Python file called 5_Functions_exercises.py and save it to your OneDrive folder. Use it to answer the following exercises.
Write a function called loving_it()
that outputs the following message to the console
I'm writing Python programs and I'm loving it.