Is there any method to make the argument of function optional? means if user don't give its value in calling the function, then it should not do any effect on the procedure of function.
To extend:
You can set what's called a default value for the argument. Any, all, or none of a function's arguments can have default values. In Bazzy's example, myfunction(); and myfunction(3); will have the same result. However, complications arise in the case of multiple arguments. The compiler can't tell which argument you've omitted... Consider the following code. int myfunction( int optional = 3, int mandatory );
What will myfunction(4); return?
Because this is ambiguous, your arguments must be placed in order, sorted by how often you will pass values to them.
For example, since you ALWAYS pass an argument to mandatory, that must come first. If there is a function with two default values int myfunction( int mandatory, int optional1 = 3, int optional2 = 5 ); then the program will omit the values at the end if you don't specify all of the arguments, so it makes sense to put the arguments you use more often first.
You can also declare different functions with the same name but different paramters. Eg:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int function(int a, int b, int c)
{
return a*b*c;
}
int function(int a, int b)
{
return a*b;
}
int main()
{
cout<<function(2,3,4)<<"\n"<<function(2,3);
cin.ignore();
return 0;
}