C++ Tutorial – 13 – Functions I
Functions are reusable code blocks that will only execute when called.
Defining functions
A function can be created by typing void followed by the function’s name, a set of parentheses and a code block. The void keyword means that the function will not return a value. The naming convention for functions is the same as for variables – a descriptive name with each word initially capitalized, except for the first one.
void myFunction() { cout << "Hello World"; }
Calling functions
The function above will simply print out a text message when it is called. To invoke it from the main function the function’s name is specified followed by a set of parentheses.
int main() { myFunction(); // "Hello World" }
Function parameters
The parentheses that follow the function name are used to pass arguments to the function. To do this the corresponding parameters must first be added to the function declaration in the form of a comma separated list.
void myFunction(string a, string b) { cout << a + " " + b; }
A function can be defined to take any number of arguments, and they can have any data types. Just ensure the function is called with the same types and number of arguments.
myFunction("Hello", "World"); // "Hello World"
To be precise, parameters appear in function definitions, while arguments appear in function calls. However, the two terms are sometimes used interchangeably.
Default parameter values
It is possible to specify default values for parameters by assigning them a value inside the parameter list.
void myFunction(string a, string b = "Earth") { cout << a + " " + b; }
Then, if that argument is unspecified when the function is called the default value will be used instead. For this to work it is important that the parameters with default values are to the right of those without default values.
myFunction("Hello"); // "Hello Earth"
Function overloading
A function in C++ can be defined multiple times with different arguments. This is a powerful feature called function overloading that allows a function to handle a variety of parameters without the programmer using the function needing to be aware of it.
void myFunction(string a, string b) { cout << a+" "+b; } void myFunction(string a) { cout << a; } void myFunction(int a) { cout << a; }
Return statement
A function can return a value. The void keyword is then replaced with the data type that the function will return, and the return keyword is added to the function’s body followed by an argument of the specified return type.
int getSum(int a, int b) { return a + b; }
Return is a jump statement that causes the function to exit and return the specified value to the place where the function was called. For example, the function above can be passed as an argument to the output stream since the function evaluates to an integer.
cout << getSum(5, 10); // "15"
The return statement can also be used in void functions to exit before the end block is reached.
void dummy() { return; }
Note that although the main function is set to return an integer type, it does not have to explicitly return a value. This is because the compiler will automatically add a return zero statement to the end of the main function.
int main() { return 0; }
Forward declaration
An important thing to keep in mind in C++ is that functions must be declared before they can be called. This does not mean that the function has to be implemented before it is called. It only means that the function’s header needs to be specified at the beginning of the source file, so that the compiler knows that the function exists. This kind of forward declaration is known as a prototype.
void myFunction(int a); // prototype int main() { myFunction(0); } void myFunction(int a) {}
The parameter names in the prototype do not need to be included. Only the data types must be specified.
void myFunction(int);
![[Affiliate link] C++ Quick Syntax Reference](https://gamingcommission.club/web.archive.org/web/20131230042353im_/d3qzmfcxsyv953.cloudfront.net/images/books/Cpp-quick-syntax-reference-medium.png)
![[Affiliate link] Lynda](https://gamingcommission.club/web.archive.org/web/20131230042353im_/d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/lynda.png)

![[Affiliate link] bookname](https://gamingcommission.club/web.archive.org/web/20131230042353im_/d3qzmfcxsyv953.cloudfront.net/images/books/Cpp-quick-syntax-reference-small.png)