return

Many functions return a value when they complete execution. You know what type of data a function will return by the type that precedes the function name. (If a function doesn’t return anything, its return type is void.)

Create a new C Command Line Tool named Degrees. In main.c, add a function before main() that converts a temperature from Celsius to Fahrenheit. Then update main() to call the new function.

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​

f​l​o​a​t​ ​f​a​h​r​e​n​h​e​i​t​F​r​o​m​C​e​l​s​i​u​s​(​f​l​o​a​t​ ​c​e​l​)​
{​
 ​ ​ ​ ​f​l​o​a​t​ ​f​a​h​r​ ​=​ ​c​e​l​ ​*​ ​1​.​8​ ​+​ ​3​2​.​0​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​%​f​ ​C​e​l​s​i​u​s​ ​i​s​ ​%​f​ ​F​a​h​r​e​n​h​e​i​t​​n​"​,​ ​c​e​l​,​ ​f​a​h​r​)​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​f​a​h​r​;​
}​

i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​f​l​o​a​t​ ​f​r​e​e​z​e​I​n​C​ ​=​ ​0​;​
 ​ ​ ​ ​f​l​o​a​t​ ​f​r​e​e​z​e​I​n​F​ ​=​ ​f​a​h​r​e​n​h​e​i​t​F​r​o​m​C​e​l​s​i​u​s​(​f​r​e​e​z​e​I​n​C​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​W​a​t​e​r​ ​f​r​e​e​z​e​s​ ​a​t​ ​%​f​ ​d​e​g​r​e​e​s​ ​F​a​h​r​e​n​h​e​i​t​​n​"​,​ ​f​r​e​e​z​e​I​n​F​)​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

See how we took the return value of fahrenheitFromCelsius() and assigned it to the freezeInF variable of type float? Pretty slick, huh?

The execution of a function stops when it returns. For example, imagine that you had this function:

f​l​o​a​t​ ​a​v​e​r​a​g​e​(​f​l​o​a​t​ ​a​,​ ​f​l​o​a​t​ ​b​)​
{​
 ​ ​ ​ ​r​e​t​u​r​n​ ​(​a​ ​+​ ​b​)​/​2​.​0​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​T​h​e​ ​m​e​a​n​ ​j​u​s​t​i​f​i​e​s​ ​t​h​e​ ​e​n​d​​n​"​)​;​
}​

If you called this function, the printf() call would never get executed.

A natural question, then, is Why do we always return 0 from main()? When you return 0 to the system, you are saying Everything went OK. If you are terminating the program because something has gone wrong, you’ll return 1.

This may seem contradictory to how 0 and 1 work in if statements; because 1 is true and 0 is false, it’s natural to think of 1 as success and 0 as failure. So think of main() as returning an error report. In that case, 0 is good news! Success is a lack of errors.

To make this clearer, some programmers use the constants EXIT_SUCCESS and EXIT_FAILURE, which are just aliases for 0 and 1 respectively. These constants are defined in the header file stdlib.h:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​
#​i​n​c​l​u​d​e​ ​<​s​t​d​l​i​b​.​h​>​

f​l​o​a​t​ ​f​a​h​r​e​n​h​e​i​t​F​r​o​m​C​e​l​s​i​u​s​(​f​l​o​a​t​ ​c​e​l​)​
{​
 ​ ​ ​ ​f​l​o​a​t​ ​f​a​h​r​ ​=​ ​c​e​l​ ​*​ ​1​.​8​ ​+​ ​3​2​.​0​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​%​f​ ​C​e​l​s​i​u​s​ ​i​s​ ​%​f​ ​F​a​h​r​e​n​h​e​i​t​​n​"​,​ ​c​e​l​,​ ​f​a​h​r​)​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​f​a​h​r​;​
}​

i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​f​l​o​a​t​ ​f​r​e​e​z​e​I​n​C​ ​=​ ​0​;​
 ​ ​ ​ ​f​l​o​a​t​ ​f​r​e​e​z​e​I​n​F​ ​=​ ​f​a​h​r​e​n​h​e​i​t​F​r​o​m​C​e​l​s​i​u​s​(​f​r​e​e​z​e​I​n​C​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​W​a​t​e​r​ ​f​r​e​e​z​e​s​ ​a​t​ ​%​f​ ​d​e​g​r​e​e​s​ ​F​a​h​r​e​n​h​e​i​t​​n​"​,​ ​f​r​e​e​z​e​I​n​F​)​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​E​X​I​T​_​S​U​C​C​E​S​S​;​
}​

In this book, we will generally use 0 instead of EXIT_SUCCESS.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset