By the end of the three parts of this lesson, you will be able to:
Define and call functions using def
Use parameters to send data into functions
Use return to send data out of functions
Understand the difference between creating and calling a function
Weโll start by learning how to define a function and how to run it in your code. This is called creating and calling a function. Think of it like writing your own command that the computer can understand and use again and again.
A function is a reusable block of code. Youโve already used built-in ones like print(). Now youโll make your own. When you create (define) a function, you must always use the def keyword, followed by the name of the function, followed by brackets and a colon. As you already know from writing if / for / while etc, when you use a colon all following lines of code are indented!
๐ Function = Reusable code block that performs a task
๐ Function call = Tells the computer to run the function
Example
def greet():
ย ย ย ย print ("Hello, welcome to Python!")
greet()ย
# greet() calls the function and makes it run. Notice that we use brackets but that they are empty.
๐ Practice Task 1
Write a function called welcome() that prints the message "Welcome to our program!".
Call the function twice.
Next, weโll learn how to make functions more flexible by adding parameters. Parameters let you send information into a function so it can do different things depending on the input. If a function is expecting a parameter then you must send it one, otherwise you will get an error. Multiple parameters can also be sent, separating each one with a comma, for example: myfunction(parameter1, parameter2).
Example
def greet_user(name):
ย ย ย ย return "Hello, " + name + "!"
message = greet_user("Alice")
print(message)
๐ Try changing "Alice" to another name. What happens?
๐ Add a second parameter and test what happens.
๐ Practice Task 1
Write a function called personal_greeting(name) that returns "Hi, [name]! Great to see you."
Call it with your own name.
๐ Practice Task 2
Write a function called favourite_colour(name, colour) that returns "Hi [name], your favourite colour is [colour]!".
Test it with your own name and favourite colour. Ask someone else to use your program. When they write their name and colour, your program should show their answers.
Finally, weโll explore how to use the return keyword. This allows a function to send back a result, which you can save in a variable and use elsewhere in your program. Itโs a powerful way to make your functions more useful.
Example
def create_greeting(name):
ย ย ย ย return f"Hello, {name}! Hope you're having a great day."
message = create_greeting("Bob")
print(message)
๐ Use return to send info back
๐ Store the returned value in a variable and use it later
๐ Common Mistakes
โ Defining a function but not calling it
โ Printing inside the function instead of using return
โ Forgetting the colon : after def
โ Forgetting brackets () when calling a function
๐ Practice Task 1
Write a function add_numbers(a, b) that returns the sum of the two numbers. Call it and print the result.
๐ Practice Task 2
Write a function double_value(x) that returns x * 2. Call it with a number and print the result.
Now that you've learned how to create and call functions, use parameters, and return values, itโs time to put everything together. These Red Tasks are fun challenges that help you practise all the skills from this lesson. Start with the basic challenge and work your way up to the advanced one. Try them on your own first, then test and improve your code.
๐ฏ Basic Challenge
Write a function called welcome() that returns "Welcome to our program!"
Call the function and print the result.
๐ฏ Extended Challenge
Write a function called personal_greeting(name) that takes one parameter and returns a message like: "Hi, Ana! Great to see you."
Call it with your name and print the result.
๐ฏ Advanced Challenge
Write a function called add_numbers(a, b) that takes two numbers and returns their sum.
Call it with two numbers and print the result.
Write the example from this lesson on paper, making sure to include:
โ
The def keyword
โ A function name and ()
โ A parameter
โ A return statement
โ A function call
โ
A print() to show the result
Read the example code carefully before writing!