0% found this document useful (0 votes)
192 views

Functions

A function is declared with a prototype containing the return type, name, and parameter list. The function definition includes the prototype and function body, which is a block of code enclosed in curly braces. Prototypes declare the data types of return values and parameters for error checking, while definitions create the actual function in memory. Examples show functions defined with different return types and parameters to perform tasks like finding the maximum number, printing values, and converting between Fahrenheit and Celsius temperatures.

Uploaded by

api-3824412
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views

Functions

A function is declared with a prototype containing the return type, name, and parameter list. The function definition includes the prototype and function body, which is a block of code enclosed in curly braces. Prototypes declare the data types of return values and parameters for error checking, while definitions create the actual function in memory. Examples show functions defined with different return types and parameters to perform tasks like finding the maximum number, printing values, and converting between Fahrenheit and Celsius temperatures.

Uploaded by

api-3824412
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Defining and Declaring Functions

A function is declared with a prototype. The function prototype, which has been seen
in previous lessons, consists of the return type, a function name and a parameter
list. The function prototype is also called the function declaration. Here are some
examples of prototypes.

return_type function_name(list of parameters);

int max(int n1, int n2); /* A programmer-defined function */


int printf(const char *format,...); /* From the standard library */
int fputs(const char *buff, File *fp); /* From the standard library */

The function definition consist of the prototype and a function body, which is a block
of code enclosed in parenthesis. A declaration or prototype is a way of telling the
compiler the data types of the any return value and of any parameters, so it can
perform error checking. The definition creates the actual function in memory. Here
are some examples of functions.

int FindMax(int n1, int n2)


{
if (n1 > n2)
{
return n1;
}
else
{
return n2;
}
}

void PrintMax(int someNumber)


{
printf("The max is %d\n",someNumber);
}

void PrintHW()
{
printf("Hello World\n");
}

float FtoC(float faren)


{
float factor = 5./9.;
float freezing = 32.0;
float celsius;

celsius = factor * (faren - freezing);


return celsius;
}

You might also like