Function Parameters

Hey all, had a question about function parameters.

If parameters of a function are optional, why go through the trouble of using them?

Can't you call a function without parameters? So why use them?

Any and all feedback is welcomed and appreciated.
Are you talking about functions with default arguments?

Example:

void wait(int seconds = 1);

Lets say this function waits for a number of second before it returns. The default argument is 1 so if the function is called without any arguments, 1 will be passed automatically so the function will wait 1 second.

wait(); // will call wait(1);

Calling the function without explicitly passing any arguments is enough if all you want to do is wait for 1 second, but if you want to wait for more than 1 second you will have to pass an argument.

wait(10);
Last edited on
Yeah parameters are optional to programmers Like-

1
2
3
4
5
6
7
8
9
10
//This program will simply print hello world
void hello()
{
cout<<"hello world";
}
int main()
{
hello();
return 0;
}


But if you need to play with values then you need the parameters

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//this program sums two number
void sum(int a,int b)
{
cout<<(a+b);
}
int main()
{
int x,y;
cout<<"Enter two numbers to add: ";
cin>>x>>y;
sum(x,y);//you see here user assignes the value of x and y
              //compiler doesn't knows the value before user enters' it
return 0;
}
Last edited on
closed account (2LzbRXSz)
Personally, I love function parameters. You just have to know when the right time to use them is. programmer007 gave perfect examples.

Some people either don't use them enough, or completely over do it.

It's sort of like how people don't NEED iPhones (or any data capable device) but they're a pretty useful tool. You can still live without one, but man isn't it nice to have the option to check your email when you're miles away from internet.

Except, in some cases you do need function parameters.
Topic archived. No new replies allowed.