Question:
Programming in C++?
Perfection
2015-06-14 16:59:46 UTC
when doing Void functions, it doesn't return anything , but with non void functions it does. My question is What is it actually returning ? When I print I see no difference .
Nine answers:
?
2015-06-15 04:09:01 UTC
Functions return a value based on the return type specified where the function is declared. If you have a function returns an "int" you can can only return an object that is an int, or can be converted to an int. When you have a function returning void, it simply means the function does not return anything, unless the function returns a void pointer(void*).



If you're not using the return keyword and the function is ending without returning a value, the result of such a situation is undefined, but the return value will be likely similar. Some compilers won't give errors or show warnings when programmers make such errors. I've accidentally had such a situation in Linux GCC. The compiler doesn't see a return, so it basically just guesses what the return value should be(But it also depends on the type the function returns).



// No, that is illegal

void myFunction() { return 10; }



// No, this is also wrong, but some compilers will not complain

int myFunction() { }



// OK!

int myFunction() { return 10; }



// This is also OK, but the one above is more common.

int myFunction(void) { return 10; }



Then you do something like:



int returned_value = myFunction();

std::cout << "Value is " << returned_value << std::endl;
mark_poc
2015-06-14 21:10:53 UTC
"It "returns" it to what ? And how would I print it. If I print a void function it prints the same as if I changed it to a non void with a return statement , so where is it returning it to? And how would I retrieve it"



Think of the function as throwing the return value at you and you have to catch it. And you catch it in another variable. Here is a function called "M" that multiplies 2 times 2 and then returns the result:



First the function definition:



int M()

{

  return (2 * 2);

}



Here is how you would now use this new function that you just created, and print out the result of 2 x 2:



int x = 0;



x = M();

cout << x;



or



cout << M();



In the first example, x "catches" the returned value of M(). In the second example, cout catches the returned value of M() and prints it out.



With a void function, you could only do it this way:



void M()

{

    cout << (2 * 2);

}



Then to use the function M():



m();
?
2015-06-17 10:06:16 UTC
Void is a return type . If you use void in a function that means you do not want to return anything from the function . Void is related to return type not to print . A simple program using void .

[This function will print "I am the print function" but does not return anything]



public void print()

{

int a=5,b=6,c;

c=a+b;

System.out.println("I am print function");

return c; //Error!! in void you can not return anything

}



[ The right program is]



public int print() ///You have to use int for ( integer )

{

int a=5,b=6,c;

c=a+b;

System.out.println("I am print function");

return c; ////This function will return 11

}
Richard J
2015-06-14 19:48:41 UTC
A void function does the task within the function itself. If you want to print anything in it you have to do so from within the void function itself.



A non void function sends you back a result, such as an integer, after performing the calculations or tasks from within it. Generally you'd print this result from the main function...
?
2015-06-14 17:47:19 UTC
Of course you see the difference, because using the value of an expression returning void is illegal.



We use "non-void functions" (really just functions) to compute a value. Most of the time, we return the computed value. If you don't _only_ return the computed value, you're arguably writing horrible code.



We use "void functions" (really a better term is "procedures" or "subroutines" or "subprogram") to describe a set of instructions.



"non-void" functions are more closely aligned to the mathematical concept of a function --- a value input in the domain of the function yields a value in the codomain, while procedures are just a series of steps.
Perfection
2015-06-14 18:00:34 UTC
It "returns" it to what ? And how would I print it. If I print a void function it prints the same as if I changed it to a non void with a return statement , so where is it returning it to? And how would I retrieve it
Sonya
2015-06-15 21:52:39 UTC
If you are using void function then it will return.Otherwise did not return the value.
Thomas
2015-06-14 17:01:23 UTC
The function will return whatever value you program it to return.
anonymous
2015-06-17 19:11:59 UTC
it will return void.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...