Cpp2 Functions

25
C++ Lecture 2 Friday 11 July 2003

Transcript of Cpp2 Functions

Page 1: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 1/25

C++

Lecture 2Friday 11 July 2003

Page 2: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 2/25

Chapter 3, Functions

built-in functions

function prototype, functiondefinition and use

storage class and scope

recursion

inline function

default argument, functionoverloading, template

Page 3: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 3/25

Math Library Functions

#include <cmath> // to use math

// library On Unix, special compiler flag is

needed

gxx file.cpp -lm

most math functions takedouble as argument and returndouble as value

C.f. math.h

Page 4: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 4/25

Math Functions

ceil(x), cos(x), exp(x), fabs(x),

floor(x), fmod(x,y), log(x),log10(x), pow(x,y), sin(x),

sqrt(x), tan(x)

For some compilers (such as

GNU C++), there are also some

special functions, such as err 

function, bessel function etc.

C.f. Fig.3.2 h

Page 5: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 5/25

Functions

Organization of Large Program

Separate tasks into functions Group related functions in

separate files.

Typical ProgramPro

to

types;main() { « ;}

func1() { «; }

func2(int i, «) { « }

Page 6: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 6/25

Functions

Function prototype

function definition use of function

argument passing in C++

C.f. Fig.3.3

Page 7: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 7/25

Function Prototypes

Format

void maximum(int, int, int); Location of function prototype

in file

When can we omit functionprototype?

Advantage of prototype

Page 8: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 8/25

Storage Classes

Storage class determines the

period during which an identifier exists in memory

Four storage classesauto, register, extern, static

C.f. Fig. 3.12

Page 9: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 9/25

Auto Storage Class

Local variables in functions or 

blocks. Auto storage classvariables are created only when

the block is active, and

disappear when the block or 

function exits.

Page 10: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 10/25

Register Storage Class

Variable existence like auto.

The register variable is asuggestion to the compiler to

put the variable in a CPU

register.

Page 11: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 11/25

Static Storage Class

Local static variable exists

during the whole programexecuting, i.e., the variable

retains its value between

function calls.

However, the reference to the

variable is local in the function

or block.

Page 12: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 12/25

Extern Storage Class

Global variables and function

names have the storage classextern. Extern storage class

variable exists during the whole

program execution.

Page 13: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 13/25

Scope Rules

The places in code segment

that an identifier is visible: function scope

file scope

block scope function-prototype scope

class scope

Page 14: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 14/25

Storage Class and

Scope

Storage class says when a

variable exists scope says where in program

segment the variable is valid for 

reference

Page 15: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 15/25

Unary Scope Resolution

Operator ::

Using ::, one can access an

global variable even if it is over-shadowed by a local variable of 

the same name.

Page 16: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 16/25

Recursion

A function can also call itself,

this is known as recursion To avoid infinite recursion, one

must have a terminating

condition in the function

recursion v.s. iteration

C.f. Fig.3.14

Page 17: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 17/25

Inline Functions

Advantage: function call

overhead is eliminated, thusfaster and less memory

consuming

Disadvantage: the code is

expanded during compilation so

that executable file is large

C.f. Fig. 3.19

Page 18: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 18/25

Call by Value v.s.

Call byR

eference

Call-by-value: the function

takes/works on a copy of theoriginal variable

Call-by-reference: the function

works on the original variable

passed by caller.C.f. Fig. 3.20

Page 19: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 19/25

Call by Reference

int func(int &); // prototype

int main()

{func(x); // call as usual

}

int func(int &x) // x is ref to int

{x = ..; // use x as usual

}

Page 20: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 20/25

Call by Reference Using

Pointer int func(int *); // prototype

int main()

{func(&x); // call as usual

}

int func(int *x) // x is ref to int

{*x = ..; // use x as usual

}

Page 21: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 21/25

Default Arguments

The right-most arguments, if 

omitted, take the default value

Default values are specified at

the first occurrence (prototype

or function definition) of the

functionC.f. Fig.3.23

Page 22: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 22/25

Function Overloading

Several functions of different

argument types can use thesame function name. E.g. we

can define a function square to

work on int as well as on

double. In fact, we need to write two

functions to handle two cases.

C.f. 3.25

Page 23: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 23/25

Function Template

A function definition with

unspecified data type.

The type is determined

according to its use at compile

time.

Page 24: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 24/25

Exercise, p.243, 3.22

Write a function that displays at the

left margin of the screen a solid

square of asterisks whose side is

specified in integer parameter side.

E

.g. side = 4 displays****************

Page 25: Cpp2 Functions

8/9/2019 Cpp2 Functions

http://slidepdf.com/reader/full/cpp2-functions 25/25

Exercise, p.245, 3.32 &

p.248, 3.45

The greatest common divisor 

(GCD) of two integers is the

largest integer that evenly

divides each of the numbers.

Write a function gcd() that

returns the greatest commondivisor of two integers.