How to Create Functions that Return Values in C Programming - dummies

# How to Create Functions that Return Values in C Programming

A great majority of the C language functions return a value; that is, they generate something. Your code may not use the values, but they’re returned anyway.

A Function That Returns a Value illustrates a function that is sent a value and then returns another value. That’s the way most functions work, although some functions return values without necessarily receiving any. For example, getchar() returns input but doesn’t require any arguments. In A Tighter Version, the convert() function accepts a Fahrenheit value and returns its Celsius equivalent.

A FUNCTION THAT RETURNS A VALUE

```#include <stdio.h>
float convert(float f);
int main()
{
float temp_f,temp_c;
printf("Temperature in Fahrenheit: ");
scanf("%f",&temp_f);
temp_c = convert(temp_f);
printf("%.1fF is %.1fCn",temp_f,temp_c);
return(0);
}
float convert(float f)
{
float t;
t = (f - 32) / 1.8;
return(t);
}```

Line 3 in A Function That Returns a Value declares the convert() function’s prototype. The function requires a floating-point value and returns a floating-point value.

The convert() function is called in Line 11. Its return value is stored in variable temp_c on that same line. In Line 12, printf() displays the original value and the conversion. The .1f placeholder is used. It limits floating-point output to all numbers to the left of the decimal, but only one number to the right.

The convert() function begins at Line 16. It uses two variables: f contains the value passed to the function, a temperature in Fahrenheit. A local variable, t, is used to calculate the Celsius temperature value, declared at Line 18 and assigned by the formula on Line 20.

Line 20 converts the f Fahrenheit value into the t Celsius value. The parentheses surrounding f – 32 direct the compiler to perform that part of the calculation first and then divide the result by 1.8. If you omit the parentheses, 32 is divided by 1.8 first, which leads to an incorrect result.

The function’s result is sent back in Line 21 by using the return keyword.

Exercise 1: Type the source code from A Function That Returns a Value into your editor. Build and run.

Functions that return values can have that value stored in a variable, as shown in Line 11 of A Function That Returns a Value, or you can also use the value immediately. For example:

`printf("%.1fF is %.1fCn",temp_f,convert(temp_f));`

Exercise 2: Edit the source code from A Function That Returns a Value so that the convert() function is used immediately in the printf() function. Hint: That’s not the only line you need to fix up to make the change complete.

You may also notice that the convert() function itself has a redundant item. Do you really need the t variable in that function?

Exercise 3: Edit your source code from Exercise 2 again, this time paring out the t variable from the convert() function.

Honestly, you could simply eliminate the convert() function altogether because it’s only one line. Still, the benefit of a function like that one is that you can call it from anywhere in your code.

So rather than repeat the same thing over and over, and have to edit that repeated chunk of text over and over when something changes, you simply create a function. Such a thing is perfectly legitimate, and it’s done all the time in C.

A Tighter Version shows the final result for Exercise 3.

A TIGHTER VERSION

```#include <stdio.h>
float convert(float f);
int main()
{
float temp_f;
printf("Temperature in Fahrenheit: ");
scanf("%f",&temp_f);
printf("%.1fF is %.1fCn",temp_f,convert(temp_f));
return(0);
}
float convert(float f)
{
return(f - 32) / 1.8;
}```

The convert() function’s math is compressed to one line, so a temporary storage variable (t from Line 18 in A Function That Returns a Value) isn’t needed.