Function Concepts in Python part 1

By

What are functions:-

  • Python function is a block of code that performs a specific task.
  • Python function allows us to divide a large program into the basic building block.
  • Python function helps to break the program code  into smaller part.It is used to avoid the repetition of the code.
Types of functions

There are two types of the functions in python.

  1. Built-in Functions
  2. User defined functions
 1.) Built-in functions:- The Built-in functions are those functions which are predefined in python.I have already explained more built-in functions in our previous lectures.e.g. len(),min(),max(),range() ,print() etc.
2.) User defined functions:-The user defined functions are those functions which are defined by user for any specific tasks in python.e.g. show(),display(),calculate() etc.
Advantage of functions:-
There are two main advantage of functions as given below:-
  • Functions help us to divide our program into multiple tasks.For each task we can define a separate functions.This makes the code modular.
  • Functions provide us a reuse mechanism.
Properties of functions
  • The body of the function must be indented suitably.
  • A function can be called any number of times.
fun1()               #first function call
fun2()               #second function call
fun3()               #third function call
  • When a function is called ,control is transferred to the function and its statements are executed and control is returned to place from where the call originated.
  • Built-in Function Name always use lowercase characters.
  • User-defined function Name can be used lower or uppercase characters
  • Function names connect multiple words using  hyphen sign (-).

Creating a python Function:-

Python  provides the def keyword to define the function.

Syntax:-

def fun (parameter):
    Block of codes
    return

Descriptions:-

  • The def keyword is used to define the function in python.
  • The identifier rules must be follow to write the name of the function in python.
  • A function accepts the parameters(arguments) according to the requirements of the function.The parameters or arguments can be optional.
  • The function block is started with colon : and block of codes must be at the same indentation.
  • The function may have a return statement.It is used to return the value to the calling function.
  • A function can have only one return statement in python.It can be optional.
Arguments in Function:-
A arguments are the type of information which can be passed into the function.This comes under small brackets and separate with a comma.
e.g.
#define a function
def sum (a,b):
    result=a+b
    print ("The sum of two numbers is:",result)
#callig  the function
sum(20,40) 
Output:-
The sum of two numbers is: 60
>>> 
Function Calling:

  • We first create function after that we can call it from another location.
  • A function must be defined first ,after that call it otherwise python interpreter gives an error.
e.g.
#define a function
def fun ():
    print("Welcome to MNT LAB")
#function calling    
fun()   
Output:-

Welcome to MNT LAB

>>> 

How to use return statement in function:-

The return statement is used at the end of the function and returns the result to the calling function.

Syntax:-

return [expression]

e.g.

#define a function
def multiplication (a,b,c):
    result=a*b*c
    return result
    print("Welcome to MNT LAB")
x=int(input("Enter first value: "))
y=int(input("Enter second value: "))
z=int(input("Enter third value: ")) 
#function calling
print("Multiplication of three number is:",multiplication(x,y,z))

Output:-

Enter first value: 10

Enter second value: 20

Enter third value: 30

Multiplication of three number is: 6000

>>> 

Description:-

In above example,i have defined a multiplication function with three arguments.It has a statement result=a*b*c , which is used to compute the a,b and c  values and stored it in result variable.This result value is return by this function to the calling function.Here i have taken these three value by user as shown in above code.

Calling the Function Without Return statement:-

We can call the python functions without return statement as given below:-

e.g.

#define a function
def multiplication (a,b,c):
    result=a*b*c
    print("Multiplication of three number is:",result)
    print("Welcome to MNT LAB")
x=int(input("Enter first value: "))
y=int(input("Enter second value: "))
z=int(input("Enter third value: ")) 
#function calling
multiplication(x,y,z)

Output:-

Enter first value: 10

Enter second value: 20

Enter third value: 30

Multiplication of three number is: 6000

Welcome to MNT LAB

>>> 

Call by reference in Python

The Memory of Call by reference is to passing the actual value as an argument in the function.All the functions are called by reference,Means all the changes made to the reference inside the function revert back to the original value referred by the reference. 

e.g. 

Passing Mutable object(list1) in program

list1=[10,20,30,40]
def modify_list(list1):
    list1.append(50)
    list1.append(60)
    list1.append(70)
    print("The list inside the function is:",list1)
# calling the function
modify_list(list1)
print("The list outside the function is:",list1)

Output:-

The list inside the function is: [10, 20, 30, 40, 50, 60, 70]

The list outside the function is: [10, 20, 30, 40, 50, 60, 70]

>>>

e.g. 

Passing Immutable object(string) in program

string="Hello Friends "
#define the function
def modify_string(str1):
    str1=str1+"Welcome to MNT LAB"
    print("print the string inside function is: ",str1)
#calling the function
modify_string(string)    
print("print the function outside the function is ",string)

Output:-

print the string inside function is:  Hello Friends Welcome to MNT LAB

print the function outside the function is  Hello Friends 

>>> 

Watch Complete Lecture Video:-

For More...

  1. List concepts in python part 2
  2. How to save image in database and display in picture box.
  3. For oop concepts in python with examples
  4. WCF concepts in .net
  5. Algorithm concepts in python with examples

0 comments:

Post a Comment

Powered by Blogger.