Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to create function in Python

Tags: function

While writing the program there may be some situation where you need to write the block of code to find the number is the prime number or not and you need to use the same block of code for multiple times. After some time, you understand that there is some bug in one of the statement. Then you will need to fix the code in all those places where the code is been used. Which is going to take a long time, But if you used a Function instead of copy paste the same code, maintenance will be easier.

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

The aim of a function is –

  1. To make your program more readable and organized, and more importantly,
  2. To reduce repeated code.


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

In Python function are defined using def keyword with it function-name and in parenthesis parameter or arguments  is placed. Function block starts with colon(:) a symbol.

Syntax –

def function-name(paramenter):
      # statement

Example –

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

Above we created a function name it callMe without any parameter. In function, we define print which contains text "Function is called". Whenever the function is called in the program then it prints text those on screen.

When we execute above code then nothing happens because we defined an only structure of function not using it.


2. Calling a function

After defining function structure now it time to use the defined function for these we need to call it. For calling, we need to specify the name of the function and pass parameter if required. Whenever we call a function with parameter then it creates a temporary copy of the value and stores it in the variable.

Syntax –

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

function-name(parameter) # calling function

Example –

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

callMe()
callMe()

When we execute the above program, it will produce the following output –

Function is called
Function is called

3. Function with parameter

We can define the parameter in function when we want to use the function with a different-different parameter.

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)

When we execute the above program, it will produce the following output –

Even number
Odd number
Even number

4. Function with return

From function, we can also return value which is back to the caller. Which we do by specifying return statement in the function.

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 we defined a function which finds max value within a list and returns the max value to the caller.

When we execute the above program, it will produce the following 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 we define a function which takes first name,last name, and age. Here, we created two default parameter one is age and another is lname in function.

When we execute the above program, it will produce the following 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")

In above program, we define a function name its display. It takes two parameters one is the name and another parameter is variable with an asterisk(*).

When we call the function then the first parameter goes to name, after those other parameters go to friends. When the function executes it converts friends argument into a tuple.

When we execute the above program, it will produce the following output –

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

The post How to create function in Python appeared first on Makitweb.



This post first appeared on Makitweb, please read the originial post: here

Share the post

How to create function in Python

×

Subscribe to Makitweb

Get updates delivered right to your inbox!

Thank you for your subscription

×