Functions in Python | ICT | Grade 9

💻

Functions in Python

ICT | Grade 9 | Language Schools

Unit 1: Artificial Intelligence and Programming Languages

Lesson 4

Introduction

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.

Key Idea: Functions help us organize programs, reduce repeated code, and make programs easier to understand and maintain.

🎥 Lesson Video

Watch the lesson first, then continue with the explanation and examples below.

Learning Objectives

1. Understand Functions

Explain what a function is and why programmers use functions.

2. Create Functions

Use the def keyword to create a function.

3. Use Parameters

Pass information to a function using parameters.

4. Use Return

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.

Simple example:
def welcome(): print("Welcome to ICT!") welcome()

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.

def function_name(): # statements

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.

def show_message(): print("Learning Python is fun!") show_message()

When show_message() is called, the message is displayed.

Remember: The function definition creates the function. The function call makes the function run.

4. Functions with Parameters

A function can receive information from outside. This information is passed through parameters.

def greet(name): print("Hello", name) greet("Ahmed") greet("Mona")

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.

def student_info(name, age): print("Name:", name) print("Age:", age) student_info("Ali", 14)

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.

def square(number): return number * number result = square(5) print(result)

The function calculates 5 × 5 and returns 25.

Important: 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.

def add_numbers(a, b): return a + b answer = add_numbers(10, 5) print(answer)

The output is:

15

8. Calling a Function

To call a function, write its name followed by parentheses. If the function has parameters, provide the required arguments.

def say_hello(): print("Hello!") say_hello()

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.

def total_price(price1, price2): return price1 + price2 total = total_price(50, 30) print("Total:", total)

Output:

Total: 80

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.

  1. Understand the problem.
  2. Break the problem into smaller tasks.
  3. Decide what each function should do.
  4. Create the functions.
  5. Call the functions in the correct order.
  6. Test the program and fix errors.

12. Common Mistakes

Mistake 1: Forgetting the colon

def welcome() print("Hello")

Correct:

def welcome(): print("Hello")

Mistake 2: Incorrect indentation

def welcome(): print("Hello")

Correct:

def welcome(): print("Hello")

Mistake 3: Forgetting to call the function

def welcome(): print("Hello")

The function has been defined, but it has not been called.

welcome()

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.

1. Which keyword is used to define a function in Python?

2. What does a function help programmers do?

3. Which symbol follows a function definition?

4. What is a parameter?

5. Which statement is used to send a value back?

6. Which code correctly defines a function?

7. What is used to run a function?

8. What is the output?

def add(a, b): return a + b print(add(2, 3))

9. Which is a correct function call?

10. What is one major advantage of functions?

No comments:

Post a Comment

التصنيفات

```html ```html ```