You are on page 1of 10

Topic:

I/O Constructs

 Unsigned integers can be read and written using the I, B, O, and Z edit
descriptors.
 They can also be read and written using list-directed and namelist I/O. The
written form of an unsigned integer under list-directed or name list I/O is the
same as is used for positive signed integers.
 Unsigned integers can also be read or written using unformatted I/O.

Basic Input / Output in C++


C++ comes with libraries that provide us with many ways for performing input
and output. In C++ input and output are performed in the form of a sequence of
bytes or more commonly known as streams.

 Input Stream: If the direction of flow of bytes is from the device (for
example, Keyboard) to the main memory then this process is called input.

 Output Stream: If the direction of flow of bytes is opposite, i.e. from main
memory to device (display screen) then this process is called output.
include <iostream>

using namespace std;

int main()

    char sample[] = "GeeksforGeeks";

    cout << sample << " - A computer science portal for geeks";

    return 0;

Output: 
GeeksforGeeks - A computer science portal for geeks
#include <iostream>

using namespace std;

int main()

    int age;

    cout << "Enter your age:";

    cin >> age;

    cout << "\nYour age is: " << age;

    return 0;

Input : 
18
Output: 
Enter your age:
 Your age is: 18

Un-buffered standard error stream (cerr):


The C++ cerr is the standard error stream that is used to output
the errors. This is also an instance of the ostream class. As cerr in
C++ is un-buffered so it is used when one needs to display the
error message immediately. It does not have any buffer to store
the error message and display it later.
 The main difference between cerr and cout comes when you
would like to redirect output using “cout” that gets redirected
to file if you use “cerr” the error doesn’t get stored in file.(This
is what un-buffered means ..It can’t store the message)
The above program asks the user to input the age. The object cin
is connected to the input device. The age entered by the user is
extracted from cin using the extraction operator(>>) and the
extracted data is then stored in the variable age present on the
right side of the extraction operator.

C++

#include <iostream>

using namespace std;

int main()

    cerr << "An error occurred";

    return 0;
}

Output: 
An error occurred
 Buffered standard error stream (clog): This is also an
instance of ostream class and used to display errors but unlike
cerr the error is first inserted into a buffer and is stored in the
buffer until it is not fully filled. or the buffer is not explicitly
flushed (using flush()). The error message will be displayed on
the screen too.
C++

#include <iostream>

using namespace std;

int main()

    clog << "An error occurred";

    return 0;
}

Output: 
An error occurred

Topic:

Modular programming is a software design technique that


emphasizes separating the functionality of a program into independent,
interchangeable modules, such that each contains everything necessary to
execute only one aspect of the desired functionality.

Concept of Modularization:

One of the most important concepts of programming is the ability to group


some lines of code into a unit that can be included in our program. The
original wording for this was a sub-program. Other names include: macro,
sub-routine, procedure, module and function. We are going to use the
term function for that is what they are called in most of the predominant
programming languages of today. Functions are important because they
allow us to take large complicated programs and to divide them into smaller
manageable pieces. Because the function is a smaller piece of the overall
program, we can concentrate on what we want it to do and test it to make
sure it works properly. Generally, functions fall into two categories:

1. Program Control – Functions used to simply sub-divide and control the


program. These functions are unique to the program being written. Other
programs may use similar functions, maybe even functions with the same
name, but the content of the functions are almost always very different.
2.
3. Specific Task – Functions designed to be used with several programs.
These functions perform a specific task and thus are usable in many
different programs because the other programs also need to do the specific
task. Specific task functions are sometimes referred to as building blocks.
Because they are already coded and tested, we can use them with confidence
to more efficiently write a large program.
The main program must establish the existence of functions used in that
program. Depending on the programming language, there is a formal way
to:

1. define a function (its definition or the code it will execute)


2. call a function
3. declare a function (a prototype is a declaration to a compiler)

Program Control functions normally do not communicate information to


each other but use a common area for variable storage. Specific Task
functions are constructed so that data can be communicated between the
calling program piece (which is usually another function) and the function
being called. This

Program Control Function:

The main program piece in many programming languages 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 often 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.

Specific Task Function:

We often have the need to perform a specific task that might be used in
many programs.

General layout of a function in a statically-typed language such as C++, C#,


and Java:

<return value data type> function identifier name(<data type> <identifier


name for input value>) {

//lines of code;

return <value>;
}

General layout of a function in a dynamically typed language such as


JavaScript and Python:

function identifier name(<identifier name for input value>) {

//lines of code;

return <value>;

def function identifier name(<identifier name for input value>):

//lines of code

return <value>

In some programming languages, functions have a set of braces {} used for


identifying a group or block of statements or lines of code. Other languages
use indenting or some type of begin and end statements to identify a code
block. There are normally several lines of code within a function.

Programming languages will either have specific task functions defined


before or after the main function, depending on coding conventions for the
given language. After our program is compiled and running, the lines of
code in the main function are executed, and when it gets to the calling of a
specific task function, the control of the program moves to the function and
starts executing the lines of code in the 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.

Program Layout:

Most programs have several items before the functions, including:

1. Documentation – Most programs have a comment area at the start of the


program with a variety of comments pertinent to the program.
2. Include or import statements used to access standard library functions.
3. Language-specific code such as namespace references or function
prototypes.
4. Global or module-level constants and variables, when required.

Key Terms:

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

You might also like