Define function. Why do we use function in a program ? What is the difference between C function and C++ functions ?

Define function. Why do we use function in a program ? What is the difference between C function and C++ functions ?

Ans.  A function a set program statement that can be processed independently. A function can be invoked which behaves as though its code is inserted at the point of the function call. The communication between a caller (calling function) and callee (called function) takes place through parameters. Functions are independent because variable names and labels defined within its body are local to it.

The various components associated with functions are as follows –

Void show(int a, int b);        //function declaration

Main ( )

{

………….

Show(x, y):                            //function call

………….

}

Void show(int a, int b)    //function definition

{

………….

………….                                   //function body

………….

}

The first statement Is the function declaration. This is the line before the beginning of main. It provides the following information to the compiler – (i) the name of the function (ii) the type of value returned and (iii) the number and the types of the arguments that must be supplied in a call to the function. Function prototyping is one of the key improvements added to the C++ functions. When a function call is encountered, the compiler checks the functions call with its prototype so that correct argument types are used. The compiler informs the user about any violation in the actual parameters that are to be passed to a function. The function declaration terminates with a semicolon. Function declarations are also called prototype, since they provide a model or blue print of the function.

The function itself is referred to as function definition. The first line of the function definition is known as function declaratory and is followed by the function body. Thus, the declaratory and the function body make up the function definition. The declaratory and declaration must use the same function name, the number of arguments, the arguments type and the return type. The body of the function is enclosed in braces. C++ allows the definition to be placed anywhere in the program. If the function is defined before its invocation, then its prototypes is optional.

A function is a dormant entity, which gets life only when a call to the function is made. A function call is specified by the function name followed by the argument enclosed in parentheses and terminated by a semicolon in the function call.

The parameters specified in the function call are known as actual parameters and those specified in the function declaratory are known as formal parameters and those specified in the function declaratory are known as formal parameter. When a function call is made, a one-to-one correspondence is established between the actual and the formal parameters. The scope of formal parameters is limited to its function only. When the function is called, control is transferred to the first statement in the function body. The other statements in the function body are then executed and control returns to the main program when the closing brace is encountered.

A function can or cannot return a value. It can occur anywhere in the function body and as soon as it is encountered, execution control will be returned to the caller.

The most important reason to use functions is to divide major programs in small functions. Another advantages of using functions is that it is possible to reduce the size of a program by calling and using them at different places in the program. Any sequence of instruction that appears in a program more than once is candidate for being made into a function. The function’s code is stored in only one place in memory, even though the function is executed many times in the course of the program. The use of functions offer flexibility in the design, development, and implementation of the program to solve complex problems.

Share this post