|
กก |
Using functions we can structure our programs in a more modular way, accessing to all the potential that structured programming in C++ can offer us.
A function is a block of instructions that is executed when it is called from some other
point of the program. The following is its format: type name ( argument1, argument2, ...) statementwhere: type is the type of data returned by the function. name is the name by which it will be possible to call the function. arguments (as many can be specified as necessary). Each argument consists of a type of data followed by a particular name, like in a variable declaration (for example, int x) and which acts within the function like any other variable. They serve to allow passing parameters to the function when this one is called. The different parameters are separated by commas. statement is the function's body. It can be a single instruction or a block of instructions. In latter case it must be delimited with key brackets signs {}. Here hou have the first 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 by there. We can see how the main function begins declaring the variable z of type int. Just after that we see a call to the addition function. If we pay attention we will be able to see the similarity between the structure of the call to the function and the declaration of the function some code lines above: The parameters have a clear correspondence. Within the main function we called to addition passing two values: 5 and 3 that correspond with the int a and int b parameters declared for the addition function. At the moment at which the function is called from main, the control is lost by main function and passed to function addition. The value of both parameters passed in the call (5 and 3) are copied to the local variables int a and int b within the function. The function addition declares a new variable (int r;), and by means of the expression r=a+b;, it assigns to r the result of a plus b. Because the passed parameters 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 has called it (main) following the program by the same point in which it was interrupted by the call to addition. But in addition, return was called within the function with the content of the variable r (return (r);), that was 8, so this value is the one returned by the function. The value returned by a function is the value given to the function when it is evaluated. Therefore, z will store the value returned by addition (5, 3), that is 8. To explain it somehow you can imagine that the call to a function (addition (5,3)) is literally replaced by the value it returns (8).
The following line of code in main is: cout << "The result is " << z;that, as you may already suppose, produces the printing of the result on the screen.
And here is another example about functions:
In this case we have created the function subtraction. The only thing that this function does is to subtract both passed parameters and to return the result. Nevertheless, if we examine the function main we will see that we have made several calls to function subtraction. We have used some different calling methods so that you see other ways or moments when a function can be called.
In order to understand well these examples you must consider once again that a call to a
function could perfectly be replaced by its return value. For example the first case,
that you should already know beacause it is the same pattern that we have used in
previous examples: z = subtraction (7,2);If we replace the function call by its result (that is 5), we would have: z = 5; As far as cout << "The second result is " << subtraction (7,2);has the same result that the previous call, but in this case we made the call to subtraction directly as a parameter for cout. Simply imagine that we had written: cout << "The second result is " << 5;since 5 is the result of subtraction (7,2).
In the case of cout << "The third result is " << subtraction (x,y);The only new thing that we introduce is that the parameters of subtraction are variables instead of constants. That is perfectly valid. In this case the values passed to the function subtraction are the values of x and y, that are 5 and 3 respectively, giving 2 as result.
The fourth case is more of the same. Simply to comment that instead of: z = 4 + subtraction (x,y);we could perfectly have put: z = subtraction (x,y) + 4;with exactly the same result. Notice that the semicolon sign (;) goes at the end of the whole expression. It does not necessarily have to go right after the function call. The explanation is once again that you imagine that a function can be replaced by its result: z = 4 + 2;
Functions with no types. Use of void.If you remember the syntax of a function declaration:type name ( argument1, argument2 ...) statementyou will see that it is obligatory that this declaration begins with a type, that is the type of the data that will be returned by the function with the return instruction. But what if we want to return no value? Imagine that we want to make a function just to show a message on the screen. We do not need that it returns any value, moreover, we either need to receive no parameters. For that was devised the void type in C language. Take a look at:
Although in C++ it is not necessary to specify void its use is considered suitable to stand out that it is a function without parameters or arguments and not anything else.
What you must always beware is that the format for calling a function includes to
specify its name and to enclose between parenthesis the arguments. The non-existence
of arguments do not exempts us from the obligation to put parenthesis, for that reason
the call to dummyfunction is dummyfunction ();This stands out that it is a call to a function and not the name of a variable or any other token.
| ||||||||||||||||