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

FivG_Pro

๐Ÿ”ฐ What Is a Function? A function is a reusable block of code that performs a specific task.Instead of repeating code, we can write it once and call it whenever…

Read more

Show7_Pro

๐ŸŸข Lesson 6 โ€“ Intermediate Level ๐Ÿ”ฐ What Is a Module? A module is simply a Python file (.py) containing code (functions, variables, classes) you can reuse in other programs….

Read more

GoldiptvPro

๐ŸŸข Lesson 5 โ€“ Intermediate Level ๐Ÿ”ฐ What Are Exceptions? An exception is an error that occurs during the execution of a program.If not handled, it will stop the program…

Read more

Domingo

๐ŸŸข Lesson 4 ๐Ÿ”ฐ What Is OOP? Object-Oriented Programming (OOP) is a programming style where you organize code into objects that contain: Python supports OOP using: ๐Ÿงฑ Example: Why Use…

Read more

SHARK_PRO

๐Ÿ”ฐ What Is File Handling? ๐ŸŸข Lesson 3 File handling lets your Python programs interact with files on your computer โ€” read data from them or save data to them….

Read more

ZinaTv

๐ŸŸข Lesson 2 โ€“ Intermediate Level ๐Ÿ”น Python Dictionaries: Storing and Accessing Key-Value Data ๐Ÿ”ฐ What Is a Dictionary? A dictionary in Python is a data structure that stores data…

Read more