FivG_Pro

🔰 What Is a Function?

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

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 needed.

✍️ Defining a Function

def greet():
    print("Hello, welcome to Python!")
    
greet()

Output:

Hello, welcome to Python!

🎯 Functions with Parameters

Functions can accept parameters (inputs).

def greet(name):
    print(f"Hello, {name}!")

greet("Ali")
greet("Sara")

Output:

Hello, Ali!
Hello, Sara!

🎯 Functions with Return Values

Functions can return data using return.

def add(a, b):
    return a + b

result = add(5, 3)
print("The sum is:", result)

Output:

The sum is: 8

📘 Default Parameters

You can assign default values to parameters.

def greet(name="Friend"):
    print(f"Hello, {name}!")

greet()          # uses default
greet("Omar")    # overrides default

Output:

Hello, Friend!
Hello, Omar!

🔍 Variable Scope (Local vs Global)

  • Local variable: exists only inside a function.
  • Global variable: exists everywhere in the program.
x = 10  # global variable

def test():
    x = 5  # local variable
    print("Inside function:", x)

test()
print("Outside function:", x)

Output:

Inside function: 5
Outside function: 10

📦 Lambda (Anonymous Functions)

Quick one-line functions using lambda.

square = lambda x: x * x
print(square(4))

Output:

16

🧠 Exercise 1: Calculator Functions

  1. Write functions: add, subtract, multiply, divide.
  2. Ask the user for two numbers.
  3. Use the functions to print results.

🧠 Exercise 2: Scope

  1. Create a global variable counter = 0.
  2. Write a function that increases the counter.
  3. Show the difference between modifying it locally vs globally.

📝 Summary

  • Functions help us organize and reuse code.
  • Use parameters to pass values and return to get results.
  • Scope determines where variables can be accessed.
  • Lambda functions are quick, single-line functions.
⬇️ Download FivG_Pro تحميل Server 1 ⬇️ Download FivG_Pro تحميل Server 2 ⬇️ Download FivG_Pro تحميل Server 3

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