learncpponline

Function Prototypes in C++

All the functions in C++ (which are used first and defined later in the program) need to be prototyped. Function prototypes are usually written at the beginning of the program, ahead of any programmer-defined functions (including main()). The general form of a function prototype is: data-type function_name(type 1 arg 1, type 2 arg 2, …, type n arg n); Where, data-type represents the data-type of the item that is returned by the function, function_name represents the function name, and type 1, type 2, …, type n represents the data-type of the arguments arg 1, arg 2, …, arg n. Notice that a function prototype resembles the first line of a function definition (though a function prototype ends with a semicolon). Advantages of Function Prototypes in C++: Use of function prototypes in C++ offers following advantages: Prototypes enables the compilers to provide stronger type checking Because of the use of prototypes,...

Accessing of Functions in C++

A function can be accessed (i.e., called) by specifying its name, followed by a list of arguments enclosed in parentheses and separated by commas as shown below. function_name(int a, int b); If the function call does not require any arguments, an empty pair of parentheses must follow the name of the function as shown below. function_name(); The function call may be a part of a single expression (such as an assignment statement), or it may be one of the operands within the more complex expression as shown below. int returnedvalue = function_name(); int c = 3 * function_name() + 5; The arguments appearing in the function call are referred to as actual arguments, in contrast to the formal arguments that appear in the first line of the function definition. In normal function call, there will be one actual argument for each formal argument. The actual arguments may be expressed as...

Defining of Functions in C++

A function definition has two principal components: the first line (including the argument declaration), and the body of the function In general, the first line can be written as: data-type function_name(type 1 arg 1, type 2 arg 2, …, type n arg n) where, data-type represents the data type of the item that is returned by the function, function_name represents the function name, and type 1, type 2, …, type n represents the data-type of the arguments arg 1, arg 2, …, arg n. The data-types are assumed to be of the type int if they are not shown explicitly. However, the omission of the data type is considered poor programming practice, even if the data items are integers. The arguments are called formal arguments, because they represent the names of the data items that are transferred into the function from the calling portion of the program. The names of...

goto statement in C++

The goto statement is used to alter the normal sequence of the program execution by transferring control to some other part of the program. In its general form, the goto statement is written as: goto label; Where label is an identifier that is used to label the target statement to which control will be transferred. Control may be transferred to any other statement within the program. The target statement must be labeled, and the label must be followed by a colon. Thus, the target statement will appear as: label: statement Each labeled statement within the program must have a unique label; i.e., no two statements can have the same label.

continue statement in C++

The continue statement is used to bypass the remainder of the current pass through a loop. The loop does not terminate when a continue statement is encountered. Rather, the remaining loop statements are skipped and the computation proceeds directly to the next pass through the loop. The continue statement can be included within a for, a while or a do-while statement. The continue statement is written simply as: continue; without any embedded expressions or statements. The continue is a keyword in the C++ program and the semicolon must be inserted after the continue statement.

break statement in C++

The break statement is used to terminate loops or to exit from a switch. It can be used within a for, a >code>while, a do-while or a switch statement. The break statement is written simply as: break; without any embedded expressions or statements. The break is a keyword in the C++ program and the semicolon must be inserted after the break statement. We have already seen the use of break statement within the example of switch statement (Click here). The break statement causes a transfer of control out of the entire switch statement, to the first statement following the switch statement.