It’s very simple, just call the function calcular_velocidade
as a function parameter calcular_aceleracao
:
calcular_aceleracao( calcular_velocidade(20, 2) , 2)
Your example would look like this:
def calcular_velocidade(distancia, tempo):
velocidade = distancia / tempo
return velocidade
def calcular_aceleracao(velocidade, tempo):
aceleracao = velocidade / tempo
return aceleracao
aceleracao = calcular_aceleracao(calcular_velocidade(20, 2), 2)
print(aceleracao)
See online: https://repl.it/repls/CompleteGrownDaemons
It is also possible to declare a variable and thus store the function return calcular_velocidade
, then using the variable declared as argument for the function calcular_aceleracao
:
velocidade = calcular_velocidade(20, 2)
aceleracao = calcular_aceleracao(velocidade, 2)
Thus remaining as follows:
def calcular_velocidade(distancia, tempo):
velocidade = distancia / tempo
return velocidade
def calcular_aceleracao(velocidade, tempo):
aceleracao = velocidade / tempo
return aceleracao
velocidade = calcular_velocidade(20, 2)
aceleracao = calcular_aceleracao(velocidade, 2)
print(aceleracao)
See online: https://repl.it/repls/CraftyTrustworthyDehardwarization
Note that the result is exactly the same, they are different ways of working. In case you need to use the result of calcular_velocidade
more than once, declaring the variable may be a better option to avoid calls from the same function.
Detail, in the first function you could simply put
return distancia / tempo
. If the variablevelocidade
will not be used for anything else and will only be returned, it is unnecessary. The same goes foraceleracao
in the second function.– hkotsubo
@hkotsubo well noted! Thank you very much for the help.
– jaysonbarretti