- Code: Select all
void myfunciton()
{
// your code here
}
The word 'void' above tells the compiler that the function will return nothing. But it can still be useful.
To use this function you'd just type:
- Code: Select all
myfunction();
You can do all kinds of handy things with them too, like passing values in. The function below still doesn’t return a value (hence the 'void') at the start, but it does receive a value that can be acted on.
- Code: Select all
void playFX(int soundID)
{
switch (soundID)
{
case 0:
// play sound here
break;
case 1:
// play another sound here
break;
default:
// do this if the value of soundID isnt handled above
break;
}
}
To use this you'd just type:
- Code: Select all
playFX(yourSoundID);
You might want to do some clever math in a function and have the result returned. In which case you might want to do something like:
- Code: Select all
result = mathFunction(3, 2);
The function for this might look like
- Code: Select all
int mathFunction(int a, int b)
{
a++; // add one to a
b+=2; // add 2 to b
return(a*b); // return a*b
// not exactly clever math, but you get my point!
}
Notice the word 'int' at the start of the function name, this indicates that the function will return an Integer value. Also notice that the variables used in the function (a and b) are declared inside the the ( ), both are expected to be integer values in this case.
I'd suggest getting a book on C. C is an excellent language to help build a general understanding of programming; it'll help you understand more about other languages too when you come to study them. I'd actually recommend two books that complement each other very well, both cover similar material but in different ways, so where one doesn’t make sense often the other one does:
C for Dummies (Dan Gookin – Published by Wiley)
Absolute Beginner’s Guide to C (Greg Perry– Published by SAMS Publishing)