A function is a small program that consists a group of statements. A function is basically a sub program. Many group of statements form a function. And many functions together form an application. Basically, functions are written in order to keep a complex programming simpler.
There are several built-in functions already available to perform the various tasks in the Python. For example, to display the output, Python has print() function, to take the input from user, there is input() function, to calculate square of a number, there is sqrt() function. Similarly to these functions, there is some requirement when programmer need to define their own function as per their requirement that are called ‘user-defined functions’.
What are the advantages of using Functions?
- Functions are important in programming language like PHP, because they are used to perform calculations and perform any task which are required in software development.
- Functions are reusable. It means, once the function is written, it can be reused as and when required.
- Functions provides modularity for programming technique. And modular techniques help to break down a complex task into simpler task.
- Functions in a software development makes programmer easy to debug. Code maintenance becomes very easy.
- Features of any function can be changed without affecting the others functions.
- Using functions, you can reduce the length of the program.
Difference between a function and method
A function is a set of programs that perform a specific task. A function can be written individually to perform a task in a python program. A function when written inside a class, it becomes the method. And method is always called on an object.
Defining a function
In python, we can define a function by using the ‘def’ keyword followed by function name. function name contains parenthesis which might contains parameters.
The def represent the starting of a function definition. and the function name consist of () is compulsory as it denote that this is a function. Also, we can pass parameters in function as function_name(parameter1, parameter2). A parameter gets its value from outside where this function is called.
def function_name(parameter1, parameter2): """ ... statements ... """
Calling a function
A function is never called itself. It is called to product function return. While calling the function, we should keep in mind to pass parameter in parenthesis.
function_name(parameter1, parameter2)
Let us make function that returns sum of two numbers.
def sum(parameter1, parameter2): """This is a function that will return sum of two numbers.""" c = parameter1+parameter2
Returning results from a function
While calling a function, function will return some value. Using return statement, function can return output. Let us understand the sum program defined on top.
def sum(parameter1, parameter2): """This is a function that will return sum of two numbers.""" c = parameter1+parameter2 return c result = sum(10,20) print(result) #Output 30
Example – A function to check whether a number is even or odd.
def even_odd(numb): if numb % 2 == 0: print(numb, "is Even.") else: print(numb, " is Odd.") even_odd(10) #Ouput 10 is Even.
Returning multiple values from a function
In any programming language, a function returns only a single value. In python, a function can returns multiple values.
- Function returns multiple values in the form of tuple.
- To grab these returned values from function, we do use equal number of variable of left side to assign returned values.
def sum_sub(a,b): x = a+b y = a-b return x,y c, d = sum_sub(10,6) print(c) print(d) #Ouput 16 4
Example – A function that returns the results of addition, subtraction, multiplication and division.
def sum_sub_mult_div(a,b): _sum = a+b _sub = a-b _mul = a*b _div = a/b return _sum, _sub, _mul, _div a, b, c, d = sum_sub_mult_div(18,6) print('Sum is ', a) print('Subtraction is ', b) print('Multiplication is ', c) print('Division is ', d) #Output Sum is 24 Subtraction is 12 Multiplication is 108 Division is 3.0