Lecture 5 Functions-1
Lecture 5 Functions-1
The functions that are supplied to you are usually in three categories:
Those built-in the operating system, those written in C++ (they are part of the C++
language), and those supplied with your programming environment. The use of these
functions is the same regardless of the means you get them; you should know what a
function looks like, how to create one, what functions are already available, where
they are located, and what a particular function does, how and when to use it.
Function Declaration
In order to create and use a function, you must let the compiler know. Letting the
compiler know about your function means you “declare” it. The syntax of declaring a
function is:
ReturnType FunctionName();
The simplest return type you can use is called, and represented as, void. Using this
keyword, the simplest formula we can use is:
Function Names
A function name follows the same rules we have applied to our variables except that
the name of a function will start in uppercase. In addition, use a name that specifies
what the function is expected to do. Usually, a verb is appropriate for a function that
performs an action. Examples of names of functions are Add, Start, Assign, Play, etc.
1
Function Definition
In order to use a function in your program, you have to let the compiler know what
the function does. To let the compiler know what the function is meant to do, you
have to “define” it; which also means describing its behavior. The formula to define a
function is:
Function Body
As an assignment, a function has a body. The body of the function describes what the
function is supposed to do. The body starts with an opening curly bracket “{“ and
ends with a closing curly bracket “}”. Everything between these two symbols belongs
to the function.
void SquareArea()
{
double Side;
2
cout << "\n Square characteristics:";
cout << "\n Side = " << Side;
cout << "\n Area = " << Side * Side;
}
Calling Functions
Once a function has been defined, other functions can use the result of its
assignment. Imagine you define two functions A and B.
If Function A needs to use the result of Function B, function A has to use the name of
function B. This means that Function A has to “call” Function B:
When calling one function from another function, provide neither the return value nor
the body, simply type the name of the function and its list of arguments, if any. For
example, to call a function named Welcome() from the main() function, simply type
it, like this:
int main()
{
Message(); // Calling the Message() function
return 0;
}
3
The compiler treats the calling of a function depending on where the function is
declared with regards to the caller. You can declare a function before calling it. Here
is an example:
#include <iostream.h>
void Message()
{
cout << "This is C++ in its truest form.";
}
int main()
{
Message(); // Calling the Message() function
return 0;
}
The example we saw above requires that you define a function before calling it. C++,
like many languages, allows you to call a function before defining it. Unlike many
languages, in C++, when calling a function, the compiler must be aware of the
function. This means that, you must at least declare a function before calling it. After
calling the function, you can then define it as you see fit. Here is an example:
#include <iostream.h>
int main()
{
void Message();
cout << "We will start with the student registration process.";
Message(); // Calling the Message() function
return 0;
}
Returning a Value
void Functions
A function that does not return a value is declared and defined as void. Here is an
example:
4
void Introduction()
{
cout << "This program is used to calculate the areas of some shapes.\n"
<< "The first shape will be a square and the second, a rectangle.\n"
<< "You will be requested to provide the dimensions and the program "
<< "will calculate the areas";
}
Any function could be a void type as long as you are not expecting it to return a
specific value. A void function with a more specific assignment could be used to
calculate and display the area of a square. Here is an example:
void SquareArea()
{
double Side;
The purpose of a function identifies what the function is meant to do. When a
function has carried its assignment, it provides a result. For example, if a function is
supposed to calculate the area of a square, the result would be the area of a square.
The result of a function used to get a student’s first name would be a word
representing a student’s first name. The result of a function is called a return value. A
function is also said to return a value.
There are two forms of expectations you will have from a function: a specific value or
a simple assignment. If you want the function to perform an assignment
without giving you back a result, such a function is qualified as void and
would be declared as
5
void FunctionName();
A return value, if not void, can be any of the data types. This means that a function
can return a char, an int, a float, a double, a bool, or a string. Here are examples
of declaring functions by defining their return values:
double FunctionName();
char FunctionName();
bool FunctionName();
string FunctionName();
If you declare a function that is returning anything (a function that is not void), the
compiler will need to know what value the function returns. The return value must be
the same type declared. The value is set with the return keyword.
char Answer()
{
char a;
cout << "Do you consider yourself a reliable employee (y=Yes/n=No)? ";
cin >> a;
return a;
}
A good function can also handle a complete assignment and only hand a valid value
to other calling functions. Imagine you want to process member’s applications at a
sports club. You can define a function that would request the first and last names;
other functions that need a member’s full name would request it from such a function
without worrying whether the name is complete. The following function is in charge of
requesting both names. It returns a full name that any desired function can use:
string GetMemberName()
{
string FName, LName, FullName;
6
}
Major = GetMajor();
Here is an example:
#include <iostream.h>
int GetMajor()
{
int Choice;
return Choice;
}
int main()
{
int Major;
7
cout << "Welcome to the student orientation program.";
cout << "Select your desired major:";
Major = GetMajor();
Example
Imagine you want to write a function that would calculate an item’s purchase price based on the item’s
store price added the tax. The tax rate is a percentage value. This means that a tax rate set at 7.50% in
C++ terms is equal to 0.075 (because 7.50/100 = 0.075). The tax amount collected on a purchase is taken
from an item’s price; its formula is:
TaxRate
Tax Amount = Item Price * 100
Here is an example:
#include <iostream.h>
int main()
8
cin >> taxRate;
cout << "\nThe final price is Tsh: " << PurchasePrice(itemPrice, taxRate);
return 0;
double price;
return price;
Exercises:
1. Use functions to write a program that computes the area of a right angled triangle.
2. Using a Function, write a C++ Program that finds the average of any four numbers.