Function in Python

Function in Python programming language

Function in Python programming language

In this tutorial, we will discuss the function in Python programming language.

Functions of Python are a collection of related and reusable statements under a single unit.

They are used to perform a specific task and only run when they are called – similar to a method in Java

Basically, two types of function are available in python.

  1. Built-in function – Also known as pre-defined function – already defined in Python language to perform one or specific task.
  2. User-defined function – User can define the function themselves.

The advantages of user-defined functions are:

  1. Clear understanding
  2. reduced duplicating code
  3. reuse of code
  4. information hiding
 

Build-in function

There are many types of built-in function in Python language

Ex-

  • Arithmetic function in Python language
  • String function in C language

 

User-defined function

  • A function created with the def keyword in Python. It is used to identify the starting point of a function.
  • A function’s name uniquely identifies every function. It is an identifier
  • A colon mark is used to note the end of a function.
  • Parameter is used to passed argument(value) to a function-it is optional
  • the return value is an optional statement used to return value from the function

 

The syntax of Python function

def function_name(parameter_list): 

statement(s)


Example

def my_function():
print("This is my function")

 

How to call a function in Python

Syntax

def function_name():
//statements
function_name() //cal the function

Example

def my_function():
print("This is my function")
my_function()

When the above code is executed, it produces the following results

This is my function

 

Example of function

Below are some demonstrations of Python’s functions:

Program1

Creation of a simple hello world program using Python function:

def my_fun():         #create function
print("Hello world")  #statements inside the function
my_fun() #call the function

 

In the above program def is a keyword used to create the function in Python. my_fun is named (identifier) of Python function

When the above code is executed, it produces the following results

Hello world

 

Parameter and argument of function:

Parameters and arguments are used to pass information or value of a function. but the parameter is not compulsory- It is optional.

Parameters are specified inside the parentheses which follow the function name. The comma is used to separate parameters.

 

The following example describes a function with one parameter (name), when we the function is called, we pass an argument to name

Program 1

def my_name(name):
print "Hello",name
my_name("Mohanraj")
my_name ("Jhon")
my_name("Saman")

 

Here, name is a String value as a parameter.

In this program, we can pass a single parameter every time function is called.

When the above code is executed, it produces the following result

Hello Mohanaraj
Hello santhiran
Hello Kumar

Default parameter value

The following example describes how to use a default function in Python language

def my_Function(age=20):
    print("my age is :"+str(age))

my_Function(35)
my_Function(42)
my_Function()
my_Function(16)

When the above code is executed, it produces the following result

my age is :35
my age is :42
my age is :20
my age is :16

When we call the function with the parameter, it uses the argument value

When we call the function without parameter, it uses the default  value

 

More examples of function in Python

program 1

def example_Func():
a=10
print a
example_Func()

 

When the above code is executed, it produced the following result

10

Program 2

def add_two_Numbers(num1,num2):
    #function with parameter
    result=num1+num2
    print (result)
    
add_two_Numbers(4,6)
    #call the function with argument
add_two_Numbers(9,16)
add_two_Numbers(45,9)
add_two_Numbers(452,66)
add_two_Numbers(46543,66789)

 

In above the program, result,num1,num2 are int values.

Here, we can specify a pair of argument according to the pair of the parameter.

When the above code is executed, it produced the following result

10
25
54
518
113332

 

Program 3

This program passes string concatenations along with string arguments.

def display(fname,lname,age,gender):
    #create a function named display with parameter
    print ("My first name is :"+fname)
    print ("My last name is :"+lname)
    print ("My age is :"+str(age))
    print ("My sex is :"+gender)
    
display("Jhon","Petter",33,"Male")
display("Siyani","Smith",36,"Female")
#call the function with argument

 

When the above code is executed, it produced the following result

My first name is :Jhon
My last name is :Petter
My age is :33
My sex is :Male
My first name is :Siyani
My last name is :Smith
My age is :36
My sex is :Female

 

The return statement in Python function

We can return a value using return statement from the function.

The syntax of the return statement in Python

return[expression_list]

Program 1

We can return a value using return statement of function

def hello_Func():
return ('Hello I am a function in Python')
print (hello_Func())

 

When the above code is executed, it produces the following results

Hello I am a function in Python

Program 2

def my_Name(name):
return('Hello'+name)
print (my_Name(' Jhon'))
print (my_Name(' Petter'))

 

When the above code is executed, it produced the following result

Hello Jhon
Hello petter
 

program 3

def rtn_Ex():
    return 10
print rtn_Ex()

 

When the above code was executed, it produced the following result

10

Program 4

def calc_Sum(x,y):
    #define a function with  parameter
    return(x+y)
  #return value
print(calc_Sum(4,7))
print(calc_Sum(44,75))
print(calc_Sum(645,574))

 

When the above code was executed, it produced the following result

11
119
1219

 

Program 5

def addthree_num(x,y,z):
    #define a function with 3 parameter
    c=x+y+z
    return(c)
  #return value
result=addthree_num(5,7,9)
result1=addthree_num(54,57,89)
result2=addthree_num(356,877,792)
print result
print result1
print result2

 

When the above code was executed, it produced the following result

21
200
2025

Program 6

def def_Func(num1,num2=5):
    print num1,num2
def_Func(1)
def_Func(2)
def_Func(6)

 

When the above code was executed, it produced the following result

1 5
2 5
6 5

In the above program, 5 is a default value

Program 7

def student (name,marks, avg,age=16,school="Ananda college"):
    print name,marks,avg,age,school
student("Jhon",76,34.6)
student("Depak",48,23.3)
student("Saman",98,64.2)

 

When the above code is executed, it produced the following result

Jhon 76 34.6 16 Ananda college
Depak 48 23.3 16 Ananda college
Saman 98 64.2 16 Ananda college

//Example for the default value

In the above program, school is a default value

Program 8

To find volume of a round shape using Python function:

import math
def volume(r):
    v=4.0/3.0*math.pi*r**3
    print v
volume(2)
volume(1)

 

When the above code was executed, it produced the following result

33.5103216383
4.18879020479

 

 

Suggested for you

String function in Python

Arithmetic function in Python

Function in C language

String functions in C language

Nested function in C language

Arithmetic function in C language

Python program to add two number using function
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Share
Published by
Karmehavannan

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago