C++ Programming - Functions - Discussion

Discussion Forum : Functions - General Questions (Q.No. 1)
1.
Which of the following function prototype is perfectly acceptable?
int Function(int Tmp = Show());
float Function(int Tmp = Show(int, float));
Both A and B.
float = Show(int, float) Function(Tmp);
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
19 comments Page 1 of 2.

Kmp said:   9 years ago
Parameters should not be initialized in the function argument.
(3)

Durgesh said:   10 years ago
float = Show(int, float) Function(Tmp);
(3)

Bhanu said:   1 decade ago
Here While calling a function if another function is passed as parameter then that function should have values as parameter inside parenthesis not the data type.

int Function(int Tmp = Show());
or
int Function(int Tmp = Show(2,3.2));

Both are correct.

But
int Function(int Tmp = Show(int x , float y)); // Incorrect.
(2)

Sharon Rajendra Manmothe said:   10 months ago
Function Prototype: A function prototype declares a function's name, return type, and parameter list. It informs the compiler about the function's existence and its signature.

Default Argument: A default argument is a value that is automatically assigned to a parameter if no argument is provided during the function call.  
In the given prototype:

int Function(int Tmp = Show()):
This declares a function named Function that returns an integer.

It takes an optional integer parameter Tmp. If no argument is provided for Tmp, the default value is obtained by calling the Show() function.

Why the other prototypes are invalid:
float Function(int Tmp = Show(int, float)):
The default argument Show(int, float) is incorrect. A default argument must be a constant expression or a function call that doesn't require any arguments.
float = Show(int, float) Function(Tmp);:

This is not a valid function prototype. It's syntactically incorrect and doesn't follow the standard function declaration format.
(2)

Vada said:   1 decade ago
First know the logic's to execute.
(2)

Sunchuramya said:   1 decade ago
First we should know the logic's to execute a program.
(2)

Prasadkumar said:   1 decade ago
--------------------------------------------------------------------
[B]. float Function(int Tmp = Show(int, float));

In above option there is type but no arguments in show().

Actually the function definition is like this:

returntype funname(type arg1,type arg2) {
body
}
-----------------------------------------------------------

[D]. float = Show(int, float) Function(Tmp);

in the above function also same error id there.
(1)

Seema said:   1 decade ago
B is incorrect as how do we send the arguments of function show.
(1)

Doga said:   1 decade ago
This is the case of the default argument, hence show should be like show (a, b) or show (2, 4.232), or such n such. But show (int, float) are data type that provide the default value to the function argument so give error. But show has no argument so possibly return some value if its definition has return.

Sushovan Das said:   1 decade ago
Option A is correct as in a parameter any other parameter can not be passed.


Post your comments here:

Your comments will be displayed after verification.