Using function to calculate addition, subtraction and multiplication of two numbers

In this blog, we will create a program using function that performs three calculation such as addition, subtraction and multiplication.

First of all, create a function

def cal(a,b):
    c = a+b
    print('Addtion of %d and %d is %d'%(a,b,c))
    c = a - b
    print('Subtraction of %d and %d is %d' % (a, b, c))
    c = a * b
    print('Multiplication of %d and %d is %d' % (a, b, c))

Above function cal() has two parameters. This parameters will take values when the function is called.

Now we will call cal() function. Also submitting two values inside the function. Values used during call to cal() will be passed onto parameters in actual cal() function. Values 10 and 9 will be passed onto parameters a and b

cal(10,9)

Output

Value of a parameter is 10
Value of b parameter is 9

Addtion of 10 and 9 is 19
Subtraction of 10 and 9 is 1
Multiplication of 10 and 9 is 90

Complete Code

def cal(a,b):
    print('Value of a parameter is %d'%a)
    print('Value of b parameter is %d' %b)
    c = a+b
    print()
    print('Addition of %d and %d is %d'%(a,b,c))
    c = a - b
    print('Subtraction of %d and %d is %d' % (a, b, c))
    c = a * b
    print('Multiplication of %d and %d is %d' % (a, b, c))

cal(10,9)

Function can be called multiple times.

Advertisement

Leave a Comment

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 )

Connecting to %s