A function is a block code that takes in some data and either performs some kind of transformation and returns the transformed data, or performs some task on the data or both.
It helps us to break the program into smaller and modular parts.
Functions are a key way to define interfaces, so programmers can share their code.
The function contains the set of programming statements enclosed by {}.
Parts of function:–
- Keyword def- This is the keyword used to say that a function will be defined now.
- Function name- This is the name that is used to identify the function. The function name comes after the def keyword.
If the function name has multiple words, they should be in lower case, and they should be separated with an underscore. - Parameter list- Parameter list are placeholder that define the parameters that go into the function. In python, parameters are enclosed in parentheses.
- Function docstring- These are optional constructs that provide a convenient way for associated documentation to the corresponding function. Docstring are enclosed by triple quotes.
- Function returns- Python function returns a value.
def name_of_function(arguments): ''' docstring ''' body of function return
To call a function means that we are telling the program to execute the function. If there is a return value defined, the function would return the value, else the function would return none.
To call the function, we write the name of the function followed by parentheses.
syntax:-
def function_name(): body of function function_name()
A function is also known as a first class object and can be passed around as arguments.
This is all about functions in python.