FUNCTIONS
Sandhya R
Function:
➢ A function is a block of organized, reusable code used to perform
a single task. Functions help break a large program into smaller,
manageable pieces.
Why Functions are Important:
❑ Reusability
o Instead of writing the same code again, we reuse it.
o Saves time and reduces errors.
❑ Modularity
o Large programs are divided into smaller parts (functions).
o Easier to debug and maintain.
❑ Readability
o Code becomes more structured and understandable.
Sandhya R
Function Syntax:
• A function is defined using the def keyword.
→ def → statements
A keyword in Python The actual logic (calculation,
Tells Python → “A function starts here” processing)
→ function_name → return
Identifier (name of function) Sends result back to the caller
Must follow variable naming rules Ends the function execution
→parameters
Variables that receive input values
Optional (function may or may not have
them) Sandhya R
Example
➢ Function is defined but not executed
➢ It runs only when called
Function Calling
Function calling is the process of executing a function by using its
name followed by parentheses.
. Step-by-Step Execution
1. Python sees function call
2. Passes argument to parameter
3. Executes code
4. Returns result
5. Continues main program
Sandhya R
Example (Without Function) Example (With Function)
Sandhya R
Types of Functions
1. Built-in Function
➢ Built-in functions are predefined functions provided by Python, which are
always available for use without importing anything.
2. Module Functions
➢ Module functions are functions that are defined inside a module (separate file)
and must be imported before use.
3. User-defined Functions
➢ User-defined functions are functions created by the programmer to perform
custom tasks.
Sandhya R
Parameter
➢ A parameter is a variable listed in the function definition that acts as a
placeholder to receive input values.
Argument
➢ An argument is the actual value passed to a function when it is called.
Parameters vs Arguments
Example
Sandhya R
Types of Arguments
Positional Arguments
Arguments that are assigned to parameters based on their
position/order.
Keyword Arguments
Arguments passed using parameter names, so order does not matter.
Default Arguments
Parameters that have predefined default values, used when no
argument is provided.
Sandhya R
Return Statement
• The return statement is used to send a value back from the function to the caller.
Example
Void Function
• A void function is a function that does not return any value.
➢ Only performs action
➢ Output is not stored
Non-Void Function
• A function that returns a value using the return statement.
Sandhya R
Global Variable
A global variable is a variable defined outside all functions and
accessible throughout the program.
➢ Exists for entire program
➢ Can be accessed anywhere
Output:
x = 10 is defined outside the function → so it is global
It can be accessed:
Inside the function
Outside the function
Sandhya R
Local Variable
A local variable is a variable defined inside a function and accessible
only within that function.
➢ Exists only during function execution
➢ Destroys after function ends
Output:
y = 5 is defined inside the function → so it is local
It can be accessed:
Inside the function
Outside the function (Error)
Sandhya R
LEGB Rule
➢ LEGB rule defines the order in which Python searches for variable names.
Order
Step 1: Inside inner() function
• Local : Inside Current function print("Inside inner:", x)
• Enclosing : Inside outer function Python looks for x:
• Global : outside all the functions Local → found "Local"
• Built-in : Python Reserved names So it prints:
Inside inner: Local
Output:
Step 2: Inside outer() function
print("Inside outer:", x)
Python looks for x:
Local to outer() → "Enclosing"
Inside outer: Enclosing
Step 3: Outside all functions
print("Outside:", x)
Python looks:
Global → "Global"
Outside: Global Sandhya R
Mutable Objects
➢ Mutable objects are objects whose values can be changed after creation.
Examples
• List Step 1: Initial list
• Dictionary numbers = [1, 2, 3]
A list is a mutable object (can be
• Set changed)
Step 2: Function call
modify_list(numbers)
The same list object is passed to the Output:
function
No new copy is created
Step 3: Inside function
my_list.append(4)
The original list is modified
Now list becomes: [1, 2, 3, 4]
Step 4: After function
print(numbers)
The change is reflected outside also Sandhya R
Immutable Objects
➢ Immutable objects are objects whose values cannot be changed after creation.
Examples
✓ int
Step 1: Initial value
✓ string num = 10
✓ tuple num is an integer (immutable type)
Step 2: Function call
change_value(num) Output:
Value 10 is passed to parameter x
Step 3: Inside function
x = 100
This does NOT modify original num
Python creates a new local variable
x = 100
Step 4: After function
print(num)
Original value is still 10
Sandhya R
Function Composition
➢ Function composition is the process of using the output of one
function as the input to another function.
o Functions can be combined
o Makes code powerful and compact
Sandhya R
Example: Step 1: Inner function runs first
make_name("Sandhya", "Ramakrishna")
Combines strings
Returns: "Sandhya Ramakrishna"
Step 2: Outer function runs
greet("Sandhya Ramakrishna")
Adds greeting
Returns: "Hello Sandhya Ramakrishna"
Step 3: Final output
print(result)
Output:
Sandhya R
Sandhya R