Function is a group of statements that performs specific task. It runs only when it is called. You can pass data (parameters) into a function.
Python has many built-in functions; we can create our own functions and those are called user-defined functions.
As our program grows larger and larger function makes your program look more organized and manageable, by breaking codes into smaller parts.
Functions makes the code re-usable.
Functions can return data as result.
Syntax:
Syntax of function
A function is defined using def keyword followed by unique function name.
Some rules to define function:
· Input parameter should be within parentheses.
· Code block with every function should be intended.
· Optional documentation string (doc string) can be used to describe what a function does. “docstring” should be within triple quotes.
How to call a function?
Once a function is defines we can call the function by entering its name with appropriate parameters.
We can call a function from another function also.
Defining and calling a function
Docstring:
Documentation string is called docstring. It is the first string after function header. It is used to describe what a function does.
Even though it is optional, it is always better to use docstring.
Triple codes are used so that docstring can have multiple lines.
We can view the docstring by using;
<<function_name>>.doc
docstring
Parameters and arguments:
A parameter is the variable listed inside the parentheses in the function definition whereas an argument is the value assigned to that variable when the function is called.
Parameter and Argument
#data-science #python-programming #python #programming