Tuesday, December 1, 2020

Returning Results from a Function in Python

Returning Results from a Function in Python

================================

* One can return the result or output from a function using a 'return' statement within the body of a function . 

Example :

return c    # this returns any value assigned to a computing variable acting as storage holder for variable

return 500   # this returns the value 500

return 50000    # this returns the value 50000

return lst          # return a list after computation of any value

return x,y,z        # return any value within a function that necessitates the return of three values as                                         output after computation of any result around these three variables

Another significant result about a function is that a function does not return any result and one need not write the return statement within the body of the function . One can rewrite the sum() function such that it will return the sum value rather than displaying the value .

=========================================================================

Lets see an example with usage of return statement a python program to find the sum of two numbers and return the result from the function .

def sum(a ,b):

     """ this function returns sum of 2 no.s """

     c = a + b

     return c

     # calling the function by associating a variable to the called function

     x = sum(10,20)

     print('The sum of the function is:',x)

     y = sum(12.5,17.5)

    print('The sum of the function is:',y)

For the above program the output or result returned after computation of the parameter values to the sum function - and the calculated result is returned to the variable c which does the computation and calculation of the values which get stored within the returning variable . Then the returning value is stored into the variables over which the calling function "sum" is assigned to .

So in the above program one can see that the result of .So in the above program one can see that the result of the sum operation is computed using the sum function and then the output of the operation is stored into the values x and y .

In order to understand the concept of functions , one can visualise a function which accepts a number and tests whether the number is even or odd . The function definition can be written in the following manner :


def even_odd(num):

     if num % 2 == 0:

     print(num," is even ")

One may observe that this function has only 1 parameter that is 'num' whose value is to be tested to know whether it is even or odd . But in case , the 'num' is divisible by 2 , then one can say that the number within the parameter is an even number or else the number is an odd number .

Here , '%' operator is called the modulus operator that gives the remainder of the division of the given numbers .And , if the remainder is 0 , then we can display that 'num' is an even number . This logic is used in an even_odd() function .

=========================================================================

Program - Write a program to test whether a number is even or odd

def test_even_odd(num):

     """ to know whether a number is even/odd """

     if num % 2 == 0:

     print(num," is an even number ")

     else:

     print(num," is an odd number ")

     # call the function

     test_even_odd(9)

     test_even_odd(16)

     #Output

     9 is an odd number

    16 is an even number


=========================================================================




No comments:

Post a Comment