If you want the number to be changed by your function, you have to assign to it what ever value the function returns.
You could declare your variable globally, then change it directly with your function if you wanted to, along the following lines...
Code:
#include <stdio.h>
int nMyNumber = 18; //Declared outside of all functions
void AddFive(void)
{
nMyNumber = nMyNumber + 5;
}
int main()
{
//int nNewNumber;
printf("My original number is %d\n", nMyNumber);
AddFive();
printf("My new number is %d\n", nMyNumber);
return 0;
}
In the above example we are working directly with the variable, rather than using a copy of it and returning a value, which on this level almost defeats the purpose of the function entirely.
A more streamlined version of the code I posted previously (1 variable removed) could look like this
Code:
#include <stdio.h>
int AddFive(int Number)
{
Number = Number + 5;
return (Number);
}
int main()
{
int nMyNumber = 18;
//int nNewNumber;
printf("My original number is %d\n", nMyNumber);
nMyNumber = AddFive(nMyNumber);
printf("My new number is %d\n", nMyNumber);
return 0;
}
This is still only working with a copy of nMyNumber, the line
"nMyNumber = AddFive(nMyNumber);"
works happily because of the order of operations. In terms of workflow at that point the following happens
[list=1][*] Addfive(nMyNumber) // Send the value of nMyNumber to the AddFive function[*] That value is now stored in the variable Number inside the Addfive function[*] Add 5 to Number[*] Return our new Number to the calling function, giving us nMyNumber=23[/list=1]
I really have tried to be clear, but I am only a beginner. Though I'd suggest if this is still really unclear you go back to reading the book/website, and better yet, look for one that doesn't use "void main" in their examples.