| C++ Functions Part I |
|
|
|
| Articles Reviews C++ | |
| Written by Juan Soulie | |
| Monday, 07 January 2008 | |
|
type name ( parameter1, parameter2, ...) { statements } where:
parameters (as many as needed): Each parameter consists of a data type specifier followed by an identifier, like any regular variable declaration (for example: int x) and which acts within the function as a regular local variable. They allow to pass arguments to the function when it is called. The different parameters are separated by commas. statements is the function's body. It is a block of statements surrounded by braces { }. Here you have the first function example: // function example
In order to examine this code, first of all remember something said at the beginning of this tutorial: a C++ program always begins its execution by the main function. So we will begin there. Function addition declares another local variable (int r), and by means of the expression r=a+b, it assigns to r the result of a plus b. Because the actual parameters passed for a and b are 5 and 3 respectively, the result is 8. The following line of code: return (r); finalizes function addition, and returns the control back to the function that called it in the first place (in this case, main). At this moment the program follows it regular course from the same point at which it was interrupted by the call to addition. But additionally, because the return statement in function addition specified a value: the content of variable r (return (r);), which at that moment had a value of 8. This value becomes the value of evaluating the function call. cout << "The result is " << z; That, as you may already expect, produces the printing of the result on the screen Scope of variables
Therefore, the scope of local variables is limited to the same block level in which they are declared. Nevertheless, we also have the possibility to declare global variables; These are visible from any point of the code, inside and outside all functions. In order to declare global variables you simply have to declare the variable outside any function or block; that means, directly in the body of the program. // function example
In this case we have created a function called subtraction. The only thing that this function does is to subtract both passed parameters and to return the result. z = subtraction (7,2); If we replace the function call by the value it returns (i.e., 5), we would have: z = 5; As well as
has the same result as the previous call, but in this case we made the call to subtraction directly as an insertion parameter for cout. Simply consider that the result is the same as if we had written: cout << "The second result is " << 5; since 5 is the value returned by subtraction (7,2). cout << "The third result is " << subtraction (x,y); The only new thing that we introduced is that the parameters of subtraction are variables instead of constants. That is perfectly valid. In this case the values passed to function subtraction are the values of x and y, that are 5 and 3 respectively, giving 2 as result. z = 4 + subtraction (x,y); we could have written: z = subtraction (x,y) + 4; with exactly the same result. I have switched places so you can see that the semicolon sign (;) goes at the end of the whole statement. It does not necessarily have to go right after the function call. The explanation might be once again that you imagine that a function can be replaced by its returned value: z = 4 + 2; Functions with no type. The use of void. type name ( argument1, argument2 ...) statement you will see that the declaration begins with a type, that is the type of the function itself (i.e., the type of the datum that will be returned by the function with the return statement). But what if we want to return no value? // void function example I'm a function! void can also be used in the function's parameter list to explicitly specify that we want the function to take no actual parameters when it is called. For example, function printmessage could have been declared as: void printmessage (void) Although it is optional to specify void in the parameter list. In C++, a parameter list can simply be left blank if we want a function with no parameters. printmessage (); The parentheses clearly indicate that this is a call to a function and not the name of a variable or some other C++ statement. The following call would have been incorrect: printmessage; Source of the article. C++ website documantation. Powered by jReviews |
|
| Last Updated ( Monday, 07 January 2008 ) | |
| < Prev | Next > |
|---|









