In C++, a function must be declared and defined before it is used (called) anywhere in the program.

  1. Dev C 2b 2b Cout Does Not Name A Type 1
  2. Dev C 2b 2b Cout Does Not Name A Type 2

C++ Function Prototype

A function prototype is a declaration of the function that tells the program about the type of the value returned by the function and the number and type of arguments.

Function prototyping is one very useful feature of C++ function. A function prototype describes the function interface to the compiler by giving details such as the number and type of arguments and the type of return values.

The prototype declaration looks just like a function definition except that it has no body i.e., its code is missing. This is the time you knew the difference between a declaration and a definition.

The cout object is ensured to be initialized during or before the first time an object of type iosbase::Init is constructed. After the cout object is constructed, it is tied to cin which means that any input operation on cin executes cout.flush. The 'c' in cout refers to 'character' and 'out' means 'output', hence cout means. C program to demonstrate endl in a program to prompt the user to enter his or her name while using endl to print each statement in the next new line while flushing the output stream: Code: //The header file iostream is imported to enable us to use cout in the program.

Dev C 2b 2b Cout Does Not Name A Type 1

A declaration introduces a (function) name to the program whereas a definition is a declaration that also tells the program what the function is doing and how it is doing.

Thus the above given examples are function definitions and the following are declarations or shall we say, function prototypes :

Therefore, you can say that a function prototype has the following parts :

  • return type
  • name of the function
  • argument list

Let's look at the following function prototype :

Here,

  • return type - int
  • name of the function - sum
  • argument list - (int n1, int n2)

Since every function prototype is followed by a semicolon, so at last there will be ; as in the above function prototype.

Tip - A function declaration can skip the argument names but a function definition, can not.

Use of Void

As you know about void data type that it specifies an empty set of values and it is used as the return type for functions that do not return a value. Thus, a function that does not return a value is declared as follows:

By declaring a function's return type void, one makes sure that the function cannot be used in an assignment statement.

Tip - If a function does not return a result, declare the result type as void.

A function that does not require any parameter (i.e., it has an empty argument list) can be declared as follows:

Tip - If a function takes no argument, you should specify void in its prototype.

Dev C%2b%2b Cout Does Not Name A Type

As already mentioned if you omit the type specifier of a function, it is assumed to be returning int values. For the functions returning non-integer values, the type specifier must be given.

A function prototype can either appear before the definition of calling the function (such prototypes are known as a global prototypes) or within the definition of calling function (such prototypes are known as local prototypes). The global and local prototypes have been described in separate tutorial, in C++ Scope Rules tutorial.

C++ Function Definition

The general form of a function definition is as given below:

Here, the return_type specifies the type of value that the return statement of the functions returns. It may be any valid C++ data type. If no type is specified, the compiler assumes that the function returns an integer value. The parameter list is a common-separated list of variables of a function referred to as its arguments. A function may be without any parameters, in which case, the parameter list is empty. Here are some examples of functions. Below is a function having only one parameter of int type and return type also as int :

Here is another function definition example. This function has return type as int, and have two integer parameters

From the above examples it is clear now that the parameter declaration list for a function takes this general form :

A C++ function can also have an 'open' parameter list i.e., it can have any number as follows :

C++ Function Prototype & Function Definition Example

Let's take an example program, demonstrating function prototype and function definition in C++. This program also uses function calling, which is covered in C++ Calling Function tutorial coming in the next chapter.

Below are the two sample run of this C++ program:


Here is another example, also demonstrating function prototype and function definition in C++. This program uses return_type as void

Here is the sample run of the above C++ program:

Let's take one more example program for the complete understanding on C++ function prototype and function definition

Below is the sample run of the above C++ program:


In this tutorial we will take a look at basic input and output. Using the C++ iostream library we will get the user’s input from the keyboard and we will print messages onto the screen. The iostream library is part of the C++ standard library.

In C++, I/O is performed by using streams. A stream is a “stream of data” in which character sequences are “flow into” or “flow out off.” A stream is an object with properties that are defined by a class. Global objects are predefined for the standard I/O channels.

The header file iostream must be included to make use of the input/output (cin/cout) operators.

Dev C 2b 2b Cout Does Not Name A Type 2

Standard Output (cout)

Dev c 2b 2b cout does not name a type b

By default, the standard output of a program points at the screen. So with the cout operator and the “insertion” operator (<<) you can print a message onto the screen. Let’s take a look at an example:

Note:the double quotes around Hello World (because it is a constant string of characters.)

To print the content of a variable the double quotes are not used. Take a look at an example:

Note: If you don’t want to use the namespace std, you could write std::cout << Yes

The << operator can be used multiple times in a single statement. Take a look at an example:

Note: the use of the white spaces. (Otherwise the sentence: “Hello, this is a test string.” will be printed like this “Hello,this is a test string.”.)

It is possible to combine variables and text:

The cout operator does not put a line break at the end of the output. So if you want to print two sentences you will have to use the new-line character ( n ).
For example:

It is possible to use the endl manipulator instead of the new-line character.
For example:

The endl manipulator will place a new-line character, so the result is the same. The difference is that the endl manipulator will also flush the buffer when you are in buffered mode. (In most cases the cout will be an unbuffered stream, so you could use the n or endl, without any difference in behavior.)

Standard input (cin)

In most cases the standard input device is the keyboard. With the cin and >> operators it is possible to read input from the keyboard.

Take a look at an example:

Note: The input is processed by cin after the return key is pressed.

The cin operator will always return the variable type that you use with cin. So if you request an integer you will get an integer and so on. This can cause an error when the user of the program does not return the type that you are expecting. (Example: you ask for an integer and you get a string of characters.) Later on we will offer a solution to this problem.

The cin operator is also chainable. For example:

In this case the user must give two input values, that are separated by any valid blank separator (tab, space or new-line).

That’s all for this tutorial.

In the next tutorial we will look at standard I/O and strings.

This entry was posted in C++ Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! or use to share this post with others.