Chapter 08 : Function Prototypes

Function with no arguments and no return values

Example: 1, Page No.: 5.55

In [1]:
#Program to addition of two numbers

def add():
    a,b=input("Enter two numbers.. ")
    c=a+b
    print "Sum is ... %d"%(c)
    
add()    
    
Enter two numbers.. 10,20
Sum is ... 30

Example: 2, Page No.:5.56

In [2]:
#Program to print addition, subtraction and multiplication values

def add():
    a=10
    b=5
    print "The Addition of given value: ",a+b
        
def sub():
    a=10
    b=5
    print "The Subtraction of given value:",a-b
    
def mul():
    a=10
    b=5
    print "The Multiplication of given value:",a*b
    
    
add()
sub()
mul()
The Addition of given value:  15
The Subtraction of given value: 5
The Multiplication of given value: 50

Function with arguments and no return values

Example: 1, page No. 5.57

In [3]:
#Program for addition of two numbers

def add(x,y):
    z= x+y
    print "Sum is:", z

#Main Function
a, b = input("Enter two Values: ")
    
add(a,b)
Enter two Values: 10,20
Sum is: 30

Example: 2, page no. 5.58

In [4]:
def date(x,y,z):
    print "Employee Joined date is:%d/%d/%d" %(x,y,z)
    
print "Enter the employee joining date."

dd,mm,yy=eval(raw_input("Enter date, month, year: "))

date(dd,mm,yy)
Enter the employee joining date.
Enter date, month, year: 11,12,1981
Employee Joined date is:11/12/1981

Function with arguments and with return value

Example: 1, page no.5.59

In [5]:
#Program for addition of two numbers

def add(x,y):
    z=0
    z=x+y
    return z

#Main Function
a,b=input("Enter the a and b value:")
c=add(a,b)
print "Result is:",c
Enter the a and b value:10,20
Result is: 30

Example: 2, Page No.:5.60

In [6]:
#Program to send and receives the values from function

def mul(m,n):
    return (m*n)

m=n=sum=0
m,n=input("Enter two values:")
sum=mul(m,n)

print "Multiplication of the entered Values:",sum
Enter two values:30,4
Multiplication of the entered Values: 120

Function with no arguments and with return value

Example: 1, PageNo.:5.61

In [7]:
#Program for addition of two numbers

def add():
    a=b=c=0
    a,b=input("Enter the Values of a and b:")
    c=a+b
    return(c)

c=add()
print "The Output of given Values:",c
    
Enter the Values of a and b:10,20
The Output of given Values: 30

Example: 2, Page No. 5.62

In [8]:
#Program to multiply three numbers using function


def mul():
    a,b,c=input("Enter the three values for Multiplication: ")
    i=a*b*c
    return(i)

s=mul()
print "Multiplication of three numbers is= ",s
    
Enter the three values for Multiplication: 4,8,2
Multiplication of three numbers is=  64