1 cs 162 introduction to computer science chapter 4 function calls herbert g. mayer, psu status...

27
1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

Upload: kelly-bryant

Post on 28-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

1

CS 162Introduction to Computer Science

Chapter 4Function Calls

Herbert G. Mayer, PSUStatus 11/9/2014

Page 2: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

2

Syllabus

C++ Functions Calls Varying Number of Actuals Nested Calls Recursion

Page 3: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

3

C++ Functions

Functions are prime building blocks for C++ programs that render them readable and maintainable; named logical modules

A function is a contained module, identified by its name

That name can be called, in which case the function executes, regardless of where in the program it appears textually

It has been designed to enclose some logically contained and coherent purpose. That purpose is fulfilled by the call

It is possible to re-use a function at a different place, call it from that different place

Page 4: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

4

C++ Functions

In the function declaration we refer to them as formal parameters

The call provides compatible actual parameters

Actual and formal must pairwise match in type and correspond by position: type compatibulity

Like in C, it is allowed to pass a smaller number of actual parameters than formally specified

A void function solely performs the action of the statements enclosed in the { and } pair

A true function, however, may return a value to its place of call; the type is specified in the definition

Page 5: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

5

Varying Number of Actuals

Define void function foo( int a, int b) with 2 formal parameters

If a is 0, a message stating so is printed, and parameter b is skipped, i.e. there will be no corresponding actual

But if a is greater than 0, the value of the second, b is printed

Note: Some compilers do not allow by default

Page 6: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

6

Implement Varying Number of Actuals

#include <iostream.h>void foo( int a, int b ){ // foo

if ( a ) {cout << “parameter b = “ << b << endl;

}else{cout << “no value for b is passed” <<

endl;} //end if

} //end foo

int main( void ){ // main

foo( 0 );foo( 1, 2014 );return 0;

} //end main

Page 7: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

7

Discuss Varying Number of Actuals

It would be an error, if a smaller than specified number of actual is passed, and yet such a formal parameter would be referenced that has not actually been provided

In such cases, random garbage on the run-time stack is mis-interpreted as formal ‘b’

Generally a serious error

Page 8: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

8

Function min( a, b )

Define int function min() that returns the smaller of 2 passed actual parameters

Though trivial, we extend this to allow the selection of the smallest of 3 or more candidate values

One way to achieve this is to nest some of the actual parameters via further function calls

Page 9: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

9

Implement min( a, b )

#include <iostream.h>int min( int a, int b ){ // min

return ( a < b ) : a ? b;} //end foo

int main( void ){ // main

cout << “smaller of -12, 12:” << min( -12, 12 ) << endl;

cout << “smallest of 88, -9, 100:” << min( min( 88, -9 ), 100 ) << endl;

cout << “smallest of 200, 300, 400:” << min( 200, min( 300, 400 ) ) << endl;

cout << “smallest of 10, -99, 100, -888:” << min( min( 10, -99), min( 10, -

888 ) ) << endl;return 0;

} //end main

Page 10: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

10

Discuss min( a, b ) => min( a, b, c, d )

The point here is to demonstrate nested function calls

Allows virtual extension of a function to a more complex one, without having to code it

See 2 samples of min( a, b, c ) with 3 candidates

And 1 sample of selecting the smallest of 4 candidates, and yet we have implemented just a simple min( a, b ) function

Page 11: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

11

Definition of Recursive AlgorithmAn algorithms is recursive, if it is partly defined by simpler versions of itself [1]

A recursive program is the implementation of a recursive algorithm What is the key problem for a programmer, using a language that is non-recursive (e.g.

standard Fortran) if the algorithm to be implemented is recursive? --See later!

What then are the other parts of a recursive algorithm? Correct recursive algorithm requires a starting point, formally known as “base case” Base case could be multiple steps

Recursive algorithm a() uses a base case as starting point for computation, plus the actual function body, including some recursive use of a()

Recursive body can be indirectly recursive through intermediate function a()-> b()-> a() – through intermediate function b()

Primitive examples are the factorial( n ) function; or Fibonacci( n ), for non-negative arguments n; Fibo( n ) shown here:

Base case 1: Fibo(0) = 0 Base case 2: Fibo(1) = 1 Recursive Definition: Fibo( n ) for n > 1 = Fibo( n-1 ) + Fibo( n-2 )

Page 12: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

12

Function Fibo( n )

Define int function Fibo() that returns the Fibonacci number of its passed, unsigned, integer argument n

Though trivial to code iteratively, we use recursion to compute Fibo( n )

We saw the definition for recursion earlier, so we know:

Check for a termination condition; that is the “partly defined” condition

Check that the recursive call proceeds with a simpler argument than the original; that is the “simpler version” of the recursion definition

Let us ignore that integers could be negative, and assume non-negative, original arguments to Fibo()

Page 13: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

13

Implement Fibo( n ). . .int Fibo( unsigned n ){ // Fibo

if ( 0 == n ) {return 0;

}else if ( 1 == n ) {return 1;

}else{return Fibo( n – 1 ) + Fibo( n – 2 );

} //end Fibo

int main( void ){ // main

cout << “Fibo( 8 ) = “ << Fibo( 8 ) << endl;return 0;

} //end main

Page 14: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

14

Discuss Fibo( n )

Demonstrate recursive function calls But the recursive function is defined in

simpler versions of itself, hence we cannot call Fibo( n ) any longer in the body

So: Fibo( n-1 ), and also Fibo( n-2 ) are valid possibilities

Also, the recursive function is partly defined via a call to itself; i.e. there are other parts

Those parts are the checks for termination early: is the argument already 0 or 1, if so, we know and return the result.

In all other cases: recurse!

Page 15: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

15

Q-Sequence, DefinitionQ-Sequence defined by Douglas Hofstadter in [1] as a function q( n ) for

positive integers n > 0

Base case n = 1: q(1) = 1Base case n = 2: q(2) = 1

Recursive definition of q(n), for positive n > 2

q( n ) = q( n – q( n - 1 ) ) + q( n – q( n - 2 ) )

Q-Sequence reminds us of Fibonacci( n ) function, but with surprising difference in the type of result:

By contract, the function results of fibonacci( n ) are monotonically increasing with increasing argument

Results of q( n ) are non-monotonic!

Note # of calls: calls(q( 40 )) = 1,137,454,741

Page 16: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

16

Q-Sequence, Coded in C#define MAX 100 // arbitrary limit; never reached!!!!int calls; // will be initialized each time

int q( int arg ){ // q

calls++; // track another callif ( arg <= 2 ) { return 1; // base case}else{ // now recurse! return q( arg - q( arg-1 ) ) + q( arg - q( arg-2 ) );} // end if

} // end q

// note: printf() allowed in C++void main(){ // main

for( int i = 1; i < MAX; i++ ) { calls = 0; // initially no calls yet printf( "Q(%2d) = %3d, #calls = %10d\n", i, q(i), calls );} // end for

} // end main

Page 17: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

17

Q-Sequence ResultsQ( 1) = 1, #calls = 1Q( 2) = 1, #calls = 1Q( 3) = 2, #calls = 5Q( 4) = 3, #calls = 13Q( 5) = 3, #calls = 25Q( 6) = 4, #calls = 49Q( 7) = 5, #calls = 93Q( 8) = 5, #calls = 161Q( 9) = 6, #calls = 281Q(10) = 6, #calls = 481Q(11) = 6, #calls = 813

. . .

Q(26) = 14, #calls = 1341433Q(27) = 16, #calls = 2174493Q(28) = 16, #calls = 3521137Q(29) = 16, #calls = 5700281Q(30) = 16, #calls = 9229053Q(31) = 20, #calls = 14941993Q(32) = 17, #calls = 24182797Q(33) = 17, #calls = 39137473Q(34) = 20, #calls = 63354153Q(35) = 21, #calls = 102525697Q(36) = 19, #calls = 165896537Q(37) = 20, #calls = 268460333Q(38) = 22, #calls = 434429737Q(39) = 21, #calls = 702952137Q(40) = 22, #calls = 1137454741

. . . Will never reach Q(100) in your life time

Page 18: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

18

Ackermann Definition

Ackermann a( m, n ) is defined as a function of two non-negative integers m and n

Base case 1: a( 0, n ) = n + 1Base case 2: a( m, 0 ) = a( m - 1, 1 )

Recursive definition of a( m, n ), m, n > 0a( m, n ) = a( m - 1, a( m, n - 1 ) )

Ackermann complexity grows awfully fast; e.g. a(4,2) is an integer number with 19,729 decimal digits; greater than the national US debt!

Page 19: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

19

Ackermann Definition

Students, code now in C++, and volunteers shows result on white-board:

Base case 1: a( 0, n ) = n + 1Base case 2: a( m, 0 ) = a( m - 1, 1 )

Recursive definition of a( m, n ), m, n > 0a( m, n ) = a( m - 1, a( m, n - 1 ) )

Page 20: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

20

Ackermann Coded in C

unsigned a( unsigned m, unsigned n ){ // a

calls++;// global unsigned

if ( 0 == m ) { // note operand order return n + 1; // first base case}else if ( 0 == n ) { // m > 0

return a( m - 1, 1 ); // other base case}else{

// m > 0, n > 0 return a( m-1, a( m, n-1 ) ); // recurse!

} // end if} // end q

void main(){ // main

for( int i = 0; i < MAX; i++ ) { printf( "\nFor m = %d\n", i ); for( int j = 0; j < MAX; j++ ) {

calls = 0; printf( "a(%1d,%1d) = %10u, calls =

%12u\n", i, j, a( i, j ), calls );

} // end for} // end for

} // end main

Page 21: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

21

Ackermann ResultsFor m = 0a(0,0) = 1, calls = 1. . .

For m = 1. . .a(1,7) = 9, calls = 16

For m = 2a(2,0) = 3, calls = 5a(2,1) = 5, calls = 14a(2,2) = 7, calls = 27a(2,3) = 9, calls = 44a(2,4) = 11, calls = 65a(2,5) = 13, calls = 90a(2,6) = 15, calls = 119a(2,7) = 17, calls = 152

For m = 3a(3,0) = 5, calls = 15a(3,1) = 13, calls = 106a(3,2) = 29, calls = 541a(3,3) = 61, calls = 2432a(3,4) = 125, calls = 10307a(3,5) = 253, calls = 42438a(3,6) = 509, calls = 172233a(3,7) = 1021, calls = 693964

For m = 4a(4,0) = 13, calls = 107

don’t even dream about computing a(4,2) or higher!

Page 22: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

22

Recursion vs. Iteration• Iteration is expressed in programming

languages by loops; e.g. for-, while-, do-, or repeat loops

• These are readable and efficient methods for expressing iteration, but are not strictly necessary

• Recursion can replace iteration; yet for some people this seems counter-intuitive

• Neophytes are sometimes unused to recursion; yet recursion can be as intuitive as simple iteration

Page 23: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

23

Replace Iteration via Recursion

• Using only functions, called recursively

• Plus arithmetic increment/decrement operators ++ -- and unary minus –

• And conventional relational operators > >= != etc.

• All other operators are dis-allowed in this experiment, i.e. no + - * / % ** etc.

Page 24: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

24

Recursion vs. Iteration: add()

// return a + b without + operation!int add( int a, int b ){ // addif ( 0 == b ) {

return a;}else if ( b < 0 ) {

return add( --a, ++b );}else{

return add( ++a, --b );} //end if

} //end add

Page 25: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

25

Recursion vs. Iteration: sub()

// return a – b; no dyadic – operationint sub( int a, int b ){ // subreturn add( a, -b );

} //end sub

Page 26: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

26

Recursion vs. Iteration: mult()// return a * b, no * but add()int mult( int a, int b ){ // multif ( 0 == b ) {

return 0;}else if ( 1 == b ) {

return a;}else if ( b < 0 ) {

return -mult( a, -b );}else{

// b > 0return add( a, mult( a, --b

) );} //end if

} //end mult

Page 27: 1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014

27

Recursion vs. Iteration: expo()// return a ** b, no ** op in C++; requires mult( int, int )int expo( int a, int b ){ // expo if ( 0 == a ) { if ( 0 == b ) { cout << ”undefined value0^0” << endl; }else if ( b < 0 ) { cout << “0 to <0 power undefined” << endl;

} //end if return 0;

}else if ( 0 == b ) { return 1; }else if ( 1 == a ) { return 1; }else if ( -1 == a ) { return b % 2 ? -1 : 1; }else if ( b < 0 ) { return 0; }else{ return mult( expo( a, --b ), a ); } //end if} //end expo