Python Functions

What is a Function?

Function is one of basic concepts in programming language. Functions are "self contained" modules of code that performs a distinct service. It is a mathematical concept that groups a collection of operations into a useful identity, which simplifies other math expressions. A function is used to segregate different functionalities of a program and often combined to perform more complicated tasks. Most programming languages come with a pre-written set of functions that are kept in a library. These functions used to remove repetitiveness thereby resulting in lesser lines of code, ease of use and better maintenance. All programming functions have input and output. The function contains instructions used to create the output from its input. You can also write your own functions to perform specialized tasks .

Python Functions

A function is defined by using the block keyword "def" , followed by the function's name, followed by a set of parentheses which hold any parameters the function will take and ending with a colon. Next follows the block of statements that are part of this function. Functions may return a value to the caller, using the keyword- 'return' .
def function-name(Parameter list): statements return [expression]
example
def sayHello(); print("Hello World!!")

Here function is now fully defined, but if we run the program at this point, nothing will happen since we didn't call the function. So, outside of the defined function block, let's call the function with sayHello()

example
def sayHello(); print("Hello World!!") sayHello() # call the function here
output
Hello World!!

Python Function Parameters

A parameter is the variable which is part of the method’s signature (method declaration). Parameters are specified within the pair of parentheses in the function definition, separated by commas. When we call the function, we supply the values in the same way.

example
def findSum(x,y): z=x+y print("Sum of numers are :" ,z) findSum(10,20) findSum(100,50) findSum(4,2)
output
Sum of numers are : 30 Sum of numers are : 150 Sum of numers are : 6
example
def dispStatus(name,age): print("Name is :" ,name) print("Age is :" ,age) dispStatus("John",35) dispStatus("Doe",50)
output
Name is : John Age is : 35 Name is : Doe Age is : 50

Default Argument Values

We can also provide default values for function parameters in Python in case the user does not want to provide values for them. This is done with the help of default argument values. The default value is assigned by using assignment (=) operator.

example
def dispStatus(name,age=25): print("Name is :" ,name) print("Age is :" ,age) dispStatus("John") dispStatus("Doe")
output
Name is : John Age is : 25 Name is : Doe Age is : 25

The main advantage of default argument is that we can give values to only those parameters to which we want to, provided that the other parameters have default argument values.

example
def dispStatus(name,age=25): print("Name is :" ,name) print("Age is :" ,age) dispStatus("John") dispStatus("Doe",34) dispStatus("Doe",age=50)
output
Name is : John Age is : 25 Name is : Doe Age is : 34 Name is : Doe Age is : 50

Variable number of arguments

Sometimes the programs might want to define a function that can take any number of parameters, i.e. variable number of arguments, this can be achieved by using the stars (*). This is very useful when we do not know the exact number of arguments that will be passed to a function.

example
import math def dispSquare(*varArgs): #iterate through all the items for num in varArgs: print("The square of the number is :" , num*num ) dispSquare(2,4,6,8,10)
output
The square of the number is : 4 The square of the number is : 16 The square of the number is : 36 The square of the number is : 64 The square of the number is : 100

Returning a Value

Not only can you pass a parameter value into a function, a function can also produce a value. The return statement is used to return from a function. The return statement is followed by an expression which is evaluated.

example
def findSum(x,y): return x+y print(findSum(10,20)) print(findSum(100,50)) print(findSum(4,2))
output
30 150 6

Inner Functions

The main advantage of inner functions is that it protect them from anything happening outside of the function, meaning that they are hidden from the global scope.

example
def calc(x,y): def findSum(x,y): return x+y sum = findSum(x,y) print("Sum of ", x, " + ", y, " is ", sum) calc(10,20)
output
Sum of 10 + 20 is 30

If you try calling the findSum(10.20) function:

output
Traceback (most recent call last): File "sample.py", line 8, in < module > findSum(10,20) NameError: name 'findSum' is not defined

Assign functions to variables

When you assign a function to a variable you don't use the () but simply the name of the function.

example
def findSum(x,y): return x+y f1 = findSum(10,20) print(f1) f1 = findSum(40,20) print(f1)
output
30 60

Functions as parameters

There are situations we have to pass Functions as parameters to other functions.

example
def sayHello(func): return "Hello " + func def sayName(name): return name print (sayHello(sayName("John")))
output
Hello John