<< 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.

Questions & Answers

What is diseconomic
Alixe Reply
how can price determination be the central problem of micro economics
simon Reply
marginal cost formula
Nandu Reply
you should differentiate the total cost function in order to get marginal cost function then you can get marginal cost from it
What about total cost
how can price determination be the central problem if micro economics
formula of cross elasticity of demand
Theresia Reply
what is ceteris paribus
Priyanka Reply
what is ceteris parabus
Ceteris paribus - Literally, "other things being equal"; usually used in economics to indicate that all variables except the ones specified are assumed not to change.
What is broker
land is natural resources that is made by nature
What is broker
what is land
What is broker
land is natural resources that is made by nature
whats poppina nigga turn it up for a minute get it
amarsyaheed Reply
what is this?
am from nigeria@ pilo
am from nigeria@ pilo
what is production possibility frontier
it's a summary of opportunity cost depicted on a curve.
please help me solve this question with the aid of appropriate diagrams explain how each of the following changes will affect the market price and quantity of bread 1. A
Manuela Reply
please l need past question about economics
Prosper Reply
ok let me know some of the questions please.
ok am not wit some if den nw buh by tommorow I shall get Dem
Hi guys can I get Adam Smith's WEALTH OF NATIONS fo sale?
hello I'm Babaisa alhaji Mustapha. I'm studying Economics in the university of Maiduguri
my name is faisal Yahaya. i studied economics at Kaduna state university before proceeding to West African union university benin republic for masters
Hi guys..I am from Bangladesh..
Wat d meaning of management
igwe Reply
disaster management cycle
Gogul Reply
cooperate social responsibility
Fedric Wilson Taylor also define management as the act of knowing what to do and seeing that it is done in the best and cheapest way
difference between microeconomics and macroeconomic
Ugyen Reply
microeconomics is the study of individual units, firm and government while macroeconomics is the study of the economic aggregates.
The classical theory of full employment
what is monopoli power
Adzaho Reply
the situation that prevails when economic forces balance so that economic variables neither increase nor decrease
what is equilibrium
what are the important of economic to accounting students with references
salihu Reply
Economics is important because it helps people understand how a variety of factors work with and against each other to control how resources such as labor and capital get used, and how inflation, supply, demand, interest rates and other factors determine how much you pay for goods and services.
explain the steps taken by the government in developing rural market?
Azeem Reply
government provide good road for than
government should provide good agricultural project and it should also provide good road so that the the product that will come out of the farm will be easy transport to the market
farming equipments should be provided to farmers to help them improve in farming
improving the transport systems providing enterpreneur edecation to the mass living in rural zones enforcment of loans and capital for the people rising awareness on the advantages of rural areas
contribution of Adam smith in economics
abel Reply
I will join
I will join
I love this book and i need extra Economic book
what's happening here
I love this book and i need extra Economic book
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?