Functions in Python
Functions in Python
Topics Covered:
• Concept of function.
• Function declaration.
• Function calling.
• Return statement.
1. Function Definition:
You define a function using the `def` keyword, followed by the
function name and parentheses. Any parameters the function takes
are listed within the parentheses.
2. Function Call:
To execute a function, you "call" it by using the function name
followed by parentheses. If the function has parameters, you pass
values inside the parentheses.
3. Return Statement:
A function can return a value using the `return` statement. The
returned value can be assigned to a variable or used in other
expressions.
4. Default Arguments:
You can provide default values for function parameters. If a value is
not passed for a parameter, the default value is used.
2
5. Variable Scope:
Variables defined inside a function are local to that function unless
declared as `global`. They are not accessible outside the function.
6. Lambda Functions:
Python supports the creation of anonymous functions using the
`lambda` keyword. These are often used for short, simple
operations.
3
Parameters and Arguments in python
1. Parameters:
• Definition: Parameters are the variables listed in the
function definition. They act as placeholders for the values
that a function will receive when it is called.
• Location: Parameters are specified within the parentheses
following the function name in the function definition.
- Example:
2. Arguments:
• Definition: Arguments are the actual values that are passed to a
function when it is called. They correspond to the parameters
defined in the function.
• Location: Arguments are the values provided within the
parentheses when calling a function.
- Example:
3. Parameter Types:
Positional Parameters: Matched by position. The order in which
arguments are passed matters, and they are assigned to parameters
based on their position.
4
Keyword Parameters: Matched by name. You explicitly mention
the parameter name when passing arguments, which allows you to
skip the order.
5
5.Void Functions:
• Functions that do not return a value (void functions) are still
called in the same way, but the result is usually not assigned.
1. Basic Usage:
- The `return` statement is followed by the value or expression
that the function should return.
- It signifies the end of the function's execution, and the control is
handed back to the calling code.
6
2. Returning Multiple Values:
- You can return multiple values by separating them with
commas. The returned values are often received as a tuple in the
calling code.
3. Exiting Early:
- If a `return` statement is encountered before the end of the
function, the function will exit immediately, and the specified value
will be returned.
7
4. Returning `None`:
- If no `return` statement is specified in a function, it implicitly
returns `None`. This is equivalent to having a `return None`
statement at the end of the function.
8
7. Exiting a Loop with `return`:
- In a function containing loops, `return` can be used to exit the
loop and the function prematurely.