4.8

## Raspberry Pi Foundation

Skip to 0 minutes and 3 secondsYou have already used some functions in Python, such as print and input. While these functions are in-built and you can't see the code, they were created in a very similar way to the programs that you have written. Now, in this step, you're going to have a go at creating your own functions. You may have functions also referred to as subroutines or procedures. And in other programming languages, those things are different. However, in Python, they're just all known as functions. Now, the syntax to create a function in Python looks like this. Now, the def stands for definition. And what you're doing when you're creating a function is defining it and giving it a name.

Skip to 0 minutes and 43 secondsNow, between the def and the brackets is the name of your function. Now, indented after the colon is the code that you want to run as part of this function. When you want to use or call a function, you use the name with round brackets, my_function, open and close a bracket. Now, like variables, functions can be given any name you wish. But it's a good idea to name your functions after the job that they do. So let's put this into practice. Now, let's create a new program and save it as Functions.

Skip to 1 minute and 23 secondsNow, we're going to use def to create an intro function, which will display a message at the start of our program. So indented under the function, let's add some code to print a message. So we use print("lets do some coding").

Skip to 1 minute and 43 secondsNow, to call a function from your program, we need to use its name, intro, and we need to open and close a bracket. Now, note that this line of code is not indented under the def, as it doesn't belong to the function. So let's run our program. And you should see the message appear. Excellent, lets do some coding. Right, now let's change our program so that it runs that function twice. You can do this by adding a second function call at the bottom of your program. And again, if I run the program again, you'll see that the message is printed twice. Now, using functions in this way allows you to reuse your code, making it easier to understand and change.

Skip to 2 minutes and 28 secondsHave a go changing your function so that it perhaps prints out a second message or asks the user for some input.

# Functions

You have already used some functions in Python, such as print and input. While they are “in-built” and you can’t see their code, they were created in a very similar way to the programs you have written. In this next section you will create your own functions.

Note: This course has a simple introduction to functions; you can learn more about functions, including how to pass and return variables, in Programming 102: Think Like a Computer Scientist.

You may hear functions referred to as subroutines or procedures. In some other programming languages, subroutines and functions are different things, but in Python they are all just known as functions.

The syntax to create a function in Python looks like this:

def my_function():
print("do this")


def stands for “definition”, since what you are doing when creating a function is defining it and giving it a name.

Between the def and the () is the name of your function followed by :. Indented under the : is the code which should be run as part of the function.

When you want to use (or call) a function you use the name with parentheses:

my_function()


Tip: Like variables, functions can be given any name you wish, but it is a good idea to name your functions after the job they do.

Let’s explore how to use functions in Python.

• Create a new program called functions.

• Use def to add a function at the top of your program called intro.

def intro():

• Indented under this line, add some code to print a message.
    print("lets do some coding.")

• Call your intro function from your program.

Note: this line of code is not indented under the def as it doesn’t belong to the function definition.

intro()


Your program should now look similar to this:

def intro():
print("lets do some coding.")

intro()

• Change your code so the function is run twice, by adding a second call to intro at the bottom of your program.
intro()