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

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

Pro_Click

๐Ÿ”ฐ What Are Lists ? A list is an ordered collection of items stored in a single variable. ๐Ÿ“Œ Lists are defined using square brackets []. ๐Ÿงช Example : pythonCopierModifierfruits…

Read more