Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Functions in Python

Uploaded by

Arnav Rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Functions in Python

Uploaded by

Arnav Rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

DAY-6

Topics Covered:
• Concept of function.
• Function declaration.
• Function calling.
• Return statement.

In Python, a function is a block of organized, reusable code that


performs a specific task. Functions provide a way to structure and
modularize your code, making it more readable, maintainable, and
efficient. Here are some key points about functions in Python:

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.

Default Parameters: Parameters can have default values. If an


argument is not provided, the default value is used.

4. Arbitrary Arguments and Keyword Arguments:


Python allows the use of `*args` and `kwargs` to pass a variable
number of arguments or keyword arguments to a function.

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.

The `return` statement in Python is used in a function to exit the


function and return a value to the calling code. Here are some key
points to note about the `return` statement:

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.

5. Use in Conditional Statements:


- The result of a function that includes a `return` statement can be
used directly in conditional statements.

6. Returning Early in Void Functions:


- In functions that do not return a value (void functions), you can
use `return` to exit early without specifying a value.

8
7. Exiting a Loop with `return`:
- In a function containing loops, `return` can be used to exit the
loop and the function prematurely.

You might also like