Default arguments

S

Sathyaish

How do you define an argument for a function such that the argument is
optional and has a default value assigned to it? I am sorry but I am
having memory lapses.
 
C

CBFalconer

Sathyaish said:
How do you define an argument for a function such that the argument
is optional and has a default value assigned to it? I am sorry but
I am having memory lapses.

You use some other language with that capability, such as Ada.
 
J

Jaspreet

Sathyaish said:
How do you define an argument for a function such that the argument is
optional and has a default value assigned to it? I am sorry but I am
having memory lapses.

int funcDefArgs(int a, int b = 4);

Remember to provide the default arguments only at the time of the decl.
of the function and **not** at definition.

Also, the rightmost arguments can be default. Soo, while 1. is wrong, 2
is prefectly fine.

1. int funcDefArgs(int a, int b = 4, int c);
2. int funcDefArgs(int a, int b = 3, int c = 2);

Try going to a pharmacist and ask for memory plus capsules. It might
help ;)
 
M

Michael Mair

Jaspreet said:
int funcDefArgs(int a, int b = 4);

Remember to provide the default arguments only at the time of the decl.
of the function and **not** at definition.

Also, the rightmost arguments can be default. Soo, while 1. is wrong, 2
is prefectly fine.

1. int funcDefArgs(int a, int b = 4, int c);
2. int funcDefArgs(int a, int b = 3, int c = 2);

Try going to a pharmacist and ask for memory plus capsules. It might
help ;)

And of course, this is a solution only for _Not_C_Language_.
It might be C++ but I cannot remember when in comp.lang.c;
comp.lang.c++ might know.


Cheers
Michael
 
J

Jaspreet

Michael said:
And of course, this is a solution only for _Not_C_Language_.
It might be C++ but I cannot remember when in comp.lang.c;
comp.lang.c++ might know.


Cheers
Michael

Sorry but I did not get that.
 
M

Michael Mair

Jaspreet said:
Sorry but I did not get that.

Sorry, my fault:
The short of it is that this is not possible in C, only in C++,
so your answer is wrong.

Cheers
Michael
 
J

Jaspreet

Michael Mair wrote:
[snip]
Sorry, my fault:
The short of it is that this is not possible in C, only in C++,
so your answer is wrong.

Cheers
Michael

Ah!! Yup apologies for posting a wrong solution.
 
A

Alan Balmer

int funcDefArgs(int a, int b = 4);

Remember to provide the default arguments only at the time of the decl.
of the function and **not** at definition.

Also, the rightmost arguments can be default. Soo, while 1. is wrong, 2
is prefectly fine.

1. int funcDefArgs(int a, int b = 4, int c);
2. int funcDefArgs(int a, int b = 3, int c = 2);
You need quotes around each of the arguments, like "int b = 4", and
don't forget that the prototype has to be something like
int funcDelArgs( char *s1, char *s2, char * s3);
 

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
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top