default parameter in C++

#include<conio.c>
#include<dos.h>
#include<stdio.c>
function defnition
void line(char ch='-',int n=80)
{
int i=0;
i=1;
while(i<=n)
{
printf("%c",ch);
delay(100);
i++;
}
return;
}

int main(void)
{
clrscr();
line();
line('*');
line('^',40);

while(!kbhit());
return 0;
}

my Q is when we call either of the line(),line('*'),line('^",40) three function there is no error and we get an output
but when we do the same with the following function we got an error
too few parameters to call the function
#include<conio.c>
#include<dos.h>
#include<stdio.c>
function defnition
int sum(int x,int y)
{
return x+y
}

int main(void)
{
clrscr();
function calling
sum();
while(!kbhit());
return 0;
}
Last edited on
line has default arguments for the two parameters so if you don't pass an argument the default arguments will be used instead.
line() is equivalent to line('-', 80).
line('*') is equivalent to line('*', 80).

sum doesn't have any default arguments so you need to explicitly pass all the arguments when calling the function.
Last edited on
The function sum expects two parameters. This means you must provide two parameters when you call it. You have not provided two parameters, so it does not work.

The function line expects two parameters. In the definition,
void line(char ch='-',int n=80)
default parameters have been provided. This means that if you do not provide parameters, it will use for the parameter ch and 80 for the parameter n. You have provided default values in the definition. If you didn't provide default values, it would look like this:
void line(char ch,int n)
Hopefully the difference is obvious.

No such default values have been provided for the function sum, so when you try to call it without providing values, there are no default values to use.


As an aside, this:
#include<conio.c>
is hopefully a mistake. As a rule of thumb, never #include a *.c file (you can, but you should really know what you're doing and understand how the linker works, so that you won't hit the common errors people hit when they start including *.cpp files). I expect you meant conio.h

Also, the above code is not C++. It is C, or possibly C++ from twenty years ago.

If you were coding correct, modern C++, you would have int main(), you'd be using namespaces, and you would be using standard C++ libraries instead of twenty year old non-standard headers. Your choice entirely, but lots of people who use Turbo-C++ don't even realise that they're not learning C++.
Last edited on
@moschops
I just change the extension file name of conio.h to conio.c through dos

but I know that I am learning C++

thanx moschops & peter 87
I just change the extension file name of conio.h to conio.c through dos


I know I'm going to regret this, but why? What about all the programs that rely on conio.h existing?

but I know that I am learning C++

You're learning very old, out-of-date C++ that a modern compiler would refuse to deal with, because it has been incorrect since about 1998. I know it's not your fault. I know you're forced to use tools from 20 years ago, even though modern tools are available for free. I just think you should be aware of it.
Last edited on
Take into acccount that though the first example of code is compiled and executed ( I will leave it as is )

#include<conio.c>
#include<dos.h>
#include<stdio.c>
function defnition
void line(char ch='-',int n=80)
{
int i=0;
i=1;
while(i<=n)
{
printf("%c",ch);
delay(100);
i++;
}
return;
}
int main(void)
{
clrscr();
line();
line('*');
line('^',40);
while(!kbhit());
return 0;
}


in the following example only the third call of the function will be valid if to add only one statement (in bold)

#include<conio.c>
#include<dos.h>
#include<stdio.c>
function defnition
void line(char ch='-',int n=80)
{
int i=0;
i=1;
while(i<=n)
{
printf("%c",ch);
delay(100);
i++;
}
return;
}
int main(void)
{
clrscr();
void line(char, int );
line(); // here will be a compilation error
line('*'); // here will be the same as above
line('^',40); // this statement is o'k
while(!kbhit());
return 0;
}


Last edited on
@moschops
I also use following
1->Code Blocks
2->Dev C++
3->Visual Studio 2008

and I was not forced to learn with these tools
I started programming few years ago initially I used TCPP IDE
and this time I am just using PELLES C for C11

I am beginner if there is any one which is good for me then plz suggest me
Last edited on
@vald
#include<conio.c>
#include<dos.h>
#include<stdio.c>
function defnition
void line(char ch='-',int n=80)
{
int i=0;
i=1;
while(i<=n)
{
printf("%c",ch);
delay(100);
i++;
}
return;
}
int main(void)
{
clrscr();
line();
line('*');
line('^',40);
while(!kbhit());
return 0;
}
this code is compiled and executed properly

why to give void line(char,int);
in main() function when we give defnition before main() function

So you are coding in C? Then this is fine (or rather, it's correct C code - it's still bizarre to rename your *.h files to *.c files for no reason, and it will break some working code in other header files)

You said you were coding in C++ - the code above is not C++, so that was a problem, but if you're coding in C, it's fine.
Last edited on
@JAI SINGH
why to give void line(char,int);
in main() function when we give defnition before main() function


To demonstrate you that default arguments depends of the scope where they are defined in a function declaration.

@JAI SINGH
and this time I am just using PELLES C for C11


There are no default arguments in C.
Last edited on
@moschops
Yes I agree with you...
Topic archived. No new replies allowed.