Function basics
By it's definition, a function is a group of code that is accessed at some point in the execution
of a program which carries out some specific task(s). They are accessed when they are called.
Below is how a function would look:
where type is the return type of the function,
name is the appropriate name given to
the function and each parameter (or argument)
is of an appropriate type.
By definition, a function can return a value of a certain type
to where the function was called. Just like variables, a function can be of type int, double,
float, bool etc... However, there is a special type of function type called void.
By definition, a void function DOES NOT RETURN A VALUE. Mostly, void functions are used when printing
something to the screen such as a welcome message or even the triangle of stars.
Declaring functions
In a similar fashion to variables, functions need to be declared in order to be used. There are
a few ways of doing this.
Method 1: Declare first, Define later
As you will observe, the trip(...) function was declared between the main() function and the
heading of the program. It was then defined in full AFTER the main function.
These are called header lines for a function as they occur in the head of the program.
This is the prefered was of declaring functions but there is another (method 2).
Method 2: Define in full
In this method, the function is declared fully BEFORE the main() function. THIS IS DANGEROUS
primarily because of other functions. As an example, say that you have another function call
inside the triple(...) function and you did not declare it ABOVE the triple(...) function, you
are in trouble. C++ will not know where the other function is in memory!!! Try to use method
number 1 when declaring functions.
Calling functions
In order for a function to be used in a program, it must be called. To do this, the name of
the function and any parameters you are passing to it must be typed out EXACTLY as how you
declared it. The below example shows how to call as well as declare a function:
Example 1:
Void example
Download source code here (Right click - Save Tagret As...)
Below is a program that will show an example of a void function and how it is called.
As you will observe, when the main function is run, the printMessage() function is called
and prints out the message. Remember the rule of void functions: they do not return a value.
Passing by value
When dealing with function parameters, there are two ways of passing variables or other values.
One ways, and the most common, is called passing by value. This will give a COPY of the value
to the function and the ORIGINAL value WILL NOT CHANGE IN THE PROGRAM. The below example shows
a simple adding function that tries to change a value but does not.
Example 2:
Value example
Download source code here (Right click - Save Tagret As...)
Below is a program that will show an example of a pass by value call to a simple adding function.
This program will pass the variables n1 and n2 to the function add(...) and return the sum of
those two numbers.
For this program, the integer parameter a is a COPY of the variable n1 while
the integer parameter b is a COPY of the variable n2. When the add(...) function
is called, the value of n1 DOES NOT CHANGE because of the passing by value.
Passing by reference
This method of passing values is very different from the first method. Here, values CAN CHANGE
if any of them are being passed by reference.
In order to denote a pass by reference, an ampersand ( & ) must be placed after the type
of the parameter.
Example 3:
Reference example
Download source code here (Right click - Save Tagret As...)
Below is a program that will show an example of a both a pass by value call
and a pass by reference.
This program contains two functions, dbl(...) and trip(...) which will double and triple a
two numbers accordingly when called. The dbl(...) function contains a pass by reference,
which will change the values of the original variables, while the trip(...) function contains
a pass by value, which only passes a copy of the values to the function.
The output of this program is as follows:
Member Functions
With C++ as many other languages, there are whats called member functions. They are "pre-made"
by the language that you are using. In general, they are part of the class it comes from. All member
functions are accessed with the dot operator ( . )
With the cout class, there are member functions that control output formatting.
Let's see the cout stream and some of its member functions.
width( numCharacters )
sets the spacing of the output in terms of the number of characters (the argument).
The width is valid only on the next cout statement. Here is an example:
The width will set 11 spaces for the "Hi!" string. Then, the "Bye!" string will print with no
restriction in terms of size. The output is:
fill( character )
sets the character that will be used in output that has a width
larger than the expression needs. Using the example above, let's add another line
of code to the program that uses fill:
The fill() function will accept a character as its parameter and will use it to fill the unused spaces
from the string. Here, there are 7 dashes that are used. This is the output:
With the cin class, there are member functions that deal with input. Let's see two of them.
get( charArray, capacity )
The cin.get() function will take a line of input (tutorial 10), and be able to
include whitespaces. Until now, the cin stream stops the input at a white space. Now, with this
function and the next, we can control that. Here is an example.
This example will allow a user to enter a string that will include whitespaces. There
are 2 arguments that need to be passed to the function. The first is the name of the character
array and the second is the space in that array.
getline( charArray, capacity, endingCharacter )
The cin.getline() function will take a line of input (tutorial 10), and be able to
include whitespaces. The only difference between the get() and getline() functions is that
getline() will have a default terminating character of the newline character '\n'.
If you need another character other than the newline, you may specify that as a third parameter,
as seen above.