void as a return type should be used when everything you plan to do with the data in the function is to be used only in that function. Should you choose to give the function parameters, such as adding two numbers:
Code:
void add_two(int x, int y)
then you plan to add your two numbers and then if you want to print them you would do so inside that function.
A data type besides void tells the function that it is to send an item of data back to the part of your program that called the function. Say you call this function with a return type int from main():
Code:
int add_two(int x, int y){
int sum;
//do stuff
return sum;
}
using return sum; you told the function to send sum back to main where you can assign it to a variable local to main and then print it out or modify it. sum itself does not change until you call the function again, you just give the value of sum to another variable so that you can use it in main.
You can send any number of data types back to the function that called your function. int, char, float, double, pointers, etc...
Here's the full program to add two numbers using a return type of int:
Code:
#include <stdio.h>
int add_two(int x, int y);
int main(){
int one, two, three;
printf("Enter two numbers: ");
scanf("%d%d", &one, &two);
three = add_two(one, two);
three++;
printf("The sum of your two numbers is %d", three);
return 0;
}
int add_two(int x, int y){
int sum;
sum = x + y;
return sum;
}
The output of this program is the sum of the two numbers the user entered in plus one.
Hope that helps
-Prelude