Hi all,
I was going through a code at some site. Here is the code snippet in
whcih I have doubt:
#include <iostream.h>
#include <stdlib.h>
void fun (int);
int main()
{
int i =100;
fun(i);
return 0;
}
void fun (int =10)
{
}
I compiled this program and it compiled very well. What I dont
understand here is formal pameter list of function "fun" - "int =10".
Is this acceptible in C or C++? WHat does int = 10 mean? First time I
came across such statement. Can anyone please explain?
.
|
|
| User: "Ron Natalie" |
|
| Title: Re: int = 10 |
02 Sep 2006 04:09:57 PM |
|
|
wrote:
Hi all,
I was going through a code at some site. Here is the code snippet in
whcih I have doubt:
#include <iostream.h>
#include <stdlib.h>
void fun (int);
int main()
{
int i =100;
fun(i);
return 0;
}
void fun (int =10)
{
}
I compiled this program and it compiled very well. What I dont
understand here is formal pameter list of function "fun" - "int =10".
Is this acceptible in C or C++? WHat does int = 10 mean? First time I
came across such statement. Can anyone please explain?
There are two issues you are probably unfamiliar with.
First, you can omit the name of a parameter if you don't otherwise
need it. Since fun(int) doesn't use the parameter inside, you
get away with not specifying it.
Second, the = 10 is a default arg. This will substitute the value
10 for the parameter if omitted in the call. Note that the declaration
of the default argument MUST BE SEEN at the point of the call.
This means that you couldn't do just "fun()" in function main
above because you don't declare the default arg until afterward.
.
|
|
|
| User: "Fraser Ross fraserATmembers.v21.co.uk" |
|
| Title: Re: int = 10 |
03 Sep 2006 09:41:27 AM |
|
|
"Ron Natalie"
Second, the = 10 is a default arg. This will substitute the value
10 for the parameter if omitted in the call. Note that the
declaration
of the default argument MUST BE SEEN at the point of the call.
This means that you couldn't do just "fun()" in function main
above because you don't declare the default arg until afterward.
If it is declared at both places it would need more maintenance and give
opportunity for coding mistakes. If it isn't declared at the definition
the programmer might forget about it. At function calls it is rather a
hidden fact that a default argument is being used which makes the call
less self documenting. I don't use default arguments much for these
reasons.
Fraser.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
.
|
|
|
| User: "Pete Becker" |
|
| Title: Re: int = 10 |
03 Sep 2006 11:08:55 AM |
|
|
Fraser Ross wrote:
If it is declared at both places it would need more maintenance and give
opportunity for coding mistakes.
If it's declared at both places it's ill-formed. You can't repeat
default arguments.
.
|
|
|
|
|
|
| User: "Jack Applin" |
|
| Title: Re: int = 10 |
02 Sep 2006 02:22:49 PM |
|
|
bharath.donnipad wrote:
void fun (int =10)
{
}
This is a function with a single unnamed argument of type int.
Additionally, the argument has a default value of 10.
It's silly to give the argument a default value, because the argument
has no name, and hence can't use it, but there it is.
Here's what it would look like if you named the parameter:
void fun (int n=10)
(Yes, you could access the unnamed arguments using stdargs,
but that's not the case here.)
--
Jack Applin
.
|
|
|
| User: "Pete Becker" |
|
| Title: Re: int = 10 |
02 Sep 2006 05:38:27 PM |
|
|
Jack Applin wrote:
(Yes, you could access the unnamed arguments using stdargs,
but that's not the case here.)
Formally, you can't. Even if there were a named argument preceding it.
The variable-length argument list macros apply only to arguments passed
through an ellipsis.
.
|
|
|
|
|

|
Related Articles |
|
|