Functions in Python

 

Functions in Python

 A function is a block of code which is executed when it is called.  Code re usability is most important feature of functions. Almost every programming languages have the functions to simplify the complex program by dividing it into smaller modules.

 Defining The Functions:

 def function_name():

          body of function

def keyword is used to define the functions in Python. function_name is user defined name followed by pair of parenthesis and colon(:). Then write body of function.

    for example,

def samplefunction():

    print("This is fiunction definition")

    print("Simple function")

 

In above example def keyword is used to define samplefunction(). It is user defined name. In the above program no any output will display because function is not called.

Function Call:

To call a function use function name followed by parenthesis.

Syntax:

function-name()

For Example:

def samplefunction():

    print("This is fiunction definition")

    print("Simple function")

samplefunction()

O/P:

This is fiunction definition

Simple function

 Function Return:

return keyword is used to return the values to function. When the function is called the values are returned to function.

Example to compare two numbers and display result

def simplenumbers(x,y):

    if x>y:

        return x

    else:

        return y

simplenumbers(10,5)  

O/P:

10

 Example to compare two numbers and display result with some formating

# Modify the result

def simplenumbers(x,y):

    if x>y:

        return x

    else:

        return y

result=simplenumbers(10,55)

print("The biggest number",result)

O/P:

The biggest number 55

 Program to take user input and compare two numbers and display biggest number

x=int(input("Enter first number:\n"))

y=int(input("Enter second number:\n"))

def comparenumbers(x,y):

    if x>y:

        return x

    else:

        return y

result=comparenumbers(x,y)

print("The biggest number is:\n",result)

O/P:

Enter first number

20

Enter second number

10

The biggest number is:

 20

 Arguments in Functions:

Argument is nothing but the values that are passed within the parenthesis of function.

We can use one or more than one arguments written in pair of parenthesis of function and separated them with comma.

Following function is defined with one argument.

# function with one argument

def myname(name):

    print("My name is",name)

myname("Manisha")

O/P:

My name is Manisha

 # function with many arguments

def myname(fname,mname,lname):

    print("My name is",fname,mname,lname)

myname("Manisha","M","More")

O/P:

My name is Manisha M More

 Addition of three numbers using function

def addition(a,b,c):

    print("Addition of three numbers is:\t",a+b+c)

addition(10,20,30)

O/P:

Addition of three numbers is:    60

 Addition of three numbers by taking user input

a=int(input("Enter first number\n"))

b=int(input("Enter second number\n"))

c=int(input("Enter third number\n"))

def addition(a,b,c):

    print("Addition of three numbers is:\t",a+b+c)

addition(a,b,c)

O/P:

Enter first number

20

Enter second number

20

Enter third number

20

Addition of three numbers is:    60

 

   

   

 

 

 

   

 

 

 

टिप्पणी पोस्ट करा

0 टिप्पण्या