Functions within Functions
Inside the function, we can not only write loops, declare variables, use if-else
statements but also call other functions. Let's look at an example.
9
1
2
3
4
5
6
7
def function_1(): # Outer function
print('I am an outer function')
def function_2(): # Inner function
print('I am an inner function ')
function_2()
function_1()
1234567def function_1(): # Outer function print('I am an outer function') def function_2(): # Inner function print('I am an inner function ') function_2() function_1()
Tarea
Swipe to start coding
You have to implement two functions:
- outer_function - this is an outer function that displays text like this:
The perimeter of this square is equal to:
- inner_function - this is an inner function, which calculates the perimeter of a square whose side is
7
.
Solución
9
1
2
3
4
5
6
7
def outer_function():
print('The perimeter of this square is equal to:')
def inner_function(side_of_square):
perimeter = 4*side_of_square
print(perimeter)
inner_function(7)
outer_function()
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 5. Capítulo 1
99
1
2
3
4
5
6
7
8
9
10
11
# Write your code below
def ___():
print('___')
def ___(side_of_square):
# Calculate the perimeter
___ = 4*side_of_square
print(___)
inner_function(___)
# Testing
outer_function()
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla