A function is a reusable block of code that performs a specific task. A
function runs only when it is called, making programs more organized,
readable, and efficient.
Functions help you:
Avoid repeating the same code
Break large programs into smaller parts
Make code easier to maintain and debug
Creating a Function
In Python, functions are created using the
def keyword,
followed by a function name and parentheses.
Syntax:
Example: Simple Function
This defines a function named
show_message.
The code inside the function is indented to indicate it belongs to the
function.
Calling a Function
A function does nothing until it is called. To call a function, write its
name followed by parentheses.
Calling a Function Multiple Times
Function Naming Rules
Must start with a letter or an underscore (_)
Can include letters, numbers, and underscores
Are case-sensitive
Should be descriptive and meaningful
Examples of Valid Function Names: calculate_total() _display_result() checkStatus1()
Why Use Functions?
Without functions, repeating the same logic leads to messy and inefficient
code.
Example: Without Using Functions
Using Functions for Reusability
Return Values in Functions
Functions can send data back using the
return
statement. Once
return is
executed, the function stops running.
Example: Function Returning a Value
Using Returned Values Directly
Functions Without a Return Statement
If a function does not explicitly return a value, Python returns
None by
default.
The pass Statement in Functions
A function definition cannot be empty. If you want to define a function but
implement it later, use the
pass
statement.
Function Arguments in Python
Arguments are values that you pass to a function so it can work with data.
They are written inside the parentheses when calling a function. A function
can accept one or more arguments, separated by commas.
Function with a Single Argument
Parameters vs Arguments
Parameter → The variable listed in the function
definition
Argument → The actual value passed when calling the
function
Required Number of Arguments
By default, the number of arguments passed must match the number of
parameters.
Correct Usage
Incorrect Usage (Error)
Default Parameter Values
You can assign default values to parameters. If no argument is provided, the
default value is used.
Keyword Arguments
With keyword arguments, you specify the parameter name along with the value.
This makes the order irrelevant.
Positional Arguments
Arguments passed without keywords are called positional arguments. Their
order matters.
Mixing Positional and Keyword Arguments
You can combine both types, but positional arguments must come first.
Passing Different Data Types as Arguments
Returning Values from Functions
Returning Different Data Types
Positional-Only Arguments
You can restrict parameters to accept only positional arguments using
/.
Keyword-Only Arguments
To allow only keyword arguments, use
* before
parameters.
Combining Positional-Only and Keyword-Only Arguments
Python *args and **kwargs
Python uses
*args for
variable positional arguments and
**kwargs for
variable keyword arguments.
Arbitrary Positional Arguments (*args)
Using *args with Regular Parameters
Practical Uses of *args
Arbitrary Keyword Arguments (**kwargs)
When you prefix a parameter with
**, the
function can accept any number of keyword arguments (stored as a
dictionary).
Using **kwargs with Regular Parameters
Combining *args and **kwargs
Unpacking Arguments
Variable Scope in Python
In Python, a variable’s scope determines where that variable can be accessed
in a program. Understanding scope helps prevent unexpected errors and makes
your code easier to manage.
Local Scope
Accessing Local Variables in Nested Functions
Global Scope
Local vs Global Variables with Same Name
The global Keyword
The nonlocal Keyword
The LEGB Rule
Python Decorators
A decorator allows you to add extra behavior to a function without modifying
the function’s original code.
Basic Decorator Concept
Decorating Functions with Arguments
Decorators with Their Own Arguments
Using Multiple Decorators
Preserving Function Metadata
Lambda Functions in Python
A lambda function is a small, unnamed function created using the
lambda
keyword.
Returning a Lambda from a Function
Lambda with Built-in Functions
Recursion in Python
Recursion is a programming technique where a function calls itself to solve
a problem by breaking it into smaller subproblems.
Example: Factorial Using Recursion
Fibonacci Sequence Using Recursion
Using Recursion with Lists
Recursion Depth Limit in Python
Generators in Python
Generators are special functions that produce values one at a time and pause
their execution between each value.