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

FlyFeditv

๐Ÿ“˜ Introduction In this project, youโ€™ll create a command-line Expense Tracker that allows users to: โœ… Add expenses with category, amount, and descriptionโœ… View total spending and category breakdownโœ… Automatically…

Read more

NetCinFly

๐Ÿ“˜ Introduction In this project, youโ€™ll develop a Student Grades Analyzer in Python that can: This project helps you strengthen your skills with lists, dictionaries, loops, and data analysis logic….

Read more

Eagle_Pro

๐ŸŒ Introduction In this project, youโ€™ll create a Web Scraper App in Python that extracts quotes, authors, and tags from a live website.Youโ€™ll use the BeautifulSoup and Requests libraries to…

Read more

Cobra_Pro

๐ŸŒฆ๏ธ Introduction In this project, youโ€™ll build a Weather App in Python that retrieves live weather information from an online API.Youโ€™ll learn how to work with HTTP requests, JSON data,…

Read more

Show7-Pro

Concepts Covered: ๐ŸŽฏ Objective: Create a simple, text-based contact book application that allows users to: ๐Ÿ’ก Code (contact_book.py): ๐Ÿง  Example Usage Console Output Example: ๐Ÿ’พ Notes โฌ‡๏ธ Download Show7-Pro ุชุญู…ูŠู„…

Read more

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