<< Chapter < Page Chapter >> Page >

This program has two functions, one from each of our categories. The technical layout of functions are the same, it is our distinction that creates the two categories based on how a function is being implemented.

Program control function

The main program piece in C++ program is a special function with the identifier name of main. The special or uniqueness of main as a function is that this is where the program starts executing code and this is where it usually stops executing code. It is usually the first function defined in a program and appears after the area used for includes, other technical items, declaration of prototypes, the listing of global constants and variables and any other items generally needed by the program. The code to define the function main is provided; however, it is not prototyped or usually called like other functions within a program. In this simple example, there are no other program control functions.

Specific task function

We often have the need to perform a specific task that might be used in many programs. In the Compile_Test.cpp source code above we have such a task that is used to stop the execution of the code until the user hits the enter key. The functions name is: pause. This function is not communicating any information between the calling function and itself, thus the use of the data type void.

General layout of a function

<return value data type>function identifier name(<data type><identifier name for input value>) {lines of code; return<value>; }

Got questions? Get instant answers now!

There is no semi-colon after the first line. Semi-colons are used at the end of a statement in C++, but not on the first line when defining a function. Functions have a set of braces {} used for identifying a group or block of statements or lines of code. There are normally several lines of code within a function. Lines of code containing the instructions end in a semi-colon. Can you identify the definition of the pause function in the above program example? The pause function definition is after the function main. Though not technically required, most programs list all functions (program control or specific task) after the function main.

Let's identify the location where the function pause is called. The calling function is the function main and it towards the end of the function. The line looks like:

pause();

When you call a function you use its identifier name and a set of parentheses. You place any data items you are passing inside the parentheses, and in our example there are none. A semi-colon ends the statement or line of code. After our program is compiled and running, the lines of code in the function main are executed and when it gets to the calling of the pause function, the control of the program moves to the pause function and starts executing the lines of code in the pause function. When it’s done with the lines of code, it will return to the place in the program that called it (in our example the function main) and continue with the code in that function.

Once we know how to define a function and how to call a function, we usually will need to know how to declare a function to the compiler (called a prototype). Because of normal computer programming industry standards, programmers usually list the function main first with other functions defined after it. Then somewhere in the function main, we will call a function. When we convert our source code program to an executable version for running on our computer, the first step of the process is compiling. The compiler program demands to know what the communication will be between two functions when a function is called. It will know the communication (what going in and out as parameters) if the function being called has been defined. But, we have not defined that function yet; it is defined after the function main. To solve this problem, we show the compiler a prototype of what the function will look like (at least the communication features of the function) when we define it.

void pause(void);

This line of code looks exactly like the first line in our function definition with one important addition of a semi-colon. Prototypes (or declarations to the compiler of the communications of a function not yet defined) are placed near the top of the program before the function main. Summary concept: If you call a function before it is defined you must prototype it before it is called. Looking at our list of the three things you do in conjunction with a function in the order that they normally appear in a program, there is a formal way to:

  1. declare a function (a prototype is a communications declaration to a complier)
  2. call a function
  3. define a function

C++ program layout

From the above example, you can see that 2/3 of the program is the two functions. Most C++ programs have several items before the function main. As in the example, they often are:

  1. Documentation – Most programs have a comment area at the start of the program with a variety of comments pertinent to the program. Any line starting with two slashes // is a comment and the compiler software disregards everything from the // to the end of the line.
  2. #include<iostream>– This line of code inserts a file into the source code. The file contains necessary code to be able to do simple input and output.
  3. using namespace std – The C++ compiler has an area where it keeps the identifier names used in a program organized and it is called a namespace. There is a namespace created in conjunction with the iostream file called: std. This line informs the compiler to use the namespace std where the identifier names in the iostream are established.
  4. Function prototypes have already been explained.
  5. We need some variables (storage areas) for this program to work. They are defined next.

Definitions

modularization
The ability to group some lines of code into a unit that can be included in our program.
function
What modules are called in the two predominant programming languages of today: C++ and Java.
program control
Functions used to simply sub divide and control the program.
specific task
Functions designed to be used with several programs.
parameter passing
How the data is communicated in to and out of a function.
identifier name
The name given by the programmer to identify a function or other program items such as variables.
function prototype
A function's communications declaration to a complier.
function call
A function's using or invoking of another function.
function definition
The code that defines what a function does.
braces
Used to identify a block of code in C++.

Questions & Answers

what is microbiology
Agebe Reply
What is a cell
Odelana Reply
what is cell
Mohammed
how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals - a modular structured approach using c++. OpenStax CNX. Jan 10, 2013 Download for free at http://cnx.org/content/col10621/1.22
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals - a modular structured approach using c++' conversation and receive update notifications?

Ask