Presentation is loading. Please wait.

Presentation is loading. Please wait.

What Is? function predefined, programmer-defined

Similar presentations


Presentation on theme: "What Is? function predefined, programmer-defined"— Presentation transcript:

1 What Is? function predefined, programmer-defined
arguments, (formal) parameters return value function call, function invocation function definition head, body function prototype (declaration) expanded form, abbreviated form local variables, global variables, scope call-by-value

2 Programming in Multiple Files
2

3 (Non) Executable Statements
all C++ statements are divided into executable and non-executable executable - some corresponding machine code is generated by the compiler examples: assignment statements, looping/branching constructs, function invocations non-executable - no machine code generated examples: function prototypes, global variable and constant declarations, #include directives global constant declarations may look like executable – but they are not: const double PI=3.14; the compiler substitutes 3.14 for every occurrence of PI in the program

4 Include Files #include instructions tell the compiler to include a specified file. The files included are also called header files and commonly have extensions .h two forms: #include <filename> - the file is found in a standard system-dependent location #include ”filename.h” - the file is located in the same directory as the rest of the code the include directives are processed before the rest of the compilation include files may also contain include directives what to put in include files - non-executable statements what not to put in include files - executable statements, function definitions purpose of include files - centralize declarations

5 Program in Multiple Files
large programs are usually kept in multiple files reasons: easy to maintain can be compiled separately functions are usually grouped into files by their purpose (functions dealing with one particular part of program are kept in one file) function invocations, constants and variables cannot be put in program before the corresponding declarations. what if they are in a separate file? program is structured as follows: program file (extension .cpp) - contains function definitions include file (extension .h) - contains corresponding function prototypes, global constant and variable declarations if function A defined in file AA.cpp needs to call a function B which is defined in a different file BB.cpp - the corresponding header file BB.h is included in file AA.cpp

6 Example Program in Multiple Files
#include "add1.h" // adds 1, // returns added value int add1(int n) { return (n + 1); } add1.cpp add1.h // adds one int add1(int); // uses the function add1 // defined in a separate file #include <iostream> #include "add1.h" int main() { // get the number cout << "Enter a number: "; int n; cin >> n; // find the number plus 1 int newn = add1(n); // print out the number plus 1 cout << newn << endl; } add1test.cpp

7 Separate Compilation Separate compilations Source program (add1.cpp)
Include files (add1.h, iostream) Link object file with standard object files and other object files to produce an executable program Add include files Check Object file (add1.o) file unit for legal syntax and compile it into an object file Standard libraries compilation Executable program

8 Preprocessor Directives
each definition (e.g. global constant def.) can be encountered only once during compilation when definition is placed in a header file, it may be included multiple times header file must structured so it is safe in case of multiple inclusion; term – multiple inclusion protection mechanism - preprocessor directives #define name value note that substitution is textual problem: #define press 50 (later) int press=20; The name in the declaration gets substituted, creating a syntax error. #ifdef name - true if name defined, #ifndef name - true if not #endif - completes #if header file myheader.h containing definitions usually has the following structure: #ifndef MYHEADER_H #define MYHEADER_H // text of the header file goes here #endif


Download ppt "What Is? function predefined, programmer-defined"

Similar presentations


Ads by Google