Showing posts with label variables. Show all posts
Showing posts with label variables. Show all posts

Sunday, January 22, 2012

Introduction to C++ Functions


This blog assumes that the reader understand how to create a C++ program containing a single statement that displays the phrase "Hello World!" Therefore this blog focuses solely on functions and does not explain the main function nor the class libraries.

A function is a group of C++ statements that collectively perform a specific well defined task. Functions can be executed repeatedly throughout a program. The advantage of a function is that the programmer does not have to repeat the C++ statements each time the task needs to be performed.

First, I will explain each statement in the example, then I will present the complete tested and debugged example.

/* The first statement lists the iostream class. */

#include <iostream>

/* The second statement invokes namespace. */

using namespace std;

/* For more details on iostream and namespace see my blog titled
An Introduction to C++ Programming. */

/* Now we can insert our function header. You can name your function any name you like. I recommend that the function name describes what the function does. This will make it easier to understand the program when you come back to modify it. The word void at the far left of the function name means that the function does not return anything to the calling function. In this example, the calling function is the main function. The word void in the parenthesis means nothing is passed to the function from the main function. */

void Display_Hello_World(void)

/* The beginning of the function is defined by an opening curly bracket. */

{
/* The function only has one statement. That statement displays the words "Hello World" on the monitor. The cout function is defined in the iostream class library. */

cout << "Hello World!" << endl;

/* The end of the function is defined by a closing curly bracket. */

}

/* The main function invokes the function name Display_Hello_World. */

int main (void)
{
Display_Hello_World();
}

/* A function can return a value. The following function declares an integer, assigns a value to that integer and returns the integer value to the calling function. */

int FReturn_A_Value (void)
{
int a_value = 5;
return a_value;
}

/* The main function calls the FReturn_A_Value funtion and  and stores the value returned in my_value. */

int main (void)
{
int my_value;
my_value = FReturn_A_Value();
}

/* A variable can be passed to a function. The function FPass_A_Variable accepts an integer from the main function and displays it. */

void FPass_A_Variable(int my_value)
{
cout << my_value << endl;
}

/* The main funtion declares an integer, assigns the integer the value of 5 and passes the value to the function FPass_A_Variable. */

int main (void)
{
int value = 5;
FPass_A_Variable(value);
}

We can implement each defined task in a function. The function makes the program easier to read and easier to troubleshoot. The name of the function should describe what the function does. Likewise, the name of the variables should describe those variables. We know that the variable is an integer. The variable name should describe what the variable is.

An example is:
int temperature;

Here are examplies of function names
Read_Temperature().
Test_temperature_range().
Display_temperature().
Display_warning_message().

Note how easy it is to understand each function does.
The more clearly one describes variables and functions, the easier it is to read and understand the program.
The function should execute a single well defined task. The inputs and outputs of each function could be defined in the comments before the actual function.

/*****************************************************************
Function name
Inputs: Description of each input
Outputs: Describe the each output
Description: Describes what the function does
*********************************************************************/
/* Place function here */

The advantage of these comments is the time it saves if the programmer should have to modify the program months or years after the program was designed.

Friday, January 13, 2012

INTRODUCTION TO THE C++ PROGRAMMING LANGUAGE



I orginally published this article on Yahoo Voices under the pen name John Mario.

The purpose of this series of articles is to explain the C++ programming language to the reader who wants to learn C++.

OVERVIEW OF THE C++ PROGRAMMING LANGUAGE

The C++ language is a versatile, structured programming language with extensive libraries of functions available for the programmer's use. The language is used for console applications, windows applications and application in modern appliances and industrial equipment.

WHAT IS A PROGRAM?

A program is a series of steps the computer performs to attain a specific result of set of results. The steps are written in a specific programming language such as C++. The programming language consists of a number of instructions that can be used in the design of any computer program. Every C++ instruction is terminated with a semi-colon.

A console application is a program that uses the old DOS-like screen. A console application does not use windows. Each output is sent directly to the monitor.

C++ LIBRARIES

Libraries contain functions for the programmer's use. A function is a routine that performs a specific operation. For example, the square root function takes the square root of a number. The C++ programming language includes a class library and a standard function library derived from the C language. A class contains an encapsulated group of related functions and variables. Variables are defined in this article. Classes and functions will be discussed in future articles.

In order to display anything on the monitor, we include the library class iostream. The library class iostream is a library of generic input and output functions provided for the programmer with any C++ compiler. To access the functions in iostream we need an include statement. The include statement of the library class iostream. begins with the pound sign.

# include < iostream > // include the library class iostream.
// note that spaces are not allowed between < and >
// spaces were included to overcome a text editor problem.

NAMESPACE

Namespace prevents conflicts between programmer functions and library functions having the same name. This statement should be inserted below the include statement in every C++ program.

using namespace std;

Without the statement 'using namespace std', the compiler will return numerous errors.

THE MAIN FUNCTION

Every C++ program must have a 'main' function. The main function is not part of iostream. The main function defines the start of the program. The 'main' function is implemented as follows:

int main (void)

{

// The program is typed in between the two curly brackets

}

Let's examine the 'main' function.

int main (void)

The keyword int means the function 'main' will return an integer value. An integer value is a whole number. In the program at the end of this article, a zero is returned.

The space between the parentheses is used to pass information to a function. (void) means no variables are passed to the 'main' function.

COMMENTS

Any program should also contain comments. Comments help a new programmer understand the program and are often helpful when modifying an old program.

The C++ compiler recognizes any line that begins with a double slash // as a comment.

// This line is a comment

A comment could also start with /* and end with */

/*

This line is a comment

This line is another comment.

There is no need for two slashes because we are using the slash and asterisk

*/

If you have a comment that is several lines long, you might try this technique so your comments clearly stand out

/*******************************************************
Add your long comment here.
It could be a long as you want.

********************************************************/

KEYWORDS

The C++ language uses keywords for types of data. The keywords int, double, float and char are common keywords used in programs. The keyword int means integer.

examples:

59 is an integer

A double is a number containing a decimal point

54.35 is a double

A floating point number is a number like 5.3 x 10^-2

5.3e-2F is a floating point number. The keyword for a floating point number is float. 'e-2' means x 10^-2 wherein ^ means 'raised to the power of'

Char stands for character. A character is any key on your keyboard. A single character is enclosed in single quotation marks

'a' is a char.

'1' is a char.

';' is a char.

VARIABLES

A variable is a name assigned to a memory location. A declaration defines exactly what is stored in that memory location.

In the following instruction, the keyword 'int' declares that the variable first_value will hold an integer. Only integers can be stored in the memory location named first_value.

int first_value;

The following instruction declares that the variable my_number will hold a double.

double my_number;

The following instruction declares that the variable my_floating_pt will hold a floating point number.

float my_floating_pt;

The following instruction declares that the variable my_character will hold a character.

char my_character;

ASSIGNMENT STATEMENTS

An assignment statement assigns a value to a variable.

Consider the following examples;

int a;

a = '1'; //this line will deliver an error because

// '1' is a character

a = 1; // this instruction places the integer 1 in the memory location named a.

char b;

b = '1'; // this instruction places the character '1' in the memory location named b.

Instead of using one instruction to define each variable, we can use one instruction to define all three variables by listing the variables and separating them with commas.

int first_value, second_value, sum_of_values;

To store a value to a memory location, we use an assignment statement

Consider the following instructions.

int first_value;

first_value = 5; // The integer 5 is stored is stored in

// the memory location named first_value

The above two statements can be combined.

The following statement declares the variable first_value to hold an integer and assigns it a value of 5.

int first_value = 5;

First_value can be changed inside the program

Example:

#include < iostream > // spaces are not allowed between < and >

using namespace std;

int main (void)

{

int ax = 4;

// Later in the program, the value of ax is changed.

ax = 5; //okay

}

CONSTANTS

A constant is a value that cannot be changed. The keyword for constant is CONST. Consider the following declaration:

const char my_constant = 'a';

This statement declares that the variable my_constant will hold a the character 'a'. If the programmer tries to change the character, the compiler will return an error.

Example:

#include < iostream > // note: spaces are not allowed between < and >
// spaces were included to overcome text editor problem.

using namespace std;

int main (void)

{

int a;

a = 5;

const char my_constant = 'a';

my_constant = 'b'; //error: cannot change a constant.

}

COUT AND CIN FUNCTIONS

The cout function is a member of the iostream class library.

'Cout' sends messages to be displayed on the monitor. Consider the following short program.

#include < iostream > // spaces are not allowed between < and >

using namespace std;

int main (void)

{

int a, b, c;

a = 5,

b = 10;

c = 20;

cout << a << ' ' << b << ' '<< c << endl;

cout << "end" << endl;

}

The output of this program excerpt would be

5 10 20

end

The cin function is also a member of the iostream class library.

The cin function reads an entry from the keyboard and stores it in a memory location.

int my_number;

cin >> my_number;

The instruction cin >> my_number reads an integer from the keyboard and stores it in the memory location named my_number. If the user types anything but an integer, the cin function will fail.

PERFORMING ADDITION

The following instruction adds the values in first_value and second_value

and stores the result in sum_of_values.

sum_of_values = first_value + second_value;

WRITE PROGRAM IN THE ENGLISH LANGUAGE

Let's consider a program that adds two integers and saves the result. An integer is a whole number.

First we'll state the individual steps of the program in English.

A variable is the name of a memory location that contains data. When we declare a variable, we must declare what type of data that variable will contain.

Define three variables as integers. Name them first_value, second_value and sum_of_values.

Prompt the user assign integer values to first_value and second_value.

Add first_value and second_value and store the sum in sum_of_values.

Display first_value, second_value and sum_of_values.

An explanation of this sample program follows the program.

A SAMPLE C++ PROGRAM

#include < iostream > // spaces are not allowed between < and >

using namespace std;

int main (void)

{

// Define the variables first_value, second_value and sum_of_values as integers

int first_value, second_value, sum_of_values;

//Display the phrase "Summation of Integers" on the monitor.

cout << "Summation of Integers" << endl;

// 'endl' means it is the end of the line.

//Prompt the user to enter the first integer

cout << "Enter first integer: ";

//Read the first integer (from the keyboard) and

//place it in the memory location named first_ value

cin >> first_value;

//Prompt the user to enter the second integer

cout << "Enter second integer" << endl;

//Read the second integer and place it in the memory location named second_value

cin >> second_value;

// Add first_value and second_value

// and store the result in sum_of_values.

sum_of_values = first_value + second_value;

// Display the phrase "first_integer = " and the value of the first integer.

cout << "first integer = " << first_value << endl;

// Display the phrase "second_integer = " and the value of the second integer.

cout << "second_integer = " << second_value << endl;

// Display the phrase " first integer + second integer = " and the sum of the two integers.

cout << " first integer + second integer = " << sum_of_values << endl;

// Our program returns a value of zero.

return 0;

}

References:

I have a Bachelor of Science degree in Electrical Engineering and worked as an embedded systems software engineer.

"The Complete Reference C++" Fourth edition ISBN 0-07-222680-3

My experience as an embedded systems software engineer designing and testing C++ applications.