Need a shove over the edge to understand "return"

Can someone help me understand the different between the exercise calculating turkey cook time by weight and the Celsius conversion?

The turkey code used a void() whereas the temp conversion did not. However, it seems that both examples have function information passed between the function and main().

I believe I understand the concept of return 1; versus return 0.

Why does the turkey example work with a void() when the temp conversion example needs a return 0; ?

Thanks

If a function returns a value, you must declare it like this:

Bar Foo ();

And define it like this:

Bar Foo ()
{
   // Compute Bar and return it...
   Bar bar = ...
   ...
   return bar;
}

Above, Foo and Bar are imaginary names of course.

If a function does not return a value, you must declare it like this:

void TickleBar ();

And define it like this:

void TickleBar ()
{
   // Tickle Bar and return
   ...
   return;
}

Returning a value is one way a function can pass information back to its caller, but there are other ways also.

[Accelerate your learning and become a competent programmer: pretty-function.org]

Thanks for the examples; I follow you there.

That’s where I am confused however–other ways.

Those examples I cited above both seem to pass information back and forth; one with void and one without.

Why is one preferred? Or, why should one not be used? I guess both questions.

A function declared as returning void can’t return a value with a return statement.

This is an error:

void TickleBar ()
{
   // Tickle Bar and return 99
   ...
   return 99;
}

It can only do so through a parameter of some pointer type (this is one other way.)

A function of this kind is intended to perform some operation and to not return a value.

so what’s the benefit of having something like a function that performs 2+2 but doesn’t return anything?

Does the idea of returning mean the operation is performed, and the answered stored, but it’s being held for “later use”?

[quote=“Dornier”]so what’s the benefit of having something like a function that performs 2+2 but doesn’t return anything?

Does the idea of returning mean the operation is performed, and the answered stored, but it’s being held for “later use”?[/quote]

Here’s an example of a void function that will print a multiplication table. Its return type is void but inside the function it is doing math and printing the table. You can also apply this to other things like printing a fahrenheit to celsius table.

[code]#include <stdio.h>

void multiplyAndPrint();

int main(){

//Write a void function to do something

multiplyAndPrint();

return 0;
}

void multiplyAndPrint(){

    for(int i = 1; i <= 10; i++){
            for(int j = 1; j <=10; j++){
                    printf("The value of %d X  %d = %d\n", i,j,(i*j));
            }   
    }   

}
[/code]

Thanks for that.

I’m going to take that on faith for now and hope the next chapters shed light on how/when to chose the best one for my needs.

The more code you write you’ll begin to figure out different needs more and more :slight_smile:

If you’re having trouble thinking you’ll never “use” void functions just think of some of the things you were taught in school. You’d think I’ll never need this but eventually or even every day you use it now.

I don’t know if you’ve made it to the Recursion section yet in the chapter but that will give you a better understanding of return. Also play with the debugger in Xcode how it shows in the book. You can see step by step what’s going on and when each function returns.

Perfect. Thanks guys. First time posting here. It’s encouraging to see quick, helpful, posts.