Python Functions | Functions without arguments

Functions without arguments

In this we do not pass any arguments to the function and the function does not return any value.

Examples:

Question:1
Write a python script to make a function to calculate and print square of a number
Sol:

#function definition
def square():
    a=int(input("Enter any no "))
    b=a*a
    print("Square = ",b)

#function calling
square()

Output:

Enter any no 5
Square = 25
>>>

Question:2
Write a python script to make a function to calculate and print the cube of a number
Sol:

#function definition
def cube():
    a=int(input("Enter any no "))
    b=a*a*a
    print("Cube = ",b)

#function calling
cube()

Output:

Enter any no 5
Cube = 125
>>>

Question:3
Write a python script to make a function to take input for two numbers calculate and print their sum, product and difference.
Sol:
Method:1 Using Single Function

#function definition
def calculate():
    a=int(input("Enter 1st no "))
    b=int(input("Enter 2nd no "))
    s=a+b
    p=a*b;
    if(a>b):
        d=a-b
    else:
        d=b-a;
    print("Sum = ",s)
    print("Prod = ",p)
    print("Diff = ",d)

#function calling
calculate()

Output:

Enter 1st no 10
Enter 2nd no 20
Sum = 30
Prod = 200
Diff = 10
>>>

Method:2 (Using Multiple  functions)

#function definition

def sum1():
    a=int(input("Enter 1st no "))
    b=int(input("Enter 2nd no "))
    s=a+b
    print("Sum = ",s)
def prod():
    a=int(input("Enter 1st no "))
    b=int(input("Enter 2nd no "))
    p=a*b
    print("prod = ",p)
def diff():
    a=int(input("Enter 1st no "))
    b=int(input("Enter 2nd no "))
    if(a>b):
        d=a-b
    else:
        d=b-a
    print("diff = ",d)
def avg():
    a=int(input("Enter 1st no "))
    b=int(input("Enter 2nd no "))
    av=(a+b)/2
    print("avg = ",av)

#function calling
sum1()
prod()
diff()
avg()

Output:

Enter 1st no 10
Enter 2nd no 20
Sum = 30
Enter 1st no 2
Enter 2nd no 3
prod = 6
Enter 1st no 25
Enter 2nd no 6
diff = 19
Enter 1st no 2
Enter 2nd no 6
avg = 4.0
>>>

Visitor Counter

0056693