# Chapter 15 : Pointers and Functions¶

## Example: 1, Page no. 5.112¶

In [1]:
#Call by value

def change(x,y):
x=x+5
y=y+10
print "In the function changes a&b is %d,%d"%(x,y)

a=10
b=20

print "\nBefore calling the function a&b is %d,%d"%(a,b)

change(a,b)
print "\nAfter calling a&b is %d,%d"%(a,b)


Before calling the function a&b is 10,20
In the function changes a&b is 15,30

After calling a&b is 10,20


## Example: 2, Page no.5.113¶

In [8]:
#program to swap(interchange) the two given variable.

def swap(x,y):

x=x+y
y=x-y
x=x-y
print "In swap function x=%d, y=%d"%(x,y)
return y,x

a=10
b=20

print "Before swap a=%d, b=%d"%(a,b)

print "\nAfter the calling the swap function, a=%d,b=%d"%swap(a,b)

Before swap a=10, b=20
In swap function x=20, y=10

After the calling the swap function, a=10,b=20


## Example: 1, Page.No.5.114¶

In [15]:
#Call By Reference

def change (x,y):
x=x+5
y=y+10
print "In the function change a&b is %d %d"%(x,y)
return x,y
a=10
b=20
print "\nBefore calling the function a&b is %d %d"%(a,b)

print "After calling the function a&b is %d %d"%change(a,b)

Before calling the function a&b is 10 20
In the function change a&b is 15 30
After calling the function a&b is 15 30


## Example: 2, Page No.5.115¶

In [35]:
#Program to swap(intercahnge) the value of variables.

a=10
b=20

print "\nBefore swap a=%d, b=%d"%(swap(a,b))

def swap(x,y):
x=x+y
y=x-y
x=x-y
print"\nIn swap function x=%d,y=%d"%(x,y)
return y,x

print "\nAfter calling swap function a=%d, b=%d"%(b,a)

In swap function x=20,y=10

Before swap a=10, b=20

After calling swap function a=20, b=10


## Example: 3, Page no. 5.115¶

In [3]:
#Program to interchange the value of variables using call by reference
def exchange(m,n):
t=m
m=n
n=t
return n,m

a,b=input("Enter two Numbers: ")

print "Before Exchange a=%d,b=%d"%(a,b)

print "After Exchange a=%d, b=%d"%(exchange(b,a))

Enter two Numbers: 10,20
Before Exchange a=10,b=20
After Exchange a=20, b=10