Previous Index Next

Value-Returning Functions

A value returning function is a function that returns a value to the caller. The return value can be any Python object, such as a number, string, list, or dictionary.

To use a value returning function, you first need to define the function. The function definition includes the function name, the arguments, and the return statement.

Here is an example of a function that returns the sum of two numbers:

def sum(a, b):
    return a + b

To call the function, you use the function name followed by the arguments. For example, to call the sum function with the arguments 1 and 2, you would use the following code:

result = sum(1, 2)

The result variable will now contain the value of the function call, which is 3. You can print the value stored in the result variable to see the output.

print(result)  # Output: 3

Let's consider another example of a value-returning function that calculates and returns the factorial of a number. Please read the code carefully to understand how it works.

def factorial(number):
    fact = 1

    for i in range(1,number+1):
        fact = fact * i

    return fact

n = 5
f = factorial(n)
print('Factorial of', n,'is', f)

Output

Factorial of 5 is 120

Functions can be used to solve more difficult problems. Take a look at the given question, examine the solution code, and notice how functions help make the code reusable and modular, which is useful for tackling complex problems.

question

def factorial(n):
    fact = 1

    for i in range(1, n+1):
        fact = fact * i
    return fact

def equation(x, n):
    sum = 0
    
    for i in range(1, n+1):
        sum += x**i / factorial(i)   
    return sum
    
x = int(input('Enter the value of x: '))
n = int(input('Enter the number of terms: '))
result = equation(x, n)
print('The sum of series is', result)

Output

Enter the value of x: 2
Enter the number of terms: 4
The sum of series is 6.0

By defining the factorial and equation functions, the code breaks down the problem into smaller, focused tasks. The factorial function calculates the factorial of a number, and the equation function performs a specific equation to compute the sum of a series. The factorial function can be used whenever factorial calculations are needed.

Function Can Return Multiple Values in Python

In Python, functions are not limited to returning only a single value. They can also return multiple values, which can be useful in various scenarios.

def perform_math_operations(x, y):
  # Perform various mathematical operations on the given numbers
  addition = x + y
  subtraction = x - y
  multiplication = x * y
  division = x / y

  # Return the results as a tuple
  return addition, subtraction, multiplication, division

# Call the function to perform math operations
result_add, result_sub, result_mul, result_div = perform_math_operations(10, 5)

# Print the results
print("Addition:", result_add)
print("Subtraction:", result_sub)
print("Multiplication:", result_mul)
print("Division:", result_div)

The perform_math_operations() function calculates the addition, subtraction, multiplication, and division of two numbers x and y. It returns these results as a tuple. When calling the function and storing the returned values in separate variables, we can print and display the individual results.

Default Return Value in Functions - None

When working with functions in programming, it's important to understand the concept of the default return value. Let's consider the following function definition in Python:

def greet():
  print("Hello, world!")

In this function, there is no explicit return statement. When this function is called and assigned to a variable, like this:

result = greet()

The variable result will store the value returned by the function. However, since there is no return statement in the function, it automatically returns a special value called None. This means the function executes the code inside it, which is printing "Hello, world!" to the console, but it doesn't provide a specific value to be assigned to result.

To verify what value is stored in result, you can print it:

print(result)

The output will be None. This is because the function executed successfully but didn't explicitly return any value, resulting in the default return value of None.

To summarize, in Python, every function returns a value, even if you don't use the return statement. If a function lacks a return statement, it automatically returns None by default. Therefore, in the given example, when the greet() function is called and assigned to result, it stores None as the return value.

Previous Index Next