A function is a block of organized, reusable code that is used to perform a single, related actions.
Defining a Function:-
A function is created using def Keyword followed by parenthesis(def functionName()). The code block within every function starts with a colon (:) and is indented.
def functionname( parameters ):
Calling a Function:-
After defining a function name and specifies the parameters, we have to need call that function .
Output:- This is a function
Return Statement in Python:-
A return statement is used to end the execution of the function call and “returns” the result to the caller. The statements after the return statements are not executed.
Types of Functions
There are three types of functions in Python:-
- Built-in functions, such as
help()to ask for help,
min()to get the minimum value,
print()to print an object to the terminal.
- User-Defined Functions , which are functions that users create to help them out.
- Anonymous functions, which are also called lambda functions because they are not declared with the standard
defkeyword. You can use the lambda keyword to create small Anonymous functions Example:- lambda_cube = lambda y: y*y*y
Scope of Variables:-
The scope of a variable determines the portion of the program where you can access a particular identifier. There are two basic scopes of variables in Python-
1. Global variables- Variables that are defined outside a function body have a Global scope.
2. Local variables- Variables that are defined inside a function body have a Local scope.