C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype,...

25
C++ Lecture 2 Friday 11 July 2003

Transcript of C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype,...

Page 1: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

C++

Lecture 2Friday 11 July 2003

Page 2: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

Chapter 3, Functions

built-in functions function prototype, function

definition and use storage class and scope recursion inline function default argument, function

overloading, template

Page 3: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

Math Library Functions

#include <cmath> // to use math // library

On Unix, special compiler flag is needed

gxx file.cpp -lm most math functions take double

as argument and return double as value

C.f. math.h

Page 4: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

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: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

Functions

Organization of Large Program• Separate tasks into functions• Group related functions in separate

files. Typical Program Prototypes;

main() { … ;}

func1() { …; }

func2(int i, …) { … }

Page 6: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

Functions

Function prototype function definition use of function argument passing in C++

C.f. Fig.3.3

Page 7: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

Function Prototypes

Formatvoid maximum(int, int, int);

Location of function prototype in file

When can we omit function prototype?

Advantage of prototype

Page 8: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

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: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

Auto Storage Class

Local variables in functions or blocks. Auto storage class variables are created only when the block is active, and disappear when the block or function exits.

Page 10: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

Register Storage Class

Variable existence like auto. The register variable is a suggestion to the compiler to put the variable in a CPU register.

Page 11: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

Static Storage Class

Local static variable exists during the whole program executing, 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: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

Extern Storage Class

Global variables and function names have the storage class extern. Extern storage class variable exists during the whole program execution.

Page 13: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

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: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

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: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

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: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

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. iterationC.f. Fig.3.14

Page 17: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

Inline Functions

Advantage: function call overhead is eliminated, thus faster and less memory consuming

Disadvantage: the code is expanded during compilation so that executable file is large

C.f. Fig. 3.19

Page 18: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

Call by Value v.s. Call by Reference

Call-by-value: the function takes/works on a copy of the original variable

Call-by-reference: the function works on the original variable passed by caller.

C.f. Fig. 3.20

Page 19: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

Call by Reference

int func(int &); // prototypeint main(){ func(x); // call as usual}int func(int &x) // x is ref to int{ x = ..; // use x as usual}

Page 20: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

Call by Reference Using Pointer

int func(int *); // prototypeint main(){ func(&x); // call as usual}int func(int *x) // x is ref to int{ *x = ..; // use x as usual}

Page 21: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

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 function

C.f. Fig.3.23

Page 22: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

Function Overloading

Several functions of different argument types can use the same 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: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

Function Template

A function definition with unspecified data type.

The type is determined according to its use at compile time.

Page 24: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

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: C++ Lecture 2 Friday 11 July 2003. Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.

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 common divisor of two integers.