Write a function that takes one argument-F for Fahrenheit-and computes the result of the following equation (converting the temperature to Celsius).

C = (F – 32) * (5 / 9)

______ tempConverter(float ________) {
  _____ _______ = ______________
  _____________________________
}

Answer:

void setup() {
  float temp = tempConverter(50);
  println(temp);
}

float tempConverter(float farenheit) { 
  float celsius = (farenheit - 32) * (5.0/9.0);
  return celsius;
} 
  • Aaron

    Quick question here -

    In the “Answer,” shouldn’t it read:

    float celsius = (farenheit – 32) * (5.0/9.0);

    I could be wrong, but just wanted to check. Thanks!

  • admin

    Yup, that’s right! Thanks, fixed now.

  • http://www.mkdesigns.net/ Mary

    I was tripped-up by the fractions needing to have a decimal point to work. Without the decimal point
    on the 5/9 equation I was returning a 0.

    I guess I should include a decimal on all floats just to be safe?

  • http://www.learningprocessing.com Daniel Shiffman

    Indeed, Processing will assume numbers are integers and you want an integer back unless you indicate a float with the decimal value (even if it is “point zero”).

  • Rhys Harris09

    This had me stumped for ages! In France with no Internet, now in a @Cafe and have found the answer! Phew! Thanks!

  • Chuckmahoney

    If you leave out the parentheses around the 5/9, then the calculation will work even if they are integers, due to the order of operations. It will first multiply by 5, then divide by 9. If you write it as above then yes, you must use floats.