BASICS OF FUNCTIONS
Python tutorial · PySpark.in
BASICS OF FUNCTIONS PYTHON
A function in Python is a named block of structured and reusable code that is designed to perform a specific task. Functions help in organizing a program into smaller, manageable units, thereby improving readability, reusability, and maintainability of code.Functions are created using the def keyword and can be executed by calling their name.
Need for Functions
Without functions:
- Programs become lengthy and difficult to manage
- Same logic must be written repeatedly
With functions:
- Code duplication is avoided
- Programs become modular
- Debugging and testing become easier
General Syntax of a Function
```
HIDE
def function_name(parameters):
statements
return value
```
Important Terminologies
Term | Meaning |
|---|---|
Function Definition | Writing the function |
Function Call | Executing the function |
Parameters | Variables in function definition |
Arguments | Values passed during function call |
Return | Value sent back to caller |
Let’s understand each ……
1.FUNCTION DEFINITION
Function definition refers to the process of creating or declaring a function using the def keyword, specifying the function name, parameters, and body.
During function definition, Python does not execute the function, it only stores the function in memory.
Syntax
```
Hide
def function_name(parameters):
statements
``` Example
```
def add(a, b):
c = a + b
return c
```
2. FUNCTION CALL
A function call refers to the process of executing a function by using its name followed by parentheses ().When a function is called, control of the program shifts from the calling statement to the function body.
Syntax
```
Hide
function_name(arguments)
```
Example
```
Hide
result = add(10, 20)
```
Execution Flow
- Program encounters function call
- Control transfers to function definition
- Arguments are assigned to parameters
- Function body executes
- Control returns to the calling statement
3. PARAMETERS
Parameters are the variables listed in the function definition that receive values from the calling function. They act as placeholders for the data that will be passed during the function call.
Example
```
hide
def multiply(x, y):
return x * y
```
Here:
- x and y are parameters
Characteristics of Parameters
- Defined inside parentheses in function definition
- Receive values during function call
- Local to the function
Note
Parameters exist only inside the function.
4. ARGUMENTS
Arguments are the actual values supplied to a function at the time of function call. They provide the data on which the function operates.
Example
```
hide
multiply(5, 4)
```
Here:
- 5 and 4 are arguments
Difference Between Parameters and Arguments
Parameter | Argument |
|---|---|
Defined in function definition | Used in function call |
Acts as a placeholder | Actual value passed |
Variable name | Constant or variable |
Local to the function | Exists in calling scope |
Receives data | Supplies data |
Defined only once | Can change for each call |
5.RETURN STATEMENT
The return statement is used to send a value from the function back to the calling statement and terminate the execution of the function. If a function does not use a return statement, it returns None by default.
Syntax
```
Hide
return expression
```
Example
```
hide
def square(n):
return n * n
result = square(4)
print(result)
```
Key Characteristics of Return
- Ends function execution
- Can return any data type
- Can return multiple values (as tuple)
- Transfers control back to caller
Example: Multiple Return Values
```
hide
def operations(a, b):
return a+b, a-b
x, y = operations(10, 5)
```
COMPLETE FLOW EXAMPLE
```
hide
def add(a, b): # Function Definition
return a + b # Return statement
result = add(10, 20) # Function Call with arguments
```
More Python tutorials
- What is Python and Why is it used for Data Science and Data Engineering?
- How Does Python Work in the Backend? Internal Working of Python
- Top 30 Python Interview Questions for Data Science
- 3-Month Python Roadmap to Excel in Data Science and Machine Learning
- Python Data Types Explained – A Beginner’s Guide
- test
All tutorials · Try the free PySpark compiler · Practice challenges