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; ?
[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));
}
}
The more code you write you’ll begin to figure out different needs more and more
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.