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.

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)

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)

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)

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.

Shahzada said:   1 decade ago
Yes option A is correct answer because in A function prototyping show function is called but in option B in prototyping of one function another prototyping is given which is not according to the syntax of function prototyping because return typing is not given.

NISHU said:   1 decade ago
You can't compare the value of one function to value of the variable TEMP ;if you want to then arguments should be used exactly not with their type. i.e., Direct variable on which this function is applied.

Hemanth said:   1 decade ago
Obviously the option A is correct answer the arguments where given in the option A so it is true where as in option B there are no arguments in prototype in the side of int and float. So it is false. :-).

Aman Goel said:   1 decade ago
Because if we talk about the function with argument then return type of function is very important.

Function return type tells that what type of value is function return.

Sree said:   1 decade ago
In the case of function prototype I think the type of the arguments are to be specified not the variable name, here return type should be specified.

Pankaj Thapa said:   1 decade ago
If a is correct then how b is incorrect it is calling a method using parameters which perfectly satisfies the syntax used for calling a method.


Post your comments here:

Your comments will be displayed after verification.