
 |
![]() |
|
|
|
|
 |
What are Functions?
You are all probably wondering what is a function. Well you can think of functions as little packages of code -- they do the work, but they can be reused later. This makes programming easier, as you will need to write less code.
Before we get into the aspects of writing a function in C++, we have to understand how to declare a function using a prototype. The declaration tells the compiler:
 | |  | | |
- the name,
- return type, and
- parameters of the function.
|
|  |
|
For instance, say you want to write a prototype for a function that adds two numbers:
 | |  | | |
int addTwoNumbers ( int n1, int n2 ); //prototype |
|  |
|
Basically, in the function prototype you must include a return type, the name of the function and if it has parameters or not (in the case of a function with no parameters you would write void as the return type and just leave the ( )’s blank instead of int as shown above.)Please Rate this Code:
|
|
|
|
|
|
|