In this chapter, you discover the purpose and use of programming concepts such as identifiers (variables, constants), containers (arrays, vectors), functions, and objects (properties, attributes, methods). This chapter covers CompTIA IT Fundamentals+ objective 4.3.
In programming, an identifier is a name assigned by the user to a program element such as a variable or constant.
A variable is a value paired with a symbolic name, enabling the program to refer to the name in calculations. For example, if x=3 and y=4, you can use the expression x × y to calculate the result (in base 10) of 12.
In programming, a constant is a value that cannot be altered by a program. Sometimes, constants are also called literals. Constant types include the following:
Integer: A whole number, such as 3.
Floating point: A number with a decimal, such as 54.4.
Character: A single character, such as ‘z’. Escape sequences, which are used to control printer or display behavior, are also considered characters, such as ‘ ’ (new line). A universal character (Unicode character set or UCS) such as ‘u02C0’ (caron character) is also considered a single character. Character variables are enclosed in single quotes.
String: A group of characters, escape sequences, or universal characters, such as “Hello, World!” String constants use double quotes.
In programming, a container is an object that can store other objects. Typically, the objects in a container include an array or vector to store data as well as objects for viewing, loading, and unloading data from the storage object.
Note
Containers are known as collections in some languages, such as Java.
An array is a collection of the same type of object with a fixed maximum size. If an array needs to grow to hold more data, the programmer must allocate a new array that is large enough to hold the additional data and copy the old array’s contents to it. For this reason, an array should be used only if the programmer knows in advance the maximum number of objects it needs to hold.
A vector is a resizable collection of the same type of object. Unlike an array, a vector can be resized as needed and new objects can be inserted anywhere into the collection as needed.
Note
For a useful discussion of arrays versus vectors in C++, see https://stackoverflow.com/questions/4424579/stdvector-versus-stdarray-in-c.
A function is a subroutine or a procedure inside a larger program. Programming languages usually have built-in functions (also known as library functions).
To use a particular set of functions, the programmer must call (request) the function library in the program. For example, to use a mathematical function in C, such as abs (absolute value of an integer) or fmax (larger of two floating-point values), the math.h header (cmath header in C++) must be included in the program. To use an input/output function such as fopen (opens file) or fread (reads from file), the stdio.h header (cstdio header in C++) must be included in a program. To learn more about C++ functions, see https://www.tutorialspoint.com/cplusplus/cpp_functions.htm. To learn more about C functions, see https://www.tutorialspoint.com/cprogramming/c_functions.htm.
A user-defined function is defined at the top of a program. This enables the function to be referred to repeatedly in a program and the compiler to compile the code before it is used. For example, a function called drawshape is defined from the lines of code needed to draw a shape. However, after it is defined, the programmer can simply refer to the function drawshape and the program will locate the function and repeat it.
A function library is a collection of procedures that can be used (called) by other programs. By using a function library, a programmer can reduce development time by not needing to develop new code to perform a particular task. Some function libraries are included in a language, but others can be licensed from third-party developers.
In programming, an object is a combination of data, its properties, its attributes, and procedures that can be used with that data, also known as methods. Object-oriented programming builds programs from objects.
Note
A useful, illustrated tutorial on object-oriented programming is available at https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html.
The exact meaning of properties in object-oriented programming varies with the language in question. Some languages define an object’s properties as the values associated with an object. For example, in JavaScript, in the variable (var) person, the firstname, lastname, age, and eyecolor can be defined as properties. To use a property, specify person.property.
However, in C#, properties are used to enable read, write, or compute access to a private field. This protects the contents of the data while enabling safe access. The access methods are often referred to as getter/setter.
An attribute is a characteristic of an object. For example, a race car object in a racing game could have these attributes: car number, sponsors, driver, fuel type, manufacturer, racing team, tire type, tire pressure, and so on.
Methods, also known as member functions, define what an object can do. For example, in a racing game, a race car object could have methods such as start race, end race, drive, pit stop, accelerate, steer, brake, crash, and coast.
Review the most important topics in this chapter, noted with the Key Topics icon in the outer margin of the page. Table 24-1 lists these key topics and the page number on which each is found.
Key Topic Element |
Description |
Page Number |
Paragraph |
Variables |
|
List |
Constant types |
|
Paragraph |
Arrays |
|
Paragraph |
Vectors |
|
Paragraph |
Function library |
|
Paragraph |
Objects |
Define the following key terms from this chapter and check your answers in the glossary:
1. _____ define(s) what programming objects can do. (Fill in the blank.)
Properties
OOP
Methods
Getter/setter
2. An object’s characteristics are called _______. (Fill in the blank.)
methods
attributes
properties
functions
3. You are creating a container to hold employee information. For the current workforce, you need space for 500 records. However, the company is planning to expand its workforce by an unknown amount. What type of object should you create to hold employee information?
Vector
Method
Array
Object
4. You are writing a program in C that will use mathematical functions. You need to include which of the following headers in your program?
abs
cmath
math.h
cstio
5. You are writing a program that needs a complex function. Instead of writing the function yourself, you can use a ___________. (Fill in the blank.)
user-defined function
function library
object
property
6. An attribute is a _________ of an object. (Fill in the blank.)
property
characteristic
method
function
7. After a conference, you are given a list of attributes for an employee. One of the potential attributes is actually a method. Which of the following is it?
Last name
Sign in
Shift
Employee number
8. You need to add a user-defined function to your program. Where should it be located?
Anywhere in the program code
End of the program
Immediately before its first use
Top of the program
9. At the beginning of your program, you enter several lines of code to create a function you call makeshape. To use the same procedure elsewhere in your program, which of the following should you do?
Reenter the code.
Refer to makeshape.
Add a line number for makeshape and use a GOTO statement.
Create a function library file for makeshape.
10. Which of the following is not a constant?
The number 3
A string of characters such as “Good morning”
Current date
The number 23.1
11. You are creating a program that calculates the number of days between the current date and the user’s birthday. Which of the following do you need to set up to get the user’s birthday?
Variable
Constant
Function
Container
12. An object is a combination of all of the following except:
Containers
Properties
Attributes
Procedures
13. Identifiers include which of the following?
Arrays and vectors
Properties and attributes
Variables and constants
Functions and methods
14. In JavaScript, in the variable (var) person, the firstname, lastname, age, and eyecolor can be defined as _________. (Fill in the blank.)
functions
constants
vectors
properties
15. A function is a(n) _________or _________inside a larger program. (Fill in the blanks.)
identifier or container
escape sequence or universal character
subroutine or procedure
conjunction or junction
As we said in Chapter 22, “Compare and Contrast Programming Language Categories,” the best way to learn to program is… wait for it… to program! There are many degree programs available, but you can get started by downloading languages, using online interpreters and compilers, and learning to solve problems you run into at work. See Chapter 22 for online resources, including interpreters and compilers where you can practice safely.