1 / 8

What Is?

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

codye
Download Presentation

What Is?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Program in Multiple Files when one file is not enough

  3. (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

  4. 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

  5. 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

  6. 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);

  7. 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

  8. 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

More Related