ZEQMA

๐Ÿ”ฐ Introduction :

The current image has no alternative text. The file name is: IMG_20250724_023331_edit_117107991644107-scaled.jpg

As your program grows, you’ll start to notice a lot of duplication. Instead of repeating the same code over and over, it’s better to compile it into a function that you can call when needed.

โœ… What is a Function ?

A function is a piece of code that performs a specific task and can be called multiple times.

Think of it like a “mini-program” within your program.

๐Ÿ“ฆ Creating a Simple Function :

โœ… General Syntax :

python

def function_name():

# Function Code

๐Ÿงช Example :

python

def say_hello():

print(“Welcome to the world of programming!”)

To run it:

python

say_hello()

๐Ÿ” Functions with Parameters

You can send information to the function for it to operate on.

python

def greet(name):

print(“Hello, ” + name + “!)

greet(“Ali”)

greet(“Sara”)

๐Ÿ“Œ “Ali” and “Sara” are values sent to the function as parameters.

๐Ÿ”™ Using return to return a result :

Python

def square(number):

return number * number

result = square(4)

print(“Result:”, result) # Result: 16

๐Ÿ“Œ return returns a result that can be stored or used later.

๐Ÿงฎ Practical Example :

A Function to Calculate the Area of a Rectangle:

Python

def area(width, height):

return width * height

w = float(input(“Enter width: “))

h = float(input(“Enter height: “))

print(“Area:”, area(w, h))

๐Ÿ” Multiple Functions in One Program :

Python

def welcome():

print(“Welcome!”)

def add(x, y):

return x + y

welcome()

result = add(5, 3)

print(“5 + 3 =”, result)

โš ๏ธ Notes for Beginners:

It is preferable to give functions clear names that express their function.

Don’t forget to include parentheses () even when there are no parameters.

The code inside the function must be indented.

๐Ÿง  Exercise 1 :

Write a function that adds two numbers:

python

def sum_two(a, b):

return a + b

๐Ÿง  Exercise 2:

Write a function that checks if a number is even:

python

def is_even(n):

return n % 2 == 0

And use it like this:

python

number = int(input(“Enter a number: “))

if is_even(number):

print(“The number is even.”)

else:

print(“The number is odd.”)

๐Ÿ“ Conclusion :

Functions help you organize your code.

You can pass parameters and return results using return.

The more organized your code is, the more maintainable and extensible it is.

๐Ÿ“Œ What will we learn in Lesson 6 ?

In the sixth and final lesson of this series, we will create a simple software project: an interactive calculator using everything you have learned about variables, conditions, loops, and functions

โฌ‡๏ธ Download ZEQMA ุชุญู…ูŠู„

Related Posts

Rapid_tv

๐Ÿ”น Introduction The Rock, Paper, Scissors Game is a classic hand game that you can easily build in Python. ๐Ÿ‘‰ Rules: ๐Ÿ”น Code Example ๐Ÿ”น Example Run ๐Ÿ”น Concepts Learned…

Read more

VOD-ZalHD

๐Ÿ”น Introduction The Number Guessing Game is a classic beginner Python project. ๐Ÿ”น Code Example ๐Ÿ”น Example Run ๐Ÿ”น Concepts Learned ๐Ÿ”น Summary โฌ‡๏ธ Download VOD-ZalHD ุชุญู…ูŠู„ Server 1 โฌ‡๏ธ…

Read more

Delux_pro

๐Ÿ”น Introduction The To-Do List App is one of the most popular beginner projects. It teaches you how to: ๐Ÿ”น Code Example ๐Ÿ”น Features of This App ๐Ÿ”น Example Run…

Read more

Fam4k

๐Ÿ”ฐ Project Overview A calculator is one of the simplest yet most effective projects to start with in Python.You will learn: โœ๏ธ Step 1: Plan the Calculator We need: ๐Ÿ–ฅ๏ธ…

Read more

Speed_HD+

1. Introduction Data visualization helps us understand patterns and insights in data by converting raw numbers into charts and graphs.In Python, the most popular libraries are: 2. Installing Required Libraries…

Read more

Dauo

1. Introduction Web scraping is the process of extracting data from websites. In Python, we commonly use libraries like requests (to fetch web pages) and BeautifulSoup (to parse and extract…

Read more