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

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