-Although the question can apply to any language, I will set my examples in Python. Well, the question would be: how to prevent function calls from stacking and at the same time divide the program into parts, that is...
def hacer_algo():
print('Acabas de hacer algo')
def hacer_otra_cosa():
print('Acabas de hacer otra cosa')
def menu_inicial():
print("""
1) Hacer algo
2) Hacer otra cosa
3) Salir
""")
eleccion = int(input('-> '))
if eleccion == 1:
hacer_algo()
menu_inicial()
elif eleccion == 2:
hacer_otra_cosa()
menu_inicial()
elif eleccion == 3:
quit()
menu_inicial()
-Well, we can quickly realize that if we use this script for a while, the calls to the function would be massively stacked, in fact, this can be seen if, after a while, we force the program to exit (cntrl + c )...
File "/home/santiago/Escritorio/Programacion/Bloque de estudio de Python/Sintaxis Basica/Practicas/Base de datos Buena/pruebas2.py", line 21, in <module>
menu_inicial()
File "/home/santiago/Escritorio/Programacion/Bloque de estudio de Python/Sintaxis Basica/Practicas/Base de datos Buena/pruebas2.py", line 14, in menu_inicial
menu_inicial()
File "/home/santiago/Escritorio/Programacion/Bloque de estudio de Python/Sintaxis Basica/Practicas/Base de datos Buena/pruebas2.py", line 17, in menu_inicial
menu_inicial()
File "/home/santiago/Escritorio/Programacion/Bloque de estudio de Python/Sintaxis Basica/Practicas/Base de datos Buena/pruebas2.py", line 14, in menu_inicial
menu_inicial()
File "/home/santiago/Escritorio/Programacion/Bloque de estudio de Python/Sintaxis Basica/Practicas/Base de datos Buena/pruebas2.py", line 17, in menu_inicial
menu_inicial()
File "/home/santiago/Escritorio/Programacion/Bloque de estudio de Python/Sintaxis Basica/Practicas/Base de datos Buena/pruebas2.py", line 14, in menu_inicial
menu_inicial()
File "/home/santiago/Escritorio/Programacion/Bloque de estudio de Python/Sintaxis Basica/Practicas/Base de datos Buena/pruebas2.py", line 17, in menu_inicial
menu_inicial()
File "/home/santiago/Escritorio/Programacion/Bloque de estudio de Python/Sintaxis Basica/Practicas/Base de datos Buena/pruebas2.py", line 14, in menu_inicial
menu_inicial()
File "/home/santiago/Escritorio/Programacion/Bloque de estudio de Python/Sintaxis Basica/Practicas/Base de datos Buena/pruebas2.py", line 17, in menu_inicial
menu_inicial()
File "/home/santiago/Escritorio/Programacion/Bloque de estudio de Python/Sintaxis Basica/Practicas/Base de datos Buena/pruebas2.py", line 14, in menu_inicial
menu_inicial()
File "/home/santiago/Escritorio/Programacion/Bloque de estudio de Python/Sintaxis Basica/Practicas/Base de datos Buena/pruebas2.py", line 11, in menu_inicial
eleccion = int(input('-> '))
KeyboardInterrupt
-What is happening here is that, when calling a function inside another, the first one never ends, causing the calls to the function to 'stack', well, this is obviously an optimization problem that I don't know how to fix , for example, it is usually of interest to me that, once an option is selected, the program returns to menu_inicial()
, however, applying this methodology the calls to the function would be stacked, generating an optimization problem, but at the same time the use of goto
obviously does not is an option ... Any suggestion ?
You are using recursion when iterating is enough. Here is the converted code:
As you can see, the strategy is to enter an infinite loop. In each iteration you ask for an option, execute it and repeat everything again. When the chosen option is "exit", you do a
break
to escape the infinite loop and terminate execution.