How to use the return of a function in another Python function

Asked

Viewed 487 times

2

I’m new in Python and I’m not being able to use a return of a function in another function in the sequence:

I’m putting a fictional example just to describe my doubt:

def calcular_velocidade(distancia, tempo):
    velocidade = distancia / tempo
    return velocidade

def calcular_aceleracao(velocidade, tempo):
    aceleracao = velocidade / tempo
    return aceleracao

calcular_velocidade(20, 2)
calcular_aceleracao(velocidade, 2)

How do I use variable value velocidade of function calcular_velocidade in place of the parameter velocidade of function calcular_aceleracao ?

  • Detail, in the first function you could simply put return distancia / tempo. If the variable velocidade will not be used for anything else and will only be returned, it is unnecessary. The same goes for aceleracao in the second function.

  • @hkotsubo well noted! Thank you very much for the help.

2 answers

3

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.

  • Daniel thank you very much for the very clear explanation and rich information. I hope your explanation reaches other IT colleagues who may have the same doubt. Abs.

2

To do this, it is not enough to create a variable within the function, since you would be creating a local and not global variable (see this reply who speaks on the subject).

To use the function return within the call of another function, you must create a variable that receives the return of the first function to be used in the call of the second function.

See below how it would look:

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)
calcular_aceleracao(velocidade, 2)

Another way that is not so recommended since sometimes it can make your code messy and difficult to read, is by calling the first function within the call of the second function.

# Perceba como ficou um pouco mais difícil de ler o código.
calcular_aceleracao(calcular_velocidade(20, 2), 2) 
  • 1

    Jean thanks so much for the help! Excellent explanations that can help several colleagues in IT.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.