Level 3 — Functions
Functions
Original notes by SunDiaL · View English source
Functions are custom operations. And what a custom operation is is just any piece of code you have written before, given a name for reusability.
Say you've written a program to calculate the factorial of a number. You can take that code and create a function with it, so you can later use it to calculate the factorial of any number. As you've probably noticed, there's two steps to this.
Function definition
Defining a function is a declarative statement. A function is defined by it's return type, name, parameters and a scope.
Return type
A function may or may not return a value. If it does, the return type is the data type. If it does not, the return type is void, a keyword used to denote the absence of a data type.
Name
The name is just that, the name, nothing special about it. There's a few restrictions regarding how to name functions but the compiler will warn you about them.
Function parameters
Examples:
//a function without return type
void SomeFunction()
{
//do stuff
}
//a function with return type int
int SomeOtherFunction()
{
return 5;
}
For more examples with parameters see function parameters.
Function invocation
Invoking (calling) a function is an imperative statement. It makes the computer execute the steps defined. It is done using (calling, get it?) the function name, and providing values for the declared parameters. Additionally, if the function has a return type, it can be used as part of an assignment.
SomeFunction();
int someValue = SomeOtherFunction();
For more examples with parameters, see function parameters.