Search code examples
pythonpython-3.xreturn

When I run my code, nothing is returned, yet there are no errors and I called to the methods


I called all the methods so I'm not sure why they won't return anything?

this is my attempt:

train_mass = 22680
train_acceleration = 10
bomb_mass = 1

def f_to_c(f_temp):
  c_temp = (f_temp - 32) * 5/9
  return c_temp
f100_in_celsius = f_to_c(100)
def c_to_f(c_temp):
  f_temp = c_temp * (9/5) +32
  return f_temp
c0_in_fahrenheight = c_to_f(0)

def get_force(mass, acceleration):
  return mass*acceleration
train_force = get_force(train_mass, train_acceleration)

def get_energy(mass, c=3*10**8):
  return mass * c**2
bomb_energy = get_energy(bomb_mass)

I was expecting the values in the method to be returned but nothing gets returned. Also, there were no errors.


Solution

  • From Python Principles:

    Print vs. return in Python

    Printing and returning are completely different concepts.

    print is a function you call. Calling print will immediately make your program write out text for you to see. Use print when you want to show a value to a human.

    return is a keyword. When a return statement is reached, Python will stop the execution of the current function, sending a value out to where the function was called. Use return when you want to send a value from one point in your code to another.

    Using return changes the flow of the program. Using print does not.