Presentation is loading. Please wait.

Presentation is loading. Please wait.

L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.

Similar presentations


Presentation on theme: "L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition."— Presentation transcript:

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

2 Program in Multiple Files

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

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

5 l large programs are usually kept in multiple files l reasons: n easy to maintain n can be compiled separately l functions are usually grouped into files by their purpose (functions dealing with one particular part of program are kept in one file) l function invocations, constants and variables cannot be put in program before the corresponding declarations. n what if they are in a separate file? l program is structured as follows: n program file (extension.cpp) - contains function definitions n 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 include d in file AA.cpp Program in Multiple Files 5

6 // adds one int add1(int); Example Program in Multiple Files // uses the function add1 // defined in a separate file #include #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 add1.h #include "add1.h" // adds 1, // returns added value int add1(int n) { return (n + 1); } add1.cpp 6

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

8 l each definition (e.g. global constant def.) can be encountered only once during compilation l when definition is placed in a header file, it may be included multiple times l header file must structured so it is safe in case of multiple inclusion; term – multiple inclusion protection n mechanism - preprocessor directives #define name value textual substitution of name for value do not use #define instead of global constants problem: #define press 50+5 int myvar = press * 20; // changes order of operations #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 Multiple Inclusion Protection 8


Download ppt "L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition."

Similar presentations


Ads by Google