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

8
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 What Is? 1

Upload: dominic-berry

Post on 05-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

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

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

What Is?

1

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

Program in Multiple Files

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

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

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

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

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

Include Files

4

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

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

Program in Multiple Files

5

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

// adds oneint add1(int);

Example Program in Multiple Files

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

add1.h #include "add1.h"// adds 1,// returns added valueint add1(int n) { return (n + 1);}

add1.cpp

6

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

Separate Compilation

Add include files

Executable program

Source program(add1.cpp)

Checkfile

unit for legalsyntax and

compile it intoan object file

Link object filewith standard

object filesand other

object files toproduce anexecutableprogram

Include files(add1.h, iostream)

compilation

Object file

(add1.o)

Separate compilations

Standard libraries

7

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

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