answersLogoWhite

0


Best Answer

A function requires an include directive whenever it makes use of a data type or function that cannot be forward declared, or where a forward declaration would be undesirable, and where a complete declaration of that type or function exists in another file.

That may sound far more complex than intended, but it's really quite simple. The include directive is a signal to the compiler that the specified file is to be inserted into your file in its entirety, just as if you'd typed all the code it contains by hand. This is clearly a huge time saver when it comes to common data types and functions, but how do you decide if an include directive is actually required or not? Simple: comment out the directive and attempt to compile. If it fails, the directive is required. The errors raised will indicate exactly what types or functions it exposes and where they are used.

Most C/C++ programmers split their declarations from their definitions. Declarations of functions and data types (including class declarations) are usually placed in a header file (.h), while the definitions of the functions are safely tucked away in a separate source file (.cpp). Programmers are rarely interested in the implementations -- the header should contain all the information necessary to make use of the functions and data types contained therein. But in order to compile, the source file must include the header file. And if the two files must include other files, then we generally place those directives in the header file thus ensuring that the header contains all the necessary information to allow it to be included in other files.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

8y ago

You use the #include directive whenever you need to include the contents of one file in another (just as if you'd typed the contents out in full). This is typically done to import declarations and definitions from a shared source file, such as a library header, however headers also ensure consistent definitions across all translation units that use them, thus ensuring those definitions adhere to the one definition rule (ODR).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When does a function need an include directive?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are its program components?

the main() function,the#include Directive, the variable definition. function prototype, program statements, the function definition,program comments and braces are the components of program in C++


What is function of include in c language?

The #include directive is used to tell the preprocessor that the specified file's contents are to be included at the point where the directive appears, just as if you'd typed those contents in full yourself. Include files are primarily used to organise declarations of external variables and functions, complex data types, constants and macro definitions. The code need only be declared once, and included wherever required. Think of include files as a means of providing forward declarations without having to retype those declarations in full. The definitions of those declarations needn't be contained in the included file, but they must be made available to the program, either as a linked library or as a separate source code file which includes those same declarations. The include keyword is used in C to tell the linker what libraries your code is going to be using.


What are the three required sets of statements for every function that a programmer writes in C plus plus?

There is no requirement for any statement in a C++ function, let alone three sets of statements. For instance, the following is a perfectly valid function: void foo(){} Clearly this does nothing as it has no statements in the function body, but it is nevertheless a valid function. Perhaps you mean something else by "statements". The only requirement of a function is that it have a return type, a valid name, an argument list and a function body. The return type may be void, of course, and the argument list may be empty, but it must include the ellipses. The function declaration need not include the function body, and the argument list need only specify the type of argument (the argument names are optional and need not match those declared in the actual definition). The function name and the arguments define the function signature (the prototype), thus the three required "components" of a function are the return type, the signature and the function body.


What type of language is directive?

Assembler.


Which library header is to make sqrt function?

#include <cmath> // simple version #include <complex> // complex version

Related questions

What are its program components?

the main() function,the#include Directive, the variable definition. function prototype, program statements, the function definition,program comments and braces are the components of program in C++


What is a pre-processor directive?

#include is a preprocessor directive. So is #define, #if, etc


In your program what if you do not provide the following directive in your code using namespacestd hint what will you need to do?

If you do not include the directive using namespace stdin your program, any references to objects in namespace std will need be be qualified with that namespace. For instance...cout


What is function of include in c language?

The #include directive is used to tell the preprocessor that the specified file's contents are to be included at the point where the directive appears, just as if you'd typed those contents in full yourself. Include files are primarily used to organise declarations of external variables and functions, complex data types, constants and macro definitions. The code need only be declared once, and included wherever required. Think of include files as a means of providing forward declarations without having to retype those declarations in full. The definitions of those declarations needn't be contained in the included file, but they must be made available to the program, either as a linked library or as a separate source code file which includes those same declarations. The include keyword is used in C to tell the linker what libraries your code is going to be using.


How do you create a user defined header file?

Header files are not much different from usual cpp files. There are basically two different things. It's file extension: you need to choose "header file" when you create it or save as .h file. Second is header files do not have main() function. When you are done with you header file do not forger to include it in your project by writing preprocessor directive:#include "your_header_file.h"


How do you include a file in PHP?

To include a file in PHP all you need to do is use the include() function as I have shown you in the example below. <?php include("filename.php"); ?>


Is cognitive therapy directive or non-directive?

Cognitive therapy is considered directive. In cognitive therapy, the therapist actively collaborates with the client to identify and change negative thought patterns and behaviors. The therapist guides the client in challenging these beliefs and developing more adaptive ways of thinking.


What is directive and non-directive approach to rural development?

comprehensively describe the directive and non-directive approach to rural development


What is need of header file?

The compiler can only see one translation unit at a time, so every translation unit that calls a function (but does not define it) must at least declare that function. Headers allows us to share declarations consistently amongst many translation units with a simple #include directive. Headers can also declare data types, aliases, constants and macros. Functions can also be defined in headers, but these must be declared inline.


How do you correct the C plus plus programming error missing function header?

You need to #include the header file that contains the missing function's declaration.


What is the difference between a DoD manual and a DoD directive?

Directives establish or describe policy, programs, and organizations. They also define missions, provide authority, and assign responsibilities. Instructions include more detail on how to implement a directive.


Why do you include count plus plus in addition of matrix?

C++ doesn't contains all operations you may need on your program so in some cases you need to include a specific class that contains the function you need the is here you need to include count class cuse C++ can't add matrices so you have to use the include ;)