Function Advanced Features - Lecture Slides | ECE 206, Study notes of Electrical and Electronics Engineering

Material Type: Notes; Class: Electrical Engr Computations; Subject: Electrical And Computer Engr; University: University of Tennessee - Knoxville; Term: Fall 2007;

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-6vc-1
koofers-user-6vc-1 🇺🇸

5

(1)

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
ECE206 - Programming
Lecture 12 – Function (Advanced
Features)
11/13/07
2
What do you need to know
Fundamentals
Function declaration
Forward prototype
Forward definition
Call-by-value vs. call-
by-reference
Command-line
arguments
Storage class
Advanced topics
Function overloading
Inline functions
Functions with default
augments
* Function templates
* Recursive functions
* Function pointer
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Function Advanced Features - Lecture Slides | ECE 206 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

ECE206 - Programming

Lecture 12 – Function (Advanced

Features)

2

What do you need to know

† Fundamentals

„ Function declaration

„ Forward prototype

„ Forward definition

„ Call-by-value vs. call-

by-reference

„ Command-line

arguments

„ Storage class

† Advanced topics

„ Function overloading

„ Inline functions

„ Functions with default

augments

„ * Function templates

„ * Recursive functions

„ * Function pointer

3

C++ storage classes - revisit

† Each C++ variable has several attributes: „ name, type, size, value „ storage class, scope, linkage

† C++ has four storage classes „ auto : the default. Variables are automatically created and initialized when they are defined and are destroyed at the end of the block containing their definition. They are not visible outside that block. „ register : a type of auto variable. a suggestion to the compiler to use a CPU register for performance. „ static : a variable that is known only in the function that contains its definition but is never destroyed and retains its value between calls to that function. It exists from the time the program begins execution. „ extern : a static variable whose definition and placement is determined when all object and library modules are combined (linked) to form the executable code file. It can be visible outside the file where it is defined.

4

Example - register

#include

register int i, j; int starttime, endtime;

starttime = clock() for (i=0; i<LARGENUM; i++) ; endtime = clock();

cout << “the duration is” << endtime – starttime;

7

Another

example

// class definition class Complex { public: Complex(); Complex(float, float); ~Complex(); void setComplex(float, float); void printComplex(); float getReal() const; float getImag() const; void setReal(float); void setImag(float);

static int getCount(); private: float real; float imag;

static int count; };

int Complex::getCount() { return count; } // method implementation Complex::Complex() { real = 0; imag = 0; count++; } Complex::Complex(float a, float b) { real = a; imag = b; count++; } Complex::~Complex() { count--; }

8

Inline functions

† Short frequently used functions are good candidates for inline functions.

† An inline function’s body is inserted directly in the compiled code at the point where the function would normally be called.

† The advantage is efficiency.

† Like the register keyword, “inline” is only a suggestion to the compiler.

9

Example

// using inline function to calculate the volume of a cube

#include using namespace std;

inline float cube(float s) { return s * s * s; }

int main() { float side;

cin >> side; cout << “The volume of cube with side “ << side << “ is “ << cube(side);

return 0; }

10

Default arguments

† Default arguments have a defined value if they are not present in the calling parameter list.

† Default argument must be the rightmost parameters (so they can be omitted).

† Default arguments must be specified only once (first appearance of function name, usually in function prototype).

13

Example

† Consider 2 functions

located in the standard

library: abs (), fabs ().

† They are first defined by

the C language, and for

compatibility, are also

included in C++.

† So for similar tasks,

programmer has two

names to remember, not

just one.

† C++ can resolve this

problem by function

overloading

// overloading abs() #include using namespace std;

// abs() is overloaded in two ways int abs(int i); double abs(double d); int main() { cout << abs(-10) << “\n”; cout << abs(-11.0) << “\n”; return 0; }

int abs(int i) { if (i<0) return -i; else return i; } double abs(double d) { if (d<0.0) return -d; else return d; }

14

  • Function Templates

† Function templates allow the definition of

functions independent of the type of the arguments and return value.

† This implements a philosophy of “write-

once / use-anywhere” by allowing parameterized types.

† Templates form the basis for the C++

Standard Template Library (STL) of algorithms, which we will discuss later.

15

  • A Function Template Example

template T maximum ( T value1, T value2, T value3 ) { T max = value1; if ( value2 > max ) max = value2; if ( value3 > max ) max = value3; return max; } A list of formal type parameters, preceded by the “class” keyword, is specified between the “<…>”, and is used in place of types in the function definition.

int maximum ( int value1, int value2, int value3 ) { int max = value1;

if ( value2 > max ) max = value2;

if ( value3 > max ) max = value3;

return max; }

int main() { int a=1,b=3,c=3; int m;

m = maximu(a, b, c);

return 0; }

16

  • Recursion

† Recursive functions calls itself (directly or through another function).

† Any C++ function call call itself, but functions that use static and extern variables must do so carefully.

† Recursion can be used to solve problems by induction:

„ Do one step, and call itself to solve the

remaining problem.

„ Detect and perform the last step (base case).

19

Recursive process

Factorial(3)

3Factorial(2)*

2Factorial(1)*

1Factorial(0)*

2*

1*

3*

6

20

  • Pros and cons of using recursive functions

† If an algorithm can be expressed in a recursive form with a simple algorithm for each step, the recursive implementation can be very easy to verify.

† Recursion incurs overhead in creation and destruction of automatic variables and in function calls.

† Recursion may hide gross inefficiencies (e.g., the Fibonacci example); watch for explosive numbers of recursive calls!

21

  • Recursive Fibonacci calculation

long fib (long n) { if ( n == 0 || n == 1 ) { return n; //base case else if ( n > 0 ) { return fib(n-1) + fib(n-2); //recursive case } else return -1; //error case } Each call to fib (except fib(0) and fib(1)) generates 2 more calls to fib, which generates an exponential explosion in the total number of calls to fib as n increases.

Let O(fib,n) be the number of calls required to calculate fib(n) Then O(fib,n) = 1 + O(fib,n-1) + O(fib,n-2).