Functions in Python
ICT | Grade 9 | Language Schools
Unit 1: Artificial Intelligence and Programming Languages
Lesson 4Introduction
In programming, we often need to perform the same task more than once. Instead of writing the same code repeatedly, we can organize the code inside a function.
A function is a reusable block of code that performs a specific task. The function runs when we call it.
🎥 Lesson Video
Watch the lesson first, then continue with the explanation and examples below.
Learning Objectives
Explain what a function is and why programmers use functions.
Use the def keyword to create a function.
Pass information to a function using parameters.
Return a result from a function using the return statement.
1. What Is a Function?
A function is a block of code designed to perform a particular task.
The code inside a function does not normally run until the function is called.
The function is called welcome().
When we call it, Python executes the code inside the function.
2. The def Keyword
Python uses the keyword def to define a function.
There are several important parts:
| Part | Meaning |
|---|---|
def |
Keyword used to define a function. |
| function_name | The name we give to the function. |
() |
Parentheses used for parameters. |
: |
Marks the beginning of the function block. |
| Indented code | The instructions executed by the function. |
3. Function Without Parameters
A function does not always need parameters. We can create a simple function that performs a task without receiving any information.
When show_message() is called, the message is displayed.
4. Functions with Parameters
A function can receive information from outside. This information is passed through parameters.
Here, name is the parameter.
The values "Ahmed" and "Mona" are arguments
passed when the function is called.
| Term | Meaning |
|---|---|
| Parameter | A variable written in the function definition. |
| Argument | The actual value passed to the function. |
5. Functions with More Than One Parameter
A function can have more than one parameter.
The function receives two pieces of information:
name and age.
6. The return Statement
A function can calculate a value and send that value back to the part of the program that called the function.
We use the return statement to return a result.
The function calculates 5 × 5 and returns
25.
return sends a value back from the function.
It is different from simply displaying a value using
print().
7. Example: Add Two Numbers
We can create a function that receives two numbers and returns their sum.
The output is:
8. Calling a Function
To call a function, write its name followed by parentheses. If the function has parameters, provide the required arguments.
The statement say_hello() is the function call.
9. Practical Example
Let's create a simple function that calculates the total price of two products.
Output:
This example shows how a function can receive data, process it, and return a result.
10. Why Do We Use Functions?
- They reduce repeated code.
- They make programs easier to understand.
- They organize code into smaller tasks.
- They make code easier to test.
- They allow code to be reused.
- They make large programs easier to maintain.
11. Functions and Problem Solving
Functions are especially useful when a problem can be divided into smaller tasks.
- Understand the problem.
- Break the problem into smaller tasks.
- Decide what each function should do.
- Create the functions.
- Call the functions in the correct order.
- Test the program and fix errors.
12. Common Mistakes
Mistake 1: Forgetting the colon
Correct:
Mistake 2: Incorrect indentation
Correct:
Mistake 3: Forgetting to call the function
The function has been defined, but it has not been called.
Quick Review
- Function: A reusable block of code that performs a specific task.
- def: Keyword used to define a function.
- Parameter: A variable that receives information inside a function.
- Argument: The actual value passed to a function.
- Calling: Running a function by using its name.
- return: Sends a value back from a function.
📝 Interactive Quiz
Test your understanding of Functions in Python. Choose one answer for each question, then click Submit Quiz.
No comments:
Post a Comment