11. Functions in C

Experience has shown that the best way to develop and maintain a large program is to construct it from smaller pieces or modules, each of which is more manageable than the original program. This technique is called divide and conquer. Modules in C are called functions. C programs are typically written by combining new functions you write with “prepackaged” functions available in the C Standard Library.

The C Standard Library provides a rich collection of functions for performing common mathematical calculations, string manipulations, character manipulations, input/output, and many other useful operations. This makes your job easier, because these functions provide many of the capabilities you need. You can write functions to define specific tasks that may be used at many points in a program. These are sometimes referred to as programmer-defined functions.

The actual statements defining the function are written only once, and the statements are hidden from other functions. Functions are invoked by a function call, which specifies the function name and provides information (as arguments) that the called function needs to perform its designated task.

Function definition:

return-value-type function-name( parameter-list ) {
definitions statements
}
  • The function-name is any valid identifier.
  • The return-value-type is the data type of the result returned to the caller. The return-value-type void indicates that a function does not return a value.
  • A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.
  • Statements is function body contains a collection of statements that define what the function does.
  • Together, the return-value-type, function-name and parameter-list are sometimes referred to as the function header.

Example:

/*Finding the max number*/
#include <stdio.h>
int main(int x, int y, int z);
/*function prototype*/
int main(void){
int number1; /* first integer */
int number2; /* second integer */
int number3; /* third integer*/
printf( "Enter three integers: " );
scanf( "%d%d%d", &number1, &number2, &number3 ); /* number1, number2 and number3 are arguments 18 to the maximum function call */
printf( "Maximum is: %d\n", );
return 0; /* indicates successful termination */
} /* end main */
/* Function maximum definition */
/* x, y and z are parameters */
int maximum( int x, int y, int z ) {
int max = x; /* assume x is largest */
if ( y > max ) {
/* if y is larger than max, assign y to max */ max = y;
} /* end if */
if ( z > max ) {
/* if z is larger than max, assign z to max */ max = z;
} /* end if */
return max; /* max is largest value */
} /* end function maximum */

 

Function prototype:

One of the most important features of C is the function prototype. This feature was borrowed by the C standard committee from the developers of C++. A function prototype tells the compiler the type of data returned by the function, the number of parameters the function expects to receive, the types of the parameters, and the order in which these parameters are expected. The compiler uses function prototypes to validate function calls. Previous versions of C did not perform this kind of checking, so it was possible to call functions improperly without the compiler detecting the errors. Such calls could result in fatal execution-time errors or nonfatal errors that caused subtle, difficult-to-detect logic errors. Function prototypes correct this deficiency.

Calling function by value and by reference:

There are two ways to invoke functions in many programming languages—call-by-value and call-by-reference. When arguments are passed by value, a copy of the argument’s value is made and passed to the called function. Changes to the copy do not affect an original variable’s value in the caller. When an argument is passed by reference, the caller allows the called function to modify the original variable’s value. Call-by-value should be used whenever the called function does not need to modify the value of the caller’s original variable. This prevents the accidental side effects (variable modifications) that so greatly hinder the development of correct and reliable software systems. Call-by-reference should be used only with trusted called functions that need to modify the original variable.

Recursion:

A function that calls itself is known as recursive function. And, this technique is known as recursion.

Questions