Function in C++ | Fuction Types in C++ - Cyber Thieve

 Function in C++

Function is is a group of instruction that takes input and perform some specified task. Every C++ program has a main() function.

function groups a number of program statements into a unit and gives it a name. This unit can then be invoked from other parts of the program.

Another reason to use functions is to reduce program size. Any sequence of instructions that appears in a program more than once is a candidate for being made into a function. The function’s code is stored in only one place in memory, even though the function is executed many times in the course

Function in C++ | Fuction Types in C++




implementation of a function :

#include <iostream>

using namespace std;

void starline();

//function declaration

//

(prototype)

int main()

{

starline();

//call to function

cout << “Data type

Range” << endl;

starline();

//call to function

cout << “char

-128 to 127” << endl

<< “short

-32,768 to 32,767” << endl

<< “int

System dependent” << endl

<< “long

-2,147,483,648 to 2,147,483,647” << endl;

starline();

//call to function

return 0;

}

//--------------------------------------------------------------

// starline()

// function definition

void starline()

//function declarator

{

for(int j=0; j<45; j++)

//function body

cout << ‘*’;

cout << endl;

}

The output from the program looks like this:

*********************************************

Data type

Range

*********************************************

char

-128 to 127

short

-32,768 to 32,767

int

System dependent

long

-2,147,483,648 to 2,147,483,647

*********************************************

The Function Declaration :

There are two ways to do this. The approach we show here is to declare the function before it is called. function starline() is

declared in the line

void starline();

The declaration tells the compiler that at some later point we plan to present a function called starline. The keyword void specifies that the function has no return value, and the empty parentheses indicate that it takes no arguments. (You can also use the keyword void in parentheses to indicate that the function takes no arguments.

Notice that the function declaration is terminated with a semicolon. It is a complete statement in itself.

Function declarations are also called prototypes, since they provide a model or blueprint for the function.

Calling the Function :

The function is called (or invoked, or executed) three times from main(). Each of the three calls looks like this:

starline();

This is all we need to call the function: the function name, followed by parentheses. The syntax of the call is very similar to that of the declaration, except that the return type is not used.

The call is terminated by a semicolon. Executing the call statement causes the function to execute; that is, control is transferred to the function, the statements in the function definition

The Function Definition :

Finally we come to the function itself, which is referred to as the function definition. The definition contains the actual code for the function. Here’s the definition for starline():


Function in C++ : types of function

build in function 

user defined function


Also Read :

Advantages of Object Oriented Programming
Object and Classes in C++
C++ Object Oriented Programming


Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!