Function prototypes in python

 Number of ways to give user-defined functions:

1. Function with no arguments and no return statement

2. Function with no argument but return value

3. Function with arguments but no return value

4. Function with arguments and return value

1. Function with no arguments and no return:

  1. def sum_two_num():#function definition

        print("Add of two number is : ",int(a)+int(b))

    a,b=input("Enter two values : ").split()

    sum_two_num()  #function calling

Output:

2. Function with no argument but return value:

  1. def sum_two_num():

        return (print("Add of two value is: ",int(a)+int(b)))

    a,b=input("Enter two values : ").split()

    sum_two_num()

Output:

3. Function with arguments but no return value

  1. def sum_two_num(x,y):

        print("Add of two number is : ",x+y)

    a,b=input("Enter two values : ").split()

    sum_two_num(int(a),int(b))    

Output:

4. Function with arguments and return value:

  • We can return values from function using return keyword.
  • The return value must be used at the calling place by

*Use with print()

*Use in any expression

*Either store it any variable

(i) Use with print():

  1. def sum_two_num(x,y):

        return x+y

    a,b=input("Enter two values : ").split()

    print("Add of two number is : ",sum_two_num(int(a),int(b)))#return in print()

Output:

(ii) Use in any expression:

  1. def sum_two_num(x,y):

        z=x+y#Within function also have expression

        return z

    a,b=input("Enter two values : ").split()

    print("Add of two number is : ", sum_two_num(int(a),int(b)))

Output:

(iii) Store it any variable:

  1. def sum_two_num(x,y):

        return x+y

    a,b=input("Enter two values : ").split()

    c=sum_two_num(int(a),int(b))#return value is stored in variable

    print("Add of two number is : ",c)

Output:

Returning multiple values:

  • Unlike other programming languages, python lets you return more than one value from function.
  • The multiple return values must be either stored in TUPLE or we can UNPACK the received value by specifying the same number of variables on the left of the assignment of the function call.

Multiple return value stored in TUPLE:

  1. def add_10(x,y,z):

        return x+10,y+10,z+10

    a=int(input("Enter first number : "))

    b=int(input("Enter second number : "))

    c=int(input("Enter third number : "))

    result=add_10(a,b,c)

    print(result)

Output:

Multiple return values stored by unpacking in multiple variables:

  1. def add_10(x,y,z):

        return x+10,y+10,z+10

    a=int(input("Enter first number : "))

    b=int(input("Enter second number : "))

    c=int(input("Enter third number : "))

    a,b,c=add_10(a,b,c)

    print(a)

    print(b)

    print(c)

Output:

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Service Support

Need our help to Learn or Post New Concepts Contact me

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me