Difference between void and float function in C

Viewed 129

I am doing a Udemy C course and some doubts have arisen regarding the functions in C. One of the exercises it's about temperature conversion (celsius, F and K) using functions and when my first thought was that:

void CF(){
    float temp1, temp2;

    printf("Escribe que temperatura quieres convertir a Fahrenheit: ");
    scanf("%f", &temp1);

    temp2 = (temp1 * 1.8) + 32;

    printf("La temperatura en Fahrenheit es: %f", temp2);
}

But the solved exercise uses:

float fahrenheit(float C){
    float F=0;
    F = (9*C)/5 + 32;
    return F;
}

And entering data through "int main" program while I introduce through the function.

My questions are: - It's better to introduce data through int main code or the function? - Why he uses "Float" function and I use "void" function and works fine in both cases?

Anyway, my code works, but I want to know what is better and why.

Thanks in advance and forgive me for my english.

4 Answers

The purpose of a function is to encapsulate a frequently used calculation. If you code it as returning a value, then you can call it whenever you want inside a bigger program, regardless of whether you want to print out the result or not. Thus the second function is more reusable.

While there is nothing wrong with the way you wrote the function, it assumes that you sit by a keyboard to enter a value, and that you will just want to look at the conversion result.

Now imagine you program a tool which takes a list of temperatures in Celsius (from a spreadsheet), and wants to convert them all into Fahrenheit. Your function wouldn't work here, but the second version can be used with a wrapper around it to read the Celsius value from the spreadsheet, call the function, and then put the converted value somewhere else.

In general it's a good design principle to keep the functionality of a function to a minimum, so that it can be used in more different circumstances.

My questions are: - It's better to introduce data through int main code or the function? - Why he uses "Float" function and I use "void" function and works fine in both cases?

It's better to use the latter (function that returns float).

Why?

Because it makes the code more re-usable.

Your function might be working fine but what if you just want to convert from Fahrenheit to C without the user entering it?

Basically what your function does is:

  • print to the console
  • scan user input
  • do the actual conversion
  • output the result

So you see, in the first function you are doing more than just a conversion.

For me, if i can choose, i will write the code as second option (return float value). That because it's shorter, more portable, and reusable. For example, in the case you have to call the function many times:

for(int i = 0; i < 10000; i++) {
    float F = fahrenheit(i*1.0);
}

If you use the first solution, you have to enter 10000 times. it's so hard.

The first function (your solution) is not a function that converts a value to another, it displays information on screen, waits for user input, converts the value and prints the result. That function not only converts but also does many other things (input/output).

It is much more specific than the second function which given a value, converts it to another, and that's all. That second function is much more reusable than the first, you can use it everywhere you need to make a conversion! Then the first function can be rewritten with the second-like one:

/*
 * General purpose function that converts a celsius value into a fahrenheit value.
*/
float fahrenheit(float C) {
    float F=0;
    F = C*1.8+32;
    return F;
}

void CF(){
    float temp1, temp2;

    printf("Escribe que temperatura quieres convertir a Fahrenheit: ");
    scanf("%f", &temp1);

    temp2 = fahrenheit(temp1); // convert temp1 to fahrenheit

    printf("La temperatura en Fahrenheit es: %f", temp2);
}

The fahrenheit function is then usable as you may use sin or cos, etc. It is the computational description of the mathematical function that converts celsius to fahrenheit.

Related