+ 1
return value and passing value difference?
Can i define a function like: 'void funcion()' and return a value? is it true that void meaning not returning any value? Last question, if i call a function without passing any argument to the function, can it still return a value back to where it's called?
2 ответов
+ 6
void means 'nothing', 'emptyness'. A function with void return value does not return anything.
You can return something without passing any argument to the function, as long as the function does not return void, e.g.
int func()
{
return 1;
}
// Without any arguments passed to manipulate, this function will always return 1.
That said, you can still alter variables with void functions, by passing variables via reference to the function, so that any changes made to the variable will also reflect in main(), or any other place it is called, e.g.
void func(int& ref)
{
ref++;
}
int main()
{
int num = 10;
func(num);
std::cout << num;
return 0;
}
// prints 11
+ 1
1: You cannot return any value if you declared your function as void, void means it cannot return any value
void func() {
return 1; //ERROR!
cout << "Hello"; //no error
}
2: Yes void means you cannot return a value
3: No, an error will occur, you didn't give an arguement to the function it will only work if you gave the arguement an optional value