Functions are the key part of most of the programming languages. Function is a group of statements, used to perform a specific task.
In this article we will learn:
- What is function?
- Advantages of functions
- Python function (definition , calling)
- Documentation string in function
- Return vs print
- Function parameters
- Parameters vs Arguments
- Parameter with default values
- Anonymous function (lambda)
- Filter function
What is Function?
Functions are the key part of most of the programming languages. Function is a group of statements, used to perform a specific task. It is used to utilize code at more than one place. We can divide huge problem into small parts called functions or modules. Suppose in program, there are lines of code which need to repeat more than one time, in that case instead of copy these lines again and again, we can create a function for that and can use it whenever needed (by calling function). In other words we can say function is a block of reusable code, used to perform a specific task.
Advantages of Functions:
- Clean code: Functions improves the clarity of the code we can say results in well structured code.
- Decomposition : It breaks the complex problem into smaller parts (functions, also called modules), which is easy to understand and read.
- Speed and Time: As we need not to write code again and again so it improves the speed and saves time.
- Re usability: We can use a function more than one time in more than one programs by calling it.
- Data hiding: By using function we can hide extra information.
- Reduce Duplicate Code
In python, a function is defined with def keyword. A normal function has three main parts these are:
- Function definition
- Body of function
- Function calling
In python function is defined using def keyword, followed by the name of the function , parenthesis and colon(:). The body of the function starts with the indentation (space). Unlike other languages, python does not use curly braces , semicolons etc. Function blocks are separated by indentation.
A function is a block of code which only runs when it is called.
Types of functions in python:
There are two types of functions in python –
Built-in functions are the part of the python language example :- len() , print() etc.
User-defined functions are created by the user itself by using def keyword.
Doc-string in python functions:
Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. Doc-string is used like a comment to document a specific segment of code.
The docstrings are declared using “””triple double quotes””” just below the class, method or function declaration
The docstrings can be accessed using the __doc__ method of the function.
Here is an python function with a documentation string:
Return() vs Print()
Every function returns a value or result. If return is not specified in function than it will return none. It is the last statement of any function, which means end of block. If there is not return statement then function will execute till end of code(i.e till last statement). This value can further use in other operations, or functions. This return value may not visible to the user, but computer can use it in further operations.
Print() just shows the human user a string representing what is going on inside the computer. The computer cannot make use of that printing. It will not in any way affect a function. Print() is simply there for the human user’s benefit, useful for understanding how a program works.
We can pass additional information to a function by using parameters. Parameters are specified after the function name, inside the parentheses.We can pass number of parameters in function, separating by comma. We can also pass parameters with a default value in a function.
Parameters vs Arguments:
Most people thinks there is no difference between parameters and arguments, but there is. A parameter is a variable in a function definition. Arguments are the data members that we pass in function parameters at the time of function calling.. In other words we can say, parameter is a variable in the declaration of function. Argument is the actual value of this variable that gets passed to function.
Function with default parameters:
We can pass default parameter values with a function. During calling a function if argument values passed than function will use these values otherwise it will use default values.
In python, anonymous function is a function, that defined without a name. Normally in python we use def keyword to declare a function. Where anonymous function is defined with the lambda keyword, that is why it is also called lambda functions.Lambda expression has no name that is why it is called anonymous function.
Lambda forms can take any number of arguments but return just one value in the form of an expression. They cannot contain commands or multiple expressions. It basically breakdowns the function.
An anonymous function cannot be a direct call to print because lambda requires an expression
lambda arguments: expression
In python filter function takes two arguments, function and sequence. It returns those items of the sequence for which the function is true.
In the example below, we are using lambda expression (function) as first parameter of filter function, you can take any normal function also.
In above example filter() has two parameters –
1st Parameter – seq (List of numbers)
2nd Parameter – lambda function
In line 4 , we converts the output into list which is list of even numbers as shown below:
So it is all about the python function, Thanks