Total Pageviews

Wednesday, September 9, 2026

Defining Functions,

🐍 Functions in Python

Defining, Calling, Passing Arguments and Returning Values

1. What is a Function?

A function is a reusable block of Python code designed to perform a specific task.

Instead of writing the same code repeatedly, we define the code inside a function and call the function whenever required.
♻ Reusable

Write code once and execute it multiple times.

🧩 Modular

Large programs can be divided into smaller modules.

📖 Readable

Functions make programs easier to understand.

🔧 Maintainable

Changes can be made inside one function.

2. Defining a Function

Python uses the def keyword to define a function.

def function_name():
    statements

Important Parts

Part Meaning
def Keyword used to define a function.
function_name Name of the function.
() Contains parameters, if required.
: Marks the beginning of the function body.
Indented statements Statements executed when the function is called.

3. Simple Function Example

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

greet()

Output

Hello, Python!

Here, greet() is the function call. When Python reaches this statement, the function body is executed.

4. Function with Parameters

A parameter is a variable defined inside the function declaration to receive data.

def greet(name):
    print("Hello", name)

greet("Bijan")

Output

Hello Bijan
Term Example Meaning
Parameter name Variable defined by the function.
Argument "Bijan" Actual value passed to the function.

5. Function with Multiple Parameters

def add(a, b):
    print("Sum =", a + b)

add(10, 20)

Output

Sum = 30

6. Function with Return Value

The return statement sends a value back from the function to the calling statement.
def square(n):
    return n * n

result = square(5)

print("Square =", result)

Output

Square = 25

7. How a Function Works

Define Function
Pass Arguments
Execute Function
Return Result
Use Result

8. Common Types of Functions

Type 1

Function without parameters and without return value.

Type 2

Function with parameters but without return value.

Type 3

Function without parameters but with return value.

Type 4

Function with parameters and return value.

9. Interactive Function Demonstration

Click a button to see how different function calls work.

Select an example above.

10. Important Points for Examination

  • Functions are defined using the def keyword.
  • A function is executed when it is called.
  • Parameters receive values passed to a function.
  • The return statement sends a value back.
  • Python functions improve code reusability.
  • Indentation is compulsory inside a function.
  • A function may have zero, one, or multiple parameters.

11. Quick Summary

def function_name(parameters):
    statements
    return value

function_name(arguments)
Remember: Define → Call → Execute → Return
```

No comments:

Post a Comment