C++ Programming - Functions - Discussion

Discussion Forum : Functions - General Questions (Q.No. 11)
11.
Where the default value of parameter have to be specified?
Function call
Function definition
Function prototype
Both B or C
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
11 comments Page 1 of 2.

Shining Prajesh said:   8 years ago
D is the correct option. Because default values of parameters can be specified in both function prototype and function definition.

Andres S said:   8 years ago
The defaults values have to be in function declaration in header file. The answer is wrong.

Imteiyaz@NieMysore said:   8 years ago
Answer should be D. Because we can mention default argument either in proto or in the definition. Default argument can be specified in prototype declaring even without knowing the argument's name i.e. identifier.

Anusha.papisetti said:   9 years ago
/* Example for C++ Function Prototype and C++ Function Definition */

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

int add(int, int); // function prototype
int subtract(int, int); // function prototype
int multiply(int, int); // function prototype
int divide(int, int); // function prototype

void main()
{
clrscr();
int a, b;
cout<<"Enter any two number: ";
cin>>a>>b;
cout<<"\nSummation = "<<add(a, b);
cout<<"\nSubtraction = "<<subtract(a, b);
cout<<"\nMultiplication = "<<multiply(a, b);
cout<<"\nDivision = "<<divide(a, b);
getch();
}

int add(int x, int y) // function definition
{
int res;
res = x + y;
return res;
}

int subtract(int x, int y) // function definition
{
int res;
res = x - y;
return res;
}

int multiply(int x, int y) // function definition
{
int res;
res = x * y;
return res;
}

int divide(int x, int y) // function definition
{
if(y==0)
{
cout<<"\n\nDivide by Zero Error..!!";
cout<<"\nPress any key to exit...";
getch();
exit(1);
}
else
{
int res;
res = x / y;
return res;
}
}

Tilak Patil said:   10 years ago
If you write function definition 1st before main. Then there is no need of function prototype declaration. Hence you have to write default argument in definition itself.

But if you write 1st prototype then it is suggested to write default arguments in prototype.

Correct Answer is D.

Prof. Kamat said:   10 years ago
Default values can bbe specified either in function header or function prototype.

Jatin vamja said:   10 years ago
@Raghav.

There is not mentioned about overloaded function in question. So I agree with @Nidhi.

Sumasree said:   1 decade ago
But both function prototype and function declaration have same arguments like.

void ret(int a, int b, int c=20) ;

void ret(int a, int b, int c=20)

So option D is correct or not. Anyone clarify me.

Raghav said:   1 decade ago
@Nidhi.

No if we write:

void ret(int a,int b,int c=20)
{
cout<<c<<endl;
}

It not as declare default value for another function which are overloaded, it will can set c = 20 for only this function. Hence optn will be "C".

Divya NG said:   1 decade ago
How it is C ?


Post your comments here:

Your comments will be displayed after verification.