How to create function in Python

A function is a block of code which performs some specific task and returns back to the caller. It has a unique name.

They are helpful when need to perform the same task with different-different values or require to use the same block of code in other places within the program.

If any changes require in the future then only need to update the function code and it will automatically make change where the function it been called.

The main aim of a function is –

  1. To make the program more readable, and organized and,
  2. Reduce repeated code.

How to create function in Python

 


Contents

  1. Defining a function
  2. Calling a function
  3. Function with parameter
  4. Function with return
  5. Default value
  6. Multiple parameter

 


1. Defining a function

To create function def keyword is use in Python. Define a unique name for the function and in parenthesis, you can specify your parameters.

Function block starts with colon(:) symbol.

Syntax –

def function-name(paramenter):
  # statement

Example

def callMe():
  print("Function is called")

In above example, I have created a callMe() function which not take any arguments. It print text on the screen when it called.

 


2. Calling a function

Specify the function name and specify the parameters in parentensis () if its takes. After completing the task it return back to the program.

Syntax –

def function-name(parameter): # defining function
  # statement

function-name(parameter) # calling function

Example 

def callMe():
  print("Function is called")

callMe()
callMe()

Output

Function is called
Function is called

3. Function with parameter

Specify the parameter in the function if you want to perform the task with different-different parameters.

Syntax –

def function-name(parameter):
  # statement

function-name(parameter)

Example

def checkevenOrodd(num):
  if num%2 == 0:
  print("Even number")
  else:
  print("Odd number")

checkevenOrodd(2)
checkevenOrodd(5)
checkevenOrodd(8)

Output

Even number
Odd number
Even number

4. Function with return

Use return statement to return value from the function to the caller.

Syntax –

def function-name(paramenter):
  # statement
  return value

Example

def maxlist(list1):
  max = list1[0]
  for l in list1:
  if l > max:
  max = l
  return max

list1 = [1,55,23,2]
list2 = [4,24,78,6,21]

print("max value in list1 is ",maxlist(list1))
print("max value in list2 is ",maxlist(list2))

Above I create function maxlist() which returns max value from the list.

Output

max value in list1 is 55
max value in list2 is 78

5. Default value

Default parameter automatically supplies value even if you don’t supply value while calling the function. Default argument declaration starts from right to left.

Syntax –

def function-name(variable=value,variable=value,...):
  # statement

Example 

def displayInfo(fname,lname="Singh",age=22):
  print("first name : ",fname,", last name : ",lname,", age : ",age)

displayInfo("Yogesh")
displayInfo("Vishal",age=24) # Changing age default value
displayInfo("Mohit","Sharma")

Above I specify the default value in lname and age variable. If this variables values not defined while calling then it will use the default set value.

Output

first name : Yogesh , last name = Singh , age : 22
first name : Vishal , last name = Singh , age : 24
first name : Mohit , last name = Sharma , age : 22

6. Multiple parameter

For handling an unknown amount of items to function we use a asterisk (*) symbol in function. It holds as many values in a single argument.

Syntax –

def function-name(*argument):
  # statement

Example

def display(name,*friends):
  print("name : ",name)
  print("friends : ",friends)

display("Yogesh singh","Sonarika","Vishal","Vijay")
display("Jiten singh","Mohit","Ajay","Abhilash","Ganesh","Aditya")

I defined asterisk (*) on the second argument. When the function is called then the first paremeter goes to name variable and all other parameters goes to friends variable.

It converts the friends argument into a tuple.

Output

name : Yogesh singh
friends : ('Sonarika','Vishal','Vijay')
name : Jiten singh
friends : ('Mohit','Ajay','Abhilash','Ganesh','Aditya')

Spread the love

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *