You will find defining a function in C++ very similar to defining a function in Python. There are some differences however. Let’s look at the general form, and then define the sections from here.
returnType functionName(parameterList)
{
// function body
}
Functions must be defined outside of other functions. C/C++ doesn’t have the concept of a sub or inner function like some languages.
Function Header
The function head contains the return type, function name, and the parameters.
Return Type
Functions may return a value. The name, if any, of the function doesn’t matter, what matter’s is the type of data returned.
If no data is returned, the return type is void. If data is to be returned, then you must specify the type of data. The reason why you use a type and not a name is because the value may be hard coded and not a variable, or it might be one of several variables.
If you specify a type, you should make sure that you always return a value. Returning a value within a loop, or if statement means that the value may not be returned, and this can cause errors in your application.
Function Name
Any legal programmer defined name can be used here. Typically we use humpback, sometimes called camel case, notation, just like we do with variable names. The very first letter is lower case, as are most letters, however the first letter of each word, other than the first, is capitalized. This makes it easier to read.
Note: Function names should indicate what the function is doing. This is often referred to as self documenting code, so that you know what the code is doing by reading the names. You names can be long, so don’t be afraid to use more than a few letters, if it makes future maintenance easier.
Parameters
Each of your parameters must have both a data type (unlike Python but like C/C++) and a name, like Python.
While parameters are optional (leave the section between the parentheses blank if there are none) there are often parameters as it allows for your function to perform more operations based upon the information sent to it.
A single parameter has a data type and then a name. If there are multiple parameters, then parameters are separated by a comma. See the examples below.
void noParametersInMyFunction()
{
// perform operations
}
void aSingleParameterFunction (int myNumber)
{
// perform operations
}
void multipleParameters (int num1, string strName, float newValue)
{
// perform operations
}
Parameter names are valid only inside of the function. IF they share the same name as the variable passed into it. Parameters, when their value is changed do not usually change the original value. (Arrays and some complex data types are the except to this rule.)
Notice how each parameter must have it’s own data type. This is because function parameters can include data of different types.
The Function Body
The code inside of the function body, must be contained within a set of braces. Inside the braces is any series of legal C/C++ code you wish to include. If you could write it inside your main function, you can write it here, including loops, conditions, and calling other functions.
Variables Defined Inside of a Function
Your function can use any variables passed to it, using their parameter name, or defined within the function itself. You cannot access outside variables.
Variables defined within a function can only be seen and accessed inside the function. This is called scope. The easy way to think about scope is that it limits where you can see a variable – based upon the block it was created in. If you are inside the braces where the variable was created, you can access it. If not, you cannot.
Returning Data
Generally, and especially if there is a return type, you will want to have a return, and the return value on the last line before the closing brace.
A good programming practice is to only return one value from a function. This is the idea of the function having a single starting and a single ending point.
Consider the two functions, which perform the same operation. While the first one is “easier” to write, it may be harder to debug, especially in larger programs.
int returnMaxPoorly(int x, int y)
{
if (x > y)
{
return x;
}
else
{
return y;
}
}
int returnMaxCorrectly(int x, int y)
{
int returnVal = y;
if ( x > y )
{
returnVal = x;
}
return returnVal;
}
Calling a Function
When you create a function, you tell it what it should do. When you call a function, you are actually executing that code.
You can have a function which is never called, but it does nothing. So why?
Instead, you want to call it.
If your function is a void function, one that doesn’t return a value, you should just call it’s name and pass the values to the parameters as shown below.
int x;
cout << "Please enter a value.";
cin >> x;
printMax(8, x);
If you are returning a value, you should have a variable there ready to accept the returned valued. You do not have to, however, what is the point of returning a value that isn’t used?
int largerNum = maxValue(8, 12);
C++ the Definition and Calling of Functions was originally found on Access 2 Learn