In Chapter 1, we covered function basics in the following sections:
You should be familiar with the concepts discussed in those lessons before proceeding.
Parameters vs Arguments
Up until now, we have not differentiated between function parameters and arguments. In common usage, the terms parameter and argument are often interchanged. However, for the purposes of further discussion, we will make a distinction between the two:
A function parameter is a variable declared in the prototype or declaration of a function:
void foo(int x); // prototype -- x is a parameter
void foo(int x) // declaration -- x is a parameter
{
}
An argument is the value that is passed to the function in place of a parameter:
foo(6); // 6 is the argument passed to parameter x foo(y+1); // the value of y+1 is the argument passed to parameter x
When a function is called, all of the parameters of the function are created as variables, and the value of the arguments are copied into the parameters. For example:
void foo(int x, int y)
{
}
foo(6, 7);
When foo() is called with arguments 6 and 7, foo’s parameter x is created and assigned the value of 6, and foo’s parameter y is created and assigned the value of 7.
Even though parameters are not declared inside the function block, function parameters have local scope. This means that they are created when the function is invoked, and are destroyed when the function block terminates:
void foo(int x, int y) // x and y are created here
{
} // x and y are destroyed here
There are 3 primary methods of passing arguments to functions: pass by value, pass by reference, and pass by address. The following sections will address each of those cases individually.
7.2 — Passing arguments by value
|
Index
|
6.13 — Void pointers
|
7.2 — Passing arguments by value
Index
6.13 — Void pointers
Alex -
If, by definition, “An argument is the value that is passed to the function”, then shouldn’t the last sentence of this lesson have started:
“There are 3 primary methods of passing arguments to functions:”?
[ Yep. Fixed. -Alex ]
[...] 2007 Prev/Next Posts « 7.1 — Function parameters and arguments | Home | 7.2 — Passing arguments by value » Thursday, July 19th, 2007 at 2:07 [...]
i want to know How to pass the HTML Tags as a parameter to the C++ Function and want to store the result in a XML Buffer.
Since terms are discussed, isn’t the prototype in fact the function declaration, and the declaration including the body the definition? Correct me, if I’m mistaken.
[...] 7.1 — Function parameters and arguments Print This Post This entry is filed under C++ Tutorial. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. 6 Responses to “7.2 — Passing arguments by value” Comment by Rory 2007-11-26 14:21:28 [...]
i dont understand about the calling a function..anybody can help me…
what are actual argument(actual parameter) and formal argument(formal parameter)?!
they use in some books!
what difference between argument and parameter?
please explain
Think of it like this: an argument is the giver and the paremeter is the reciever.
Actual parameters are nothing but the actual values/arguments you are passing during function call[ex: foo(6,7);].
Formal parameters are the parameters into function definition [ex: int foo(int i, int j)]
“When a function is called, all of the parameters of the function are created as variables, and the value of the arguments are copied into the parameters.”
The last bit of this sentence sounds awkward. Shouldn’t it be: “… copied into the variables.”?
Seeing as the paremeters are the variables anyway, it does that already, it’s just implied :).
This helped thanks
Thanks for the lessons!
They are amazingly well explained!
Congratulations!
Q.1 write a the c program using a function method to calculation the area and perimeter for the rectangle and the square by any given width and height . the program should ask the user to stop or retry the program with other values .
Q.2 write a program in c language using a function method to calculate the following series and give the result;
X= n^1*n^2*n^3*…………* n^k
Where n&k are any number given by the user
pls… help me
pls… help me
are you asking the full code here…. i better suggest try it your own …
if any problem you can paste the code guys will help you out.
@moon1212: are you asking the full code here…. i better suggest try it your own …
if any problem you can paste the code guys will help you out.
ok thank you
Thank you all salvation Halith