1 Crore+ students have signed up on EduRev. Have you? |
What happens to a function defined inside a class without any complex operations (like looping, a large number of lines, etc)?
Any function which is defined inside a class and has no complex operations like loops, a large number of lines then it is made inline.
An inline function is expanded during the compile-time of a program.
Default values for a function is defined when the function is declared inside a program.
If an argument from the parameter list of a function is defined constant then _______________
A function is not allowed a constant member of the parameter list.
What will be the output of the following C++ code?
#include<iostream>
using namespace std;
int fun(int x = 0, int y = 0, int z)
{ return (x + y + z); }
int main()
{
cout << fun(10);
return 0;
}
Default arguments should always be declared at the rightmost side of the parameter list but the above function has a normal variable at the rightmost side which is a syntax error, therefore the function gives an error.
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int fun(int=0, int = 0);
int main()
{
cout << fun(5);
return 0;
}
int fun(int x, int y) { return (x+y); }
C++ allows to define such prototype of the function in which you are not required to give variable names only the default values. While in function definition you can provide the variable names corresponding to each parameter.
The execution of a C++ program starts from the main() function.
Which of the following is the default return value of functions in C++?
C++ uses int as the default return values for functions. It also restricts that the return type of the main function must be int.
Inline function is those which are expanded at each call during the execution of the program to reduce the cost of jumping during execution.
In which of the following cases inline functions may not word?
i) If the function has static variables.
ii) If the function has global and register variables.
iii) If the function contains loops
iv) If the function is recursive
A function is not inline if it has static variables, loops or the function is having any recursive calls.
Where should default parameters appear in a function prototype?
Default parameters are defined to the rightmost side of parameter list in a function to differentiate between the normal and default parameters for example if a function is defined as fun(int x = 5, int y) then if we call fun(10) then 10 should be given to x or y because one can apply both logics like x = 10 already defined and 10 passed is for y but if compiler reads it from left to right it will think it is for x and no parameter is given for y, therefore, the compiler will give error.
Which of the following feature is used in function overloading and function with default argument?
Both of the above types allows a function overloading which is the basic concept of Polymorphism.
What will be the output of the following C++ code?
#include<iostream>
using namespace std;
class Test
{
protected:
int x;
public:
Test (int i):x(i) { }
void fun() const { cout << "fun() const " << endl; }
void fun() { cout << "fun() " << endl; }
};
int main()
{
Test t1 (10);
const Test t2 (20);
t1.fun();
t2.fun();
return 0;
}
As the object declared are of two types one is normal object and other is constant object So normal objects calls normal fun() whereas constant objects calls constant fun().
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void square (int *x, int *y)
{
*x = (*x) * --(*y);
}
int main ( )
{
int number = 30;
square(&number, &number);
cout << number;
return 0;
}
As we are passing value by reference therefore the change in the value is reflected back to the passed variable number hence value of number is changed to 870.
The important things required in a function is its return type and its name other than that parameter list are optional which a function may or may not have.
15 videos|20 docs|13 tests
|
Use Code STAYHOME200 and get INR 200 additional OFF
|
Use Coupon Code |
15 videos|20 docs|13 tests
|
|
|
|
|
|
|
|
|
|