I'm relatively new to programming, and I've been wondering about a problem such as this. How can I think about decomposing the code into its constituent functions? I went the simplest route I could, just putting the formula in its own function. I believe this makes the code more readable, but there's also a part of me that wants to put the while
loop into a function as well.
Is there a good rule of thumb for separating my code into functions?
#include <stdio.h>
int fahr_to_celsius(int f);
/* print Farhenheit-Celsius table
for fahr = 0, 20, ..., 300 *;
use a function for conversion */
int main(void)
{
int fahr, celsius;
int lower, upper, step;
lower = 0; /* lower limit of the temperature table */
upper = 300; /* upper limit */
step = 20; /* step size */
fahr = lower;
while (fahr <= upper) {
celsius = fahr_to_celsius(fahr);
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
}
int fahr_to_celsius(int fahr)
{
int celsius = 5 * (fahr-32) / 9;
return celsius;
}