answersLogoWhite

0

What is c plus plus function?

Updated: 10/7/2022
User Avatar

Wiki User

10y ago

Best Answer

A function prototype is just another name for a function declaration which essentially describes the function's interface (as opposed to its implementation). The declaration must include the name of the function, the number and type of its arguments (if any) and the return type. Formal argument names are not required in a prototype, nor is the implementation, but the implementation must be defined somewhere, whether it is in an external library which links to your program, a separate source file within your program, or implicitly inline expanded within the prototype itself.

Prototypes are typically placed in a header file while the implementation is placed in a corresponding source file. Separating interfaces from implementations in this manner modularises your code. When you need to make use of a function, you simply include the header that contains the prototype. The prototype provides all the information required in order to use the function, and tells the compiler all it needs to know in order to match the interface with its definition.

Prototypes are also used to forward declare functions. In C++, a function must be declared before it can be used, even if the function is not fully defined at the point it is first used. By placing prototypes in a header, the programmer does not have to look up the prototype in order to write a forward declaration, he simply includes the header that contains the prototype itself. When you include a header it's just as if you'd copy/pasted the entire contents of that header into your source code at the point of inclusion.

Note that templates must be fully-defined before they can be used. Typically the definition is placed in the same file as the prototype (in some cases inline expanded), but you can also include the definition file in the prototype header in order to modularise interfaces and implementations.

Note also that the definition of a function is also a declaration of that same function, even if it was forward declared. However, there can only ever be one forward declaration and one definition at most of any one function (the "one definition rule", or ODR). This presents a problem when you have several source files that all require access to the same function prototype, since you'd effectively be declaring the function once for each inclusion.

This problem is resolved by using inclusion guards in the header. An inclusion guard is a compiler directive that ensures a file is only ever included once in any compilation. A typical inclusion guard for a file named MyHeader.h is as follows:

#ifndef _MY_HEADER_H_

#define _MY_HEADER_H_

// actual content of header goes here

#endif _MY_HEADER_H_

The first time this file is included, the _MY_HEADER_H_ symbol will not be defined, so the compiler defines it and then includes the content in your source file. The next time the file is included, the compiler will see that it has already defined _MY_HEADER_H_, thus everything up to the corresponding #endif _MY_HEADER_H_ at the end of the file is completely ignored.

The definition must also appear just once in your source. Although the definition is itself a declaration, the compiler knows that if a function declaration has not been defined then it must be a prototype or forward declaration, and will use the prototype to match the declaration with its definition. But once defined, it cannot subsequently be redefined.

User Avatar

Aniya Konopelski

Lvl 10
1y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is c plus plus function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are the building function in c plus plus?

There is no such term as "building function" in C++.


A c plus plus statement that invokes a function is known as?

...a function call.


When will you make a function inline in c plus plus?

yes,we can make function inline


What is the only function all C plus plus programs must contain?

Every C plus plus program that is a main program must have the function 'main'.


In C plus plus when a function is executing what happens when the end of the function is reached?

Control is returning to the caller of the function.


What do you call an object function in c plus plus?

method


What is a main function in c plus plus?

It is the first function that gets called when the program is executed.


What is the function y equals ax2 plus bx plus c?

It is a quadratic function which represents a parabola.


How do you create folder with c plus plus?

Use function mkdir.


Why to use gotoxy function in c plus plus?

to locate coordinates ..


How do you enter a sentence as input in c plus plus?

Use the C++ getline() function from the standard library.


Write a C plus plus function that print a triangle of stars?

Write a function that print a triangle of stars.