answersLogoWhite

0

In C++ there is no such thing as a parameter, there are only arguments, both actual and formal. Some languages use the term parameter to mean a formal argument and argument to mean an actual argument, while others reverse the meanings completely. Some languages make no distinction at all and use the terms parameter and argument interchangeably. However, C++ is quite clear on this: actual arguments are the names that you pass to a function, while formal arguments are the names received by the function. Even so, you will still encounter incorrect usage of the terms parameter and arguments, even by C++ experts (myself included!)

The following example code demonstrates the difference between an actual argument and a formal argument, as the terms apply in C++:

int foo(int formal)

{

return(formal*2);

}

void bar(int& formal)

{

formal*=2;

}

int main()

{

int actual=1;

actual = foo(actual);

bar(actual);

return(0);

}

The function foo() declares a formal argument by value while bar() declares a formal argument by reference. In main() we declare a variable with the name actual and pass this actual argument to both functions in turn.

When we pass actual to foo(), the value of actual is assigned to formal. Since formal is a copy of actual, they are separate names with separate values (initially they will have the same value of course). Thus any changes made to formal will have no effect on actual, hence we must assign the return value from foo() to actual in main(), in order to record the change made by foo().

When we pass actual to bar(), a reference to actual is assigned to formal. A reference is simply an alternate name for the same argument, however the name actual is not visible to bar(), so they are still separate names, but they always have the same value. Thus any changes to formal will affect actual, thus there is no need to assign any return value to record the change.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

Explain the difference between salary and wage and give example to illustrate who earns salary and who earns wage?

What is the difference between a salary and commission


Different parameter passing methods with examples?

explain parameter passing methods c program


Explain ICMP and its error messages?

4-parameter.


What is fet and parameter of fet?

explain all the parameters of fet


Explain and illustrate insertion sort algorithm to short a list of n numburs?

Explain and illustrate insertion sort algorithm to short a list of n numburs


What is Call by value explain with example?

Call by value is a parameter passing mechanism in programming where a copy of the actual value of an argument is passed to a function. This means that changes made to the parameter within the function do not affect the original argument. For example, in the following Python code: def add_five(num): num += 5 return num result = add_five(10) print(result) # Outputs: 15 print(10) # Outputs: 10 (original argument remains unchanged) Here, num receives a copy of the value 10, and modifying num does not affect the original value.


Illustrate the communication process and explain?

communication process


Illustrate the process of communication and explain?

communication process


Explain and illustrate calibration?

Adjusting the settings to get the best result


Can you Explain how Darwin's finches illustrate the principle of character displacement?

no


What is a synonym for the word explicate?

amplify, clarify, develop, explain, illustrate


What is a synonym for describe?

portray explain depict illustrate express tell