If a function requires data to perform its task or returns multiple values. There should be a mechanism to pass on values to a function or to get back multiple values. Parameter list comes in play. These parameter list is located in parenthesis following the function name. The parameter list may contain any number of parameters, including none at all to indicate that a function does not require any parameters.

Each parameter should specify a type and an identifier. A function can specify multiple parameters by separating each parameter from the next with a comma.

e.g.,  drawLine(char k, int n)

The number of arguments in a function call to a function must be the same as the number of parameter listed in the function prototype. The order of arguments in the lists determines correspondence. The first argument corresponds to the first parameter, the second corresponds to the second parameter, and so and so. Each argument must be of a data type that can be assigned to the corresponding formal parameter with no un-expected loss of information.

Each time a function call is executed, an area of memory is allocated for storage of that function’s data. Function area contains storage cells for the parameters and any local variable that may be declared in the function. The function area is always lost when function is terminated. It is recreated empty when the function is called again.

Scope of a name refers to the region in a program where a particular meaning of a name is visible or can be referenced / accessible. The scope of a function begins with its proto-type and continues to the end of source file.

Value and Reference Parameters

At this point we know that how to write a function that return up to one result. Can we return multiple values from a function? Output parameters come in play, that can help us to return more than one values from a function. Suppose a function that accept two numbers and returns their sum and average.

  void computeSumAvg(x,  y,  sum,  mean)

The values of x and y are passed into the function when it is first called. Sum and average are calculated by the following expression in the body of function computeSumAvg.

sum = num1 + num2;
average = sum / 2.0;

The function should returns both sum and average.

Call-by-Value & Call-by-Reference

In C++, insert symbol & immediately following the type of a formal parameter  to declare an output parameter.

void computeSumAvg(float num1, float num2, float& sum, float& average)
 sum = num1 + num2;
 average = sum / 2.0;

Call-by-Value: An argument passing mechanism in which the value of an argument is stored in the called function’s data area. C++ compiler uses this mechanism for all input parameters.

Call-by-Reference:  An argument passing mechanism in which the address of an argument is stored in the called function’s data area. C++ compiler uses this mechanism for all output parameters.

If information is to be passed into a function and does not have to be returned or passed out of the function, parameter representing this information should be value parameter like num1, and num2 in previous demonstration – also called input parameters.

If information is to be returned to the calling function through parameter, then the parameter representing this information should be a reference parameter (sum and average) – also called output parameter.

If information is to be passed into a function, perhaps modified, and a new value returned,  the formal parameter representing this information must be a reference parameter – also called input/output parameter

Practice Problem


A company wants to transmit data over the telephone, but is concerned that it phone could be tapped. All of the data are transmitted as four digit integers. The company has asked you to write a program that encrypts the data so that it can be transmitted more securely. Your program should read a four digit integer and encrypt it as follows:

Algorithm for encryption

Replace each digit by (the sum of that digit plus 7) modulus 10. then swap the first digit with the 3rd, swap 2nd with the 4th and then print the encrypted integer.



Tagged with: C/C++ language

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.