Block of reusable code to perform a specific action.
Reusing Code
Using an existing code without writing it every time we need.
Code
Input
Output
Defining a Function
Function is uniquely identified by the
Code
Input
Output
Calling a Function
The functional block of code is executed only when the function is called.
Code
Input
Output
Defining & Calling a Function
A function should be defined before it is called.
Code
Input
Output
Printing a Message
Consider the following scenario, we want to create a function, that prints a custom message, based on some variable that is defined outside the function. In the below code snippet, we want to access the value in the variable
Code
Input
Desired Output
We use the concept of Function Arguments for these types of scenarios.
Function With Arguments
We can pass values to a function using an Argument.
Code
Input
Output
Variables Inside a Function
A variable created inside a function can only be used in it.
Code
Input
Output
Returning a Value
To return a value from the function use
Exits from the function when return statement is executed.
Code
Input
Output
Code written after
Code
Input
Output
Built-in Functions
We are already using functions which are pre-defined in Python. Built-in functions are readily available for reuse
- print()
- int()
- str()
- len()