Python- Function

We use the def keyword to create a user-defined function.

eg: def function_name( parameters ) :

eg: There are two functions in this program. One which returns a value and the other which does not return a value.

def calculate_sum( num1, num2 ):
total = num1 + num2
print (“First number is :”, num1)
print (“Second number is : “, num2)
print (“The total sum is “, total)
return total ;

def calculate_diff (num1, num2):
print (“Function without return”)
print (“First number is :”, num1)
print (“First number is :”, num2)
print (“Difference between num1 and num2 is :”, num2-num1)

total = calculate_sum(10,20)
print (“We can return the value as well :”, total)

calculate_diff(20,40)

 

————————————————————————————

 

The output will be shown as :

 

First number is : 10
Second number is : 20
The total sum is 30
We can return the value as well : 30
Function without return
First number is : 20
First number is : 40
Difference between num1 and num2 is : 20

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s