80 likes | 93 Vues
Learn about functions in C++, including predefined, programmer-defined, arguments, and return values. Discover how to structure programs in multiple files and manage global constants. Master separate compilation and multiple inclusion protection techniques.
E N D
What Is? • function • predefined, programmer-defined • arguments, (formal) parameters • return value • function call, function invocation • caller, callee • function definition • head, body • function prototype (declaration) • expanded form, abbreviated form • local variable, global constant, scope • call-by-value
Program in Multiple Files when one file is not enough
(Non) Executable Statements • all C++ statements are divided into executable and non-executable • executable – some corresponding machine code is generated by the compiler • example: assignment statements, looping/branching constructs, function invocations • non-executable - no machine code generated • example: function prototypes, variable and constant declarations, #include directives • global constant declarations may look like executable, they are not: const double pi=3.14; the compiler may substitute 3.14 for every occurrenceof pi in the program
Include Files • #include directives tell the compiler to include specified file. The files included are also called header files and commonly have extensions .h • two forms: #include <filename> - file is found in standard system-dependent location #include ”filename.h” - file is found in the same directory as the rest of the code • purpose of include files: centralize declarations • 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
Program in Multiple Files • large programs are usually kept in multiple files • reasons: • easier to maintain • can be compiled separately • functions are usually grouped into files by their purpose: functions dealing with one part of program are kept in one file • scope rule: function, variable, constant have to be declared before use • what if declaration is in a different 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
add1.cpp #include "add1.h" // adds 1, // returns added value int add1(int n) { return n + 1; } add1test.cpp // 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; } Example Program in Multiple Files add1.h // adds one int add1(int);
link object file with standard object files and other object files to produce an executable program Separate Compilation separate compilations source program(add1.cpp) Include files(add1.h, iostream) add include files check object file(add1.o) for errors if none compile it into an object file standard libraries compilation executable program
Multiple Inclusion Protection • 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 textual substitution of name by value • do not use #define instead of global constants problem: #define press 50+5 int myvar = press * 20; // changes order of operations • value may be skipped: #define BIG_NAME #ifdef name - true if name defined, #ifndefname - 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