In this tutorial, we will learn how to call a function within a function in Python. This is a useful technique in programming that helps to break down complex tasks into smaller, more manageable tasks.
This technique can lead to cleaner and more organized code that is easier to maintain and debug.
Step 1: Defining a Function
In Python, a function is defined using the def
keyword, followed by the function name, a pair of parentheses containing any input parameters, and a colon :
. The function body should be indented and the body should end with a return
statement to return the output of the function.
Here is an example of a simple function that adds two numbers:
1 2 3 |
def add_numbers(num1, num2): result = num1 + num2 return result |
Step 2: Calling a Function
To call a function, simply use the function name followed by a pair of parentheses containing any required arguments. The returned result can be stored in a variable for further use or printed directly.
Here is an example of calling the add_numbers
function defined earlier:
1 2 3 |
sum_result = add_numbers(2, 3) print(sum_result) # Output: 5 |
Step 3: Calling a Function Within a Function
Calling a function within a function is as simple as calling a function anywhere else in your Python code. In the body of one function, simply call another function using its name and input parameters.
Consider the following example, where we use the add_numbers
function within a new function called multiply_numbers
:
1 2 3 4 5 |
def multiply_numbers(num1, num2): result = num1 for i in range(1, num2): result = add_numbers(result, num1) return result |
In this example, we call the add_numbers
function within the multiply_numbers
function, effectively using it as a helper function.
To use this new function, simply call it as you would any other function:
1 2 3 |
product = multiply_numbers(3, 4) print(product) # Output: 12 |
Step 4: Example – Calculating the Power of a Number
In this example, we will create a function to calculate the power of a given number. We will use the multiply_numbers
function defined earlier to perform the calculations.
1 2 3 4 5 6 7 8 9 10 11 12 |
def power(base, exponent): result = base for i in range(1, exponent): result = multiply_numbers(result, base) return result base_number = 2 exponent_number = 3 power_result = power(base_number, exponent_number) print(power_result) # Output: 8 |
In this example, we define a new function called power
, which accepts two input parameters: base
and exponent
. Using a loop and the multiply_numbers
function, our power
function calculates and returns the result of raising base
to the power of exponent
.
Complete Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
def add_numbers(num1, num2): result = num1 + num2 return result def multiply_numbers(num1, num2): result = num1 for i in range(1, num2): result = add_numbers(result, num1) return result def power(base, exponent): result = base for i in range(1, exponent): result = multiply_numbers(result, base) return result base_number = 2 exponent_number = 3 power_result = power(base_number, exponent_number) print(power_result) # Output: 8 |
Conclusion
In this tutorial, we learned how to call a function within a function in Python. This technique is useful for breaking down complex tasks into smaller, more manageable tasks and results in cleaner and more organized code that is easier to maintain and debug.