Hello
Does every function in C++ returns value? If yes what is that value and what is the use?
No. Some functions do not return a value.
No, some functions have no return. These functions are declared as having a void
type, to indicate that they don't.
void foo(int bar)
{
cout << bar << endl;
}
As for the value of the return and how it is used, that depends greatly on the function in question. For example, the math function pow()
returns a double
, which is the result of the function's computation (it computes the value of the first argument raised to the second argument). The old C-style input function scanf()
returns the number of characters read in. Th character function toupper()
returns the upper-case form of it's input, if the character is a letter. It all depends on the function and what it is meant to do.
What value will this function return
int x()
{
cout<<"Hello World";
return 0;
}
If it returns 0 then when it will return other than 0? And whether function have any default return value?
What value will this function return
The clue is in the code.return 0;
In your function example, returning the integer 0 is a bit meaningless.
You function can as well returning nothing(void) like:
void x()
{
cout<<"Hello World";
}
If it returns 0 then when it will return other than 0?
i'd be happy if you ask this question in connection with main
function.
anyways , its nothing speical to return different values , you just need to use that value with return statement.See below :
int Factorial_Three()
{
return 6;
}
int main()
{
cout<<Factorial_Three();
return 0;
}
Here the function Factorial_Three
returns the value 6(which is what you want(i.e. other than 0).And it return the value from where the function is called.Therefore it prints 6.
And whether function have any default return value?
for main
function , its int
. it means if your don't specify any return type then it will be considered as int
by default. but it's not good practice , therefore always use int main()
instead of main()
.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.