15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

226<br />

Passing Parameters<br />

Defining a function in the C language looks like this:<br />

datatype function_name (parameter list)<br />

{<br />

commands<br />

return value<br />

}<br />

Inside the function, one or more commands must calculate a new result.<br />

Then you use the RETURN keyword to define what value to store in the function<br />

name. Whatever value this is, it must be the same data type that you<br />

defined <strong>for</strong> the function name in the first line. So if you defined a function as<br />

a String data type, you can’t return an integer value from that function.<br />

A typical function in BASIC might look like this:<br />

FUNCTION ConvertC2F (Temperature AS SINGLE) AS SINGLE<br />

Temperature = ((9/5) * Temperature) + 32<br />

RETURN Temperature<br />

END FUNCTION<br />

The function name ConvertC2F can hold a Single data type.<br />

Unlike a subprogram that may or may not return a modified value, functions<br />

always return a value. To call a function, you must assign the function name<br />

to a variable or use the function name itself as a variable, such as<br />

PRINT “Temperature in Fahrenheit = “; ConvertC2F (12)<br />

Because functions always return a value, they (almost always) have a parameter<br />

list. So you can identify functions in a program by looking <strong>for</strong> the parameter<br />

list in parentheses.<br />

DIM Temp AS SINGLE<br />

Temp = 12<br />

PRINT “Temperature in Celsius = “; Temp<br />

PRINT “Temperature in Fahrenheit = “; ConvertC2F (Temp)<br />

END<br />

FUNCTION ConvertC2F (Temperature AS SINGLE) AS SINGLE<br />

Temperature = ((9/5) * Temperature) + 32<br />

RETURN Temperature<br />

END FUNCTION<br />

Unlike a subprogram that you can call just by typing its name on a line by<br />

itself, you can call a function only by using that function name as if it’s a<br />

variable.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!