<< Chapter < Page Chapter >> Page >

You can hide a function from other source files. If you declare a function as static, its name identifies it only within the source file containing the function definition. Because the name of a static function is not an external identifier, you cannot use it in other source files. If you try to call such a function by its name in another source file, the linker will issue an error message, or the function call might refer to a different function with the same name elsewhere in the program.

The function printArray( ) in the following listing might well be defined using static because it is a special-purpose helper function, providing formatted output of an array of float variables.

// The static function printArray( ) prints the elements of an array // of float to standard output, using printf( ) to format them.// Arguments: An array of float, and its length. // Return value: None.static void printArray( const float array[ ], int n ){ for ( int i=0; i<n; ++i ) {printf( "%12.2f", array[i] ); // Field width: 12; decimal places: 2if ( i % 5 == 4 ) putchar( '\n' );// New line after every 5 numbers }if ( n % 5 != 0 ) putchar( '\n' ); // New line at the end of the output }

If your program contains a call to the printArray() function before its definition, you must first declare it using the static keyword:

static void printArray(const float [ ], int);int main( ) {float farray[123];/* ... */ printArray( farray, 123 );/* ... */ }

Function prototype

A function prototype in C++ is a declaration of a function that omits the function body but does specify the function's name, arity, argument types and return type. While a function definition specifies what a function does, a function prototype can be thought of as specifying its interface. Just like a blueprint, the prototype tells the compiler what the function will return, what the function will be called, as well as what arguments the function can be passed. The general format for a prototype is simple:

type function_name ( arg_type arg1, ..., arg_type argN );

arg_type just means the type for each argument -- for instance, an int, a float, or a char. It's exactly the same thing as what you would put if you were declaring a variable.

There can be more than one argument passed to a function or none at all (where the parentheses are empty), and it does not have to return a value. Functions that do not return values have a return type of void. Lets look at a function prototype:

int mult ( int x, int y );

This prototype specifies that the function mult will accept two arguments, both integers, and that it will return an integer. Do not forget the trailing semi-colon. Without it, the compiler will probably think that you are trying to write the actual definition of the function.

When the programmer actually defines the function, it will begin with the prototype, minus the semi-colon. Then there should always be a block with the code that the function is to execute, just as you would write it for the main function. Any of the arguments passed to the function can be used as if they were declared in the block.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to computer science. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10776/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?

Ask