Anatomy of a C++ Function
[Anatomy of a C++ Function]
In C++, functions are required to make your code scalable, extensible and easy to maintain. in this post, we will be discussing the anatomy of a function.
Functions contain three main parts: Prototype, Header, and Definition. Each of these is important in their own right, and together they create usable code that your applications can use during execution.
[Prototypes]
Function prototypes have a specific structure. They require the following parts:
- Return type
- Name
- Parameter list
The return type is the type of data being returned from the function, either void or int or something else entirely. The name of the function is the name that will be used to differentiate it from other functions in the program and the parameter list is a series of variable types which will be passed to the function at run-time. Here is what a function prototype looks like:
<return type> functionName(<parameter list>);
We can make a simple function as a demonstration:
int double(int);
As you can see, the function prototype we just created contains a return type, a name and a parameter list (in this case a single parameter.)
[Headers]
Function headers are nearly identical to prototypes, with a couple of exceptions. One of the main differences is that you need to qualifiy the parameter list. What this means is that you have to give a variable name for each variable in the list. Additionally, for class-specific functions, you need to also include the scope operator. Also, since the header comes directly above the function definition, you will not be ending the line with a semicolon, but rather with braces. Here is the anatomy of a function header:
<return type> <class name>::functionName(int myVariable, int myOtherVariable)
{
}
Again, it is nearly identical to the function prototype, but has a bit more information in it. Here is what our function header should look like for our double function:
int double(int x)
{
}
It is pretty easy to move from prototype to header with very few modifications. You must declare the variable names for the variables you are passing to the function and you must make sure you have braces surrouding the actual definition section of the function.
[Definitions]
Now that you have a prototype and a header, we need to implement the function. Function implementation occurs in the definition of the function:
<return type> <class name>::functionName(int myVariable, int myOtherVariable)
{
// function definition here
}
For our example function, we would have something like this:
int double(int x)
{
return x * 2;
}
As you can see, the function is pretty simple. In this function we are simply returning an integer that is doubled. The return statement has to return a value that is consistent with the header and prototype return values. If we had instead tried:
int double(int x)
{
return “Hello World”;
}
Our code would have given us an error since converting from a string to an integer is pretty much impossible.
[Conclusion]
In this post, we discussed the three main parts of a function: Prototype, Header, and Definition. The prototype is placed at or near the top of the page and tells the compiler what to expect out of the function in terms of return type and parameter list. The header expands upon the prototype and declares required variables and sets the required scope. This usually comes later in the sourcefile and always directly precedes the definition. The definition is the implementation of the function. If the function is to return a value, the function must contain a return statement.