Now that you know what a variable is I'll explain what a function is.
Imagine a simple math equation
Code:
f(x) = x^2+1
g(x,y) = x/y
For several values of x and y you have
Code:
f(2) = 2^2+1 = 5
f(10) = 10^2+1 = 101
g(4,5) = 4/5 = 0.8
g(0,7) = 0/7 = 0
For diferent values of x and y you have diferent results. Note that all functions return only ONE value.
Now using C notation, it the exact same thinking, with a diference: you have to write the type of your input variables like this
Code:
f(int x){
// do something and return x^2+1
}
g(int x,int y){
// do something and return = x/y
}
But you may note there's something missing here, which is the return type of the result. Any function can return a integer, a char, a floating point number, a pointer... whatever. So to point out this, one MUST specify always a return type for a function.
So
Code:
int f(int x){
// do something and return x^2+1
}
float g(int x,int y){
// do something and return = x/y
}
This way f and g are functions that calculate something using integers and return a single result.
And to finish what does one need to write to point that the function should return something?
A return statement:So
Code:
int f(int x){
// do something and return x^2+1
return x*x + 2;
}
float g(int x,int y){
// do something and return = x/y
return x/y;
}
Whenever a return statement is reached your function execution ends and the result of the expression in the return statement is returned.
But what if y is 0 ?? Division by zero isn't allowed. So using the return statement:
Code:
float g(int x,int y){
// do something and return = x/y
if(y == 0)
return 0;
else
return x/(float)y;
}
And to finish why "x/(float)y" and not "x/y" ?
x and y are integers so division between them 2 will result in a integer division or by other words, only the quocient is returned and the remainder is dispised. To get the remainder use % instead.
e.g.
Code:
2/3 = 0 2%3 = 2
5/4= 1 5%4 = 1
13/11 = 1 13%11 = 2
But we want it to return a complete floating point value, including the divided remainder.
So after casting y ( "(float)y" ) , having therefore a float variable, the division will result in a floating point value.
Code:
2/(float)3 = 0.66666...
5/(float)4= 1.25
13/(float)11 = 1,1818...
//Or if you declare implicit floats
2/3.0 = 0.66666...
5/4.0 = 1.25
13/11.0 = 1,1818...
So your g function receives 2 ints and calculates the division between them and returns it in floating point format.
Also type void is allowed for functions return value. Or in other word use void for function that don't return anything.
Code:
void give_grandma_a_cake(){//no parameters
go_to_grandmas_house();
give_the_cake();
run_from_scary_wolf();
}
int main(){
/*****
This expression is invalid because the function
doesn't return anything
int result = give_grandma_a_cake();
*****/
//now this is valid because there's no return value
//and therefore nothing to assign no any variable
give_grandma_a_cake();
return 0;//return 0 to the operation system ;)
}
The contrary is also valid. One doesn't need to send input variables to get a different result every time. A example of that is the rand() function which returns a different random value each time it is called.
Code:
int r1 = rand();
int r2 = rand();
int r3 = rand();
r1, r2, and r3 have different values... or may have, but that's another story.