A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
A function can return data as a result.
Creating a Function
In Python a function is defined using the def keyword:
def my_function():
print("Hello from a function")
Calling a function:-
To call a function in Python, write the function's name followed by two parentheses ()
Parameters and Arguments:-
The terms parameter and argument can be used for the same thing: information that are passed into a function.
From a function's perspective:
A parameter is the variable listed inside the parentheses in the function definition.
An argument is the value that is sent to the function when it is called.
By default, a function must be called with the correct number of arguments.
Meaning that if your function expects 2 arguments,
you have to call the function with 2 arguments, not more, and not less.
If you do not know how many arguments that will be passed into your function,
add a * before the parameter name in the function definition.
This way the function will receive a tuple of arguments, and can access the items accordingly
We can also pass some default values.
Apart from these there are also keyword arguments and arbitrary keyword arguments.
We can pass lists through the functions as arguments.
Recursion is supported by python with proper syntax and indentation.
We can return values from a function.
We can use pass statement also.
Lambda Function:-
A lambda function is a small anonymous function.
A lambda function can take any number of arguments, but can only have one expression.
Syntax:-
lambda arguments : expression
It can take any number of arguments.
Why Use Lambda Functions?
The power of lambda is better shown when you use them as an anonymous function inside another function.
Say you have a function definition that takes one argument,
and that argument will be multiplied with an unknown number.
Use that function definition to make a function that always doubles or triples the number you send in