by Dinesh Thakur Category: Functions

Auto

 

The values of the variables are not retained beyond the scope of a function in which they are declared. The values of the variables can be accessed only during the execution of a function. Whatever variables are declared in a function, they are assumed implicitly of auto storage type. For example in the following program segment

 
by Dinesh Thakur Category: Functions

Arguments in a function can also be of array type. When the arrays are passed by value in a function, it is written as shown in the following example:

 
by Dinesh Thakur Category: Functions

A recursive function is a function which invokes itself repeatedly. In this case function name appears within the function. Two examples of recursive function are given as follows:

 
by Dinesh Thakur Category: Functions

A function can also return values to the calling program. For such functions, the type of the value returned to the calling function should be mentioned before the function name in the definition. The general syntax of a function which returns value is:

 
by Dinesh Thakur Category: Functions

A function contains a set of statements which can be invoked from any part of the program. Function plays an important role whenever a sequence of instructions is to be repeated in a program. Thus it helps in reducing the size of the program. The function code is stored at only one place in the memory, though the function can be executed any number of times. Main () is also a function. Every program must contain the main () function. A function mayor may not return values to the calling function.

 
by Dinesh Thakur Category: Functions

In some cases when a similar action is to be performed on different types of data, different functions having different names are to be defined for all types of data. This makes the program very complex as the programmer must keep a track of the names of all the functions defined in the program. To prevent such situations, C++ allows the functions to be overloaded.

 
by Dinesh Thakur Category: Functions

Any value assigned to an automatic variable within a function is lost once the control returns from the called function to the calling function. However, there may be a situation where the value of the local variable needs to be preserved even after the execution of the function gets over. This need can be fulfilled by declaring the variable as static. A static variable is commonly used when it is necessary for a function to remember a value between its calls. To understand the concept of static variables, consider this example.

 
by Dinesh Thakur Category: Functions

Like C, C++ enables to pass arguments to the main () function also. These arguments are passed by typing them after the program name on the command line. Hence, these arguments are known as command line arguments. Command line arguments help in providing data to the program.

 
by Dinesh Thakur Category: Functions

In addition to call by value, a function can also be called by reference. C++ provides two ways of passing arguments as reference to a function. The arguments can be passed either as a reference type or as a pointer type.

 
by Dinesh Thakur Category: Functions

When a function is called by value, the values of the actual arguments are copied into the formal arguments and the function works with these copied values. As a result, any changes made to the copied value in the called function do not affect the value of the actual argument in the calling function.

 
by Dinesh Thakur Category: Functions

In order to use a function in different parts of a program, the function must be called or invoked by another function. In C++, functions are called by specifying the name of the function, followed by the parentheses. The parentheses mayor may not contain a list of arguments depending on the function definition.

 
by Dinesh Thakur Category: Functions

In order to use a function in a program, the function must be first defined somewhere in a program. A function definition contains the code that specifies the actions to be performed. The syntax for defining a function is

 
by Dinesh Thakur Category: Functions

Like variables, functions also need to be declared before they are used in programs. A function declaration is also known as function prototype. Function prototype is a model or a blueprint for a function that informs the C++ compiler about the return type, the function name, and the number and data type of the arguments passed to the function. Function name together with parameter list is known as function signature and it does not include return type of a function. The syntax for declaring a function is



 

Page 1 of 3



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles