0
What exactly does return/void do in C++
what exactly does return do in C++? Along with void? Sorry, I just recently began learning C++
1 Answer
+ 3
It means that the function does not return a value.
example: if you have a function that it's purpose is to just print the sum of two numbers for instance, you write
void printFunction(int x, int y){
cout << x+y;
}
this function just used to print something.
To better understand, I will write a function that does return a value:
int sum(int x, int y){
int z = x+y;
return z;
}
int main(){
int var1 = 3;
int var2 = 6;
int sumOfVars = sum(var1, var2);
printFunction(var1, var2);
}
As you can see I used a function sum to assign a value to an integer variable because it returns a value of int (I.e 'int sum')
the function sums the two vars and assign the result to the variable named sumOfVars.
after that I called the printFuction just like that to print a value to the screen.
If I would write:
int sumOfVars = printFunction(var1, var2);
i would get an error because there is no value return from the function whatsoever.