int = 10

  • Thread starter bharath.donnipad
  • Start date
B

bharath.donnipad

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?
 
J

Jack Applin

bharath.donnipad said:
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.)
 
R

Ron Natalie

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.
 
P

Pete Becker

Jack said:
(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.
 
F

Fraser Ross

"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.
 
P

Pete Becker

Fraser said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top