This matter gets pretty tricky for new programs. I'll show you two simple programs that calculates the sum of two numbers using both methods . That is , returning a value and using a pointer.
Functions returning a value :
Code:
#include <stdio.h>
int sum(int , int);
int main(void)
{
int num1, num2, result;
printf("Enter the first number : ");
scanf("%d", &num1);
printf("Enter the second number : ");
scanf("%d", &num2);
result = sum(num1, num2);
printf("%d", result);
return 0;
}
int sum(int x, int y)
{
int result;
result = x + y;
return result;
}
First of all we declare\write our functions according to this prototype :
Code:
type name_of_function(type variable);
type : here is where you declare the value the function should return as a result , it can be an int , double, char or whatever type you want your function to return.
name_of_function : a name specified by the user , you can choose any name (within the naming rules) but we usually name it according to its job.
variable : this variable is what we call an argument , it takes the value sent from any other function to use it within it-self.
don't get it ? keep reading ...
First , we declared our function sum (you can name it as you wish btw) as an int wich means it returns an integer (we'll get to this) , between the parentheses we're telling the computer how many and what kind of arguments our sum function will take (or point to). We declared two int variables without the necissity of specifing their names.
Now since function main won't take any arguments * , it should be declared as int main (void) where int is the type of the value that main will return. main returns a value of 0 to report a successful execution.
* If you don't know main can take arguments then just keep using 'main (void)' untill you learn how to get a clear image of it.
Within main , we declared 3 int variables , 2 to store inputs and one to store the return value of the function sum.
the line : result = sum(num1, num2)
executes sum(num1,num2) first , it sends the values of num1 and num2 to the two int variables in function sum so they are stored in the order they were sent in. You can imagine it bieng like that :
Code:
main : num1 num2
| |
V V
sum : x y
note that our functions must take as many values (aka:arguments) as it can hold. In our case , we declared sum with two variables so 'main' MUST send two arguments or else you'll get an error. Also the name of the variables in our costum functions' declarations (like sum) doesn't need to match the ones we're sending from any other functions. For example , in our program , we sent the value of two variables (as arguments) which are called 'num1' and 'num2' in main , to another two variables called 'x' and 'y' in function 'sum' . Its just passing the value (or adress in case of pointers) not the variable itself.
within the sum function we declare an int variable called result to hold the sum of x and y as shown. the 'return result' statement would return the value of 'result' to function main again.
For example , lets say we took 5 and 6 as inputs from the user , they would get stored in their variables (num1 and num2) , sent to function sum as arguments , stored in x and y , get summed and stored in our variable 'result' wich will be 11 in this example.
Now function sum will simply return the value 11 to the function that called it wich is 'main' here , it'll simply replace the :
Code:
result = sum(num1, num2);
with :
This is what returning a value mean.
Functions using pointers :
You need to understand the concept of pointers in order to understand this.
We can let pointers declared in other functions point to memory adresses in function main (or any other function we use). We can write the previous program using pointers , without returning a value , as the following :
Code:
#include <stdio.h>
void sum(int *);
int main(void)
{
int result;
sum(&result);
printf("%d", result);
return 0;
}
void sum(int *x)
{
int num1, num2 , sum;
printf("Enter the first number : ");
scanf("%d", &num1);
printf("Enter the second number : ");
scanf("%d", &num2);
sum = num1 + num2;
*x = sum;
}
since 'x' is pointing to the adress of 'result' , it will be able to manipulate\store the value of the variable 'result' (wich it actually does here in our example) thus we don't need to return a value to main because 'result' can be manipulated by its pointer 'x' , so we don't need to send back a value to main.
notes :
1. Only pointers within our function declaration (that is , the pointers declared between parentheses) are able to point at adresses of arguments from other functions. Trying to point to a variable in function 'main' from a pointer within the body any other function would lead to an error.
2. function 'sum' returns a value to 'main' , 'main' returns a value to the computer.
3. 'return 0' tells the computer that the program has successfly ended.
I hope this clears things up..