Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 C++ Lecture 2 Friday 11 July 2003

2 Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope l recursion l inline function l default argument, function overloading, template

3 Math Library Functions l #include // to use math // library l On Unix, special compiler flag is needed gxx file.cpp -lm l most math functions take double as argument and return double as value C.f. math.h

4 Math Functions l 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) l 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

5 Functions l Organization of Large Program Separate tasks into functions Group related functions in separate files. l Typical Program Prototypes; main() { … ;} func1() { …; } func2(int i, …) { … }

6 Functions l Function prototype l function definition l use of function l argument passing in C++ C.f. Fig.3.3

7 Function Prototypes l Format void maximum(int, int, int); l Location of function prototype in file l When can we omit function prototype? l Advantage of prototype

8 Storage Classes l Storage class determines the period during which an identifier exists in memory l Four storage classes auto, register, extern, static C.f. Fig. 3.12

9 Auto Storage Class l 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.

10 Register Storage Class l Variable existence like auto. The register variable is a suggestion to the compiler to put the variable in a CPU register.

11 Static Storage Class l Local static variable exists during the whole program executing, i.e., the variable retains its value between function calls. l However, the reference to the variable is local in the function or block.

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

13 Scope Rules l The places in code segment that an identifier is visible: l function scope l file scope l block scope l function-prototype scope l class scope

14 Storage Class and Scope l Storage class says when a variable exists l scope says where in program segment the variable is valid for reference

15 Unary Scope Resolution Operator :: l Using ::, one can access an global variable even if it is over- shadowed by a local variable of the same name.

16 Recursion l A function can also call itself, this is known as recursion l To avoid infinite recursion, one must have a terminating condition in the function l recursion v.s. iteration C.f. Fig.3.14

17 Inline Functions l Advantage: function call overhead is eliminated, thus faster and less memory consuming l Disadvantage: the code is expanded during compilation so that executable file is large C.f. Fig. 3.19

18 Call by Value v.s. Call by Reference l Call-by-value: the function takes/works on a copy of the original variable l Call-by-reference: the function works on the original variable passed by caller. C.f. Fig. 3.20

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

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

21 Default Arguments l The right-most arguments, if omitted, take the default value l Default values are specified at the first occurrence (prototype or function definition) of the function C.f. Fig.3.23

22 Function Overloading l 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. l In fact, we need to write two functions to handle two cases. C.f. 3.25

23 Function Template l A function definition with unspecified data type. l The type is determined according to its use at compile time.

24 Exercise, p.243, 3.22 l 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 ****

25 Exercise, p.245, 3.32 & p.248, 3.45 l 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.


Download ppt "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."

Similar presentations


Ads by Google