C++ Programming - Functions - Discussion
Discussion Forum : Functions - General Questions (Q.No. 12)
12.
Which of the following statement is correct?
Discussion:
4 comments Page 1 of 1.
K2u2007 said:
1 decade ago
Default Function Arguments:
C++ allows a function to assign a parameter a default value when no argument
corresponding to that parameter is specified in a call to that function. The default value
is specified in a manner syntactically similar to a variable initialization. For example,
this declares myfunc() as taking one double argument with a default value of 0.0:
void myfunc(double d = 0.0)
{
// ...
}
Now, myfunc() can be called one of two ways, as the following examples show:
myfunc(198.234); // pass an explicit value.
myfunc(); // let function use default.
The first call passes the value 198.234 to d. The second call automatically gives d the
default value zero.
C++ allows a function to assign a parameter a default value when no argument
corresponding to that parameter is specified in a call to that function. The default value
is specified in a manner syntactically similar to a variable initialization. For example,
this declares myfunc() as taking one double argument with a default value of 0.0:
void myfunc(double d = 0.0)
{
// ...
}
Now, myfunc() can be called one of two ways, as the following examples show:
myfunc(198.234); // pass an explicit value.
myfunc(); // let function use default.
The first call passes the value 198.234 to d. The second call automatically gives d the
default value zero.
Seema Ager said:
2 years ago
@All.
Here is the coding part,
#include<iostream>
using namespace std;
void fun(int a, int b =5);
int main()
{
fun(2);
return 0;
}
// void fun(int a, b =10) // here we are redefining the default value of an argument b. it is not allowed, it will throw an error
void fun(int a, int b )
{
cout<<a<<" "<<b<<endl;
}
Here is the coding part,
#include<iostream>
using namespace std;
void fun(int a, int b =5);
int main()
{
fun(2);
return 0;
}
// void fun(int a, b =10) // here we are redefining the default value of an argument b. it is not allowed, it will throw an error
void fun(int a, int b )
{
cout<<a<<" "<<b<<endl;
}
Shining prajesh said:
8 years ago
void fun(int a=0,int b=0) // default value of parameters
{
cout<<"\nSum of value="<<a+b;
}
int main()
{
fun(1,2); //default value in calling of the function.
return 0;
}
Default value in the definition of the function is redefined by the default value of calling of the function.
{
cout<<"\nSum of value="<<a+b;
}
int main()
{
fun(1,2); //default value in calling of the function.
return 0;
}
Default value in the definition of the function is redefined by the default value of calling of the function.
Kavita said:
1 decade ago
Default values are given at time of function prototype. It can also be written at time of function definitiion. But it must be same as that of in function signature.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers