BBM 101 Introduc/on to Programming I Fall 2013, Lecture 6-7

Similar documents
BBM 101 Introduc/on to Programming I Fall 2014, Lecture 7. Aykut Erdem, Erkut Erdem, Fuat Akal

BBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$617$

BBM#101# #Introduc/on#to# Programming#I# Fall$2014,$Lecture$7$

Detail from C Propaganda Poster // Reddit user TheBall BBM 101. Introduction to Programming I. Lecture #11 C Iterations, Functions, Multi-D Arrays

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 5. Aykut Erdem, Erkut Erdem, Fuat Akal

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4

CS101 Introduction to computing Function, Scope Rules and Storage class

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

Lecture 04 FUNCTIONS AND ARRAYS

Outline. Pointers arithme.c and others Func.ons & pointers

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

Preview from Notesale.co.uk Page 2 of 79

NAMES, SCOPES AND BINDING A REVIEW OF THE CONCEPTS

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

CSE123. Program Design and Modular Programming Functions 1-1

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 4 Functions By C.K. Liang

Chapter 5 C Functions

Introduction to Programming

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Lecture 10: Recursive Functions. Computer System and programming in C 1

Lecture 04 FUNCTIONS AND ARRAYS

C Programs: Simple Statements and Expressions

Chapter 3 - Functions

Functions. (transfer of parameters, returned values, recursion, function pointers).

Functions. Systems Programming Concepts

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

C Functions. 5.2 Program Modules in C

UNIT III (PART-II) & UNIT IV(PART-I)

Chapter 5 Methods / Functions

Proofs about Programs

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014

Methods. Methods. Mysteries Revealed

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

Programming in C Quick Start! Biostatistics 615 Lecture 4

Benefits of Methods. Chapter 5 Methods

CSE 230 Intermediate Programming in C and C++ Functions

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Dynamic memory allocation

Lecture 9 - C Functions

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Functions. Chapter 5

15 FUNCTIONS IN C 15.1 INTRODUCTION

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Programming & Data Structure Laboratory. Day 2, July 24, 2014

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Lecture 2:- Functions. Introduction

Func%ons. CS449 Fall 2017

COMP s1 Lecture 1

A PROBLEM can be solved easily if it is decomposed into parts. Similarly a C program decomposes a program into its component functions.

What Is a Function? Illustration of Program Flow

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

CS113: Lecture 4. Topics: Functions. Function Activation Records

COP3502 Programming Fundamentals for CIS Majors 1. Instructor: Parisa Rashidi

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

Functions. Arash Rafiey. September 26, 2017

C++ Programming Lecture 11 Functions Part I

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Computer Science & Engineering 150A Problem Solving Using Computers

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Structured Programming. Functions and Structured Programming. Functions. Variables

C Programming for Engineers Functions

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Chapter 8: Function. In this chapter, you will learn about

CS313D: ADVANCED PROGRAMMING LANGUAGE

Fundamentals of Programming & Procedural Programming

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Unit 7. Functions. Need of User Defined Functions

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

UNIVERSITY OF WINDSOR Fall 2006 QUIZ # 1. Examiner:Ritu Chaturvedi Dated : Oct 3rd, Student Name: Student Number:

Chapter 7 Functions. Now consider a more advanced example:

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

CSCI 2132 Software Development. Lecture 18: Functions

EK131 E5 Introduction to Engineering

Pointers and scanf() Steven R. Bagley

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Abstract Data Types

JTSK Programming in C II C-Lab II. Lecture 3 & 4

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

CS115 Principles of Computer Science

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

(2-2) Functions I H&K Chapter 3. Instructor - Andrew S. O Fallon CptS 121 (January 18, 2019) Washington State University

User Defined Functions

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Transcription:

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 6-7 Instructors: Aykut Erdem, Erkut Erdem, Fuat Akal TAs: Yasin Sahin, Ahmet Selman Bozkir, Gultekin Isik, Oguzhan Guclu 1

Today Func/ons Defini@ons Invoca@on Parameter Lists Return Values Prototypes Variable Scopes Block Structure Global and Local Variables Sta@c Variables Recursion 2

Today Func/ons Defini@ons Invoca@on Parameter Lists Return Values Prototypes Variable Scopes Block Structure Global and Local Variables Sta@c Variables Recursion 3

Introduc/on Structured Programming is a problem- solving strategy and a programming methodology that includes the following two guidelines: The flow of control in a program should be as simple as possible. The construc@on of a program should embody top- down design. 4

Top- down Design Top- down design (stepwise refinement, or divide and conquer) consists of repeatedly decomposing a problem into smaller problems. A program is constructed from smaller pieces (components, modules) Each piece is more manageable than the original program 5

Func/ons Programs combine user- defined func@ons with library func@ons C standard library has a wide variety of func@ons, e.g. several math func@ons are included in math.h Invoking func@ons Provide func@on name and arguments Func@on performs opera@ons or manipula@ons Func@on returns results Func@on call analogy Boss asks worker to complete task Worker gets informa@on, does task, returns result Informa@on hiding: boss does not know details 6

Math Library Func/ons Math library func@ons perform common mathema@cal calcula@ons #include <math.h> Format for calling func@ons Func/onName( argument ); If mul@ple arguments, use comma- separated list y = sqrt( 900.0 ); Calls func@on sqrt, which returns the square root of its argument Arguments may be any r- value (constants, variables, or expressions) 7

Math Library Func/ons Func/on Header int abs(int num) double fabs(double num) Descrip/on Returns the absolute value of an integer element Returns the absolute value of a double precision element. double pow(double x,double y) Returns x raised to the power of y. int rand(void) double sin(double angle) double cos(double angle) double sqrt(double num) Returns a random number Returns the sine of an angle; the angle should be in Radius. Returns the cosine of an angle; the angle should be in Radius. Returns the the square root of a double 8

Math Library Func/ons (Example) How to code square root of (x1 - x2) 2 + (y1 - y2) 2 by using math.h library func@ons? a = x1 x2; b = y1 y2; c = pow(a, 2) + pow(b, 2); d = sqrt(c); 9

Func/ons We have already wriyen/called some func@ons before. /* Welcome to BBM 101 */ #include <stdio.h> int main(void) { printf( Hello world!\n ); return 0; main is a func@on that must exist in every C program. prinu is a library func@on which we have already used in our program. Let s create and call our own functions now! 10

Func/on Defini/on Syntax type name (parameters){ variables; statements; name is the name of the func@on type is the type of the returned value by the func@on void means the func@on returns nothing Func@ons return int value if nothing is specified parameters specify the types and names of the parameters separated by comma 11

Func/on Returning a Value (Example) Let s define a func@on to compute the cube of a number: int cube ( int num ) { int result; result = num * num * num; return result; This func@on can be called as: n = cube(5); 12

void Func/on (Example) /* function definition */ void print_message(void){ printf( A message for you: ); printf( Have a nice day!\n ); int main(void){ /* function invocation */ print_message(); return 0; 13

Func/on Invoca/on A program is made up of one or more func@ons, one of them being main( ). When a program encounters a func@on, the func@on is called or invoked. A\er the func@on does its work, program control is passed back to the calling environment, where program execu@on con@nues. Function1(){ return; Function2(){ return; int main(){ Function1(); Function2(); return; 14

The return Statement When a return statement is executed, the execu@on of the func@on is terminated and the program control is immediately passed back to the calling environment. If an expression follows the keyword return, the value of the expression is returned to the calling environment as well. A return statement can be one of the following two forms: return; return expression; Examples: return; return 1.5; return result; return a+b*c; return x < y? x : y; 15

The return Statement (Example) Define a func@on to check if asked year is a leap year int IsLeapYear(int year){ return ( ((year % 4 == 0) && (year % 100!= 0)) (year % 400 == 0) ); This func@on may be called as: if (IsLeapYear(2005)) printf( 29 days in February.\n ); else printf( 28 days in February.\n ); 16

Example: Find minimum of two integers #include <stdio.h> int min(int a, int b){ if (a < b) return a; else return b; int main (void){ int j, k, m; Input two integers: 11 3 The mininum is 3. printf( Input two integers: ); scanf( %d %d, &j, &k); m = min(j, k); printf( \nthe minimum is %d.\n, m); return 0; 17

Func/on Parameters A func@on can have zero or more parameters. The formal parameter list in declara@on header int f (int x, double y, char c); Parameter variables and their types are declared here. The actual parameter list in func@on calling: value = f(age, 100*score, initial); Cannot be told what their types are from here. 18

Rules for Parameter Lists The number of parameters in the actual and formal parameter lists must be consistent. Parameter associa@on is posigonal: the first actual parameter matches the first formal parameter, the second matches the second, and so on. Actual parameters and formal parameters must be of compa@ble data types. Actual parameters may be a variable, constant, any expression matching the type of the corresponding formal parameter. 19

Call- by- Value Invoca/on Each argument is evaluated, and its value is used locally in place of the corresponding formal parameter. If a variable is passed to a func@on, the stored value of that variable in the calling environment will not be changed. In C, all calls are call- by- value. 20

Func/on Call The type of a func@on- call expression is the same as the type func@on being called, and its value is the value returned by the func@on. Func@on calls can be embedded in other func@on calls. t = cubesum(i); j = cubesum(t); is equivalent to j = cubesum(cubesum(i)); 21

Func/on Call (Example) #include <stdio.h> int compute_sum (int n){ int sum = 0; for ( ; n > 0; --n) sum += n; int main (void){ int n = 3, sum; printf( %d, n); sum = compute_sum(n); printf( %d, n); return sum; printf( %d, n); printf( %d, sum); return 0; 3 0 3 6 22

Example: Find maximum of three integers #include <stdio.h> int maximum(int a, int b, int c){ int max = a; if (b > max) max = b; if (c > max) max = c; return max; Input three integers: 11 3 7 The maxinum is 11. int main (void){ int j, k, l, m; printf( Input three integers: ); scanf( %d %d %d, &j, &k, &l); printf( \nthe maximum is %d.\n, maximum(j, k, l)); return 0; 23

Func/on Prototypes General form for a func@on prototype declara@on: return_type function_name (parameter-type-list) Used to validate func@ons Prototype only needed if func@on defini@on comes a\er use in program The func@on with the prototype int maximum( int, int, int ); (Takes in 3 ints, returns an int) 24

Using Func/on Prototypes #include <stdio.h> int max (int a, int b){... int min (int a, int b){... int main(void){... min(x,y); max(u,v);... = #include <stdio.h> int max(int,int); int min(int,int); int main(void){ min(x,y); max(u,v);... int max (int a, int b){... int min (int a, int b){... 25

Today Func/ons Defini@ons Invoca@on Parameter Lists Return Values Prototypes Variable Scopes Block Structure Global and Local Variables Sta@c Variables Recursion 26

Block Structure and Variable Scope #include <stdio.h> int total, count; int main(int argc, const char * argv[]){ total = count = 0; { int count = 0; while (1) { if (count > 10) break; total += count; count++; printf("%d\n", count); Global count variable is valid in the whole program. Local count variable is only valid in the red block here. 11 1 count++; printf("%d\n", count); return 0; 27

External Variables Local variables can only be accessed in the func@on in which they are defined. If a variable is defined outside any func@on at the same level as func@on defini@ons, it is available to all the func@ons defined below in the same source file à external variable Global variables are external variables defined before any func@on defini@on Their scope will be the whole program 28

Local Variables #include <stdio.h> void func1 (void){ int i = 5; printf( %d\n, i); i++; printf( %d\n, i); 5 5 6 5 int main (void){ int i = 5; printf( %d \n, i); func1(); printf( %d \n,i); return 0; 29

Sta/c Variables A variable is said to be sta/c if it is allocated storage at the beginning of the program execu@on and the storage remains allocated un@l the program execu@on terminates. External variables are always sta@c. Within a block, a variable can be specified to be sta@c by using the keyword sta@c before its type declara@on: static type variable-name; Variable declared sta@c can be ini@alized only with constant expressions (if not, its default value is zero). 30

Sta/c Variables (Example) #include <stdio.h> void incr(void); int main(void){ int i; void incr(void); A sta/c variable inside a func/on keeps its value between invoca/ons. for (i=0; i<3; i++) incr(); return 0; Output: static_i = 0 static_i = 1 static_i = 2 void incr(void){ static int static_i = 0; printf( static_i = %d\n, static_i++); 31

Sta/c Variables (Example- Ini/al Value) #include <stdio.h> void put_stars(int n){ static int static_n; int i; for (i=0; i<static_n; i++) printf( ); for (i=0; i<n; i++) printf( * ); Output: *** ** *** printf( \n ); static_n+= n; int main(void){ put_stars(3); put_stars(2); put_stars(3); return 0; 32

Today Func/ons Defini@ons Invoca@on Parameter Lists Return Values Prototypes Variable Scopes Block Structure Global and Local Variables Sta@c Variables Recursion 33

Recursion Recursion is the process whereby a construct operates on itself. In C, a func@on may directly or indirectly call itself in the course of execu@on. direct: The call to a func@on occurs inside the func@on itself indirect: A func@on calls another func@on, which in turn makes a call to the first one Recursion is a programming technique that naturally implements the divide- and- conquer problem solving methodology. 34

Recursive Func/on How does it Look like? Computes the factorial of a nonnega/ve integer. int fact(int n) { if (n == 0) return (1); else return (n * fact(n-1)); The func/on fact calls itself. We will see how it really works soon! 35

The Nature of (direct) Recursion One or more simple cases of the problem (called the stopping cases or base case) have a simple non- recursive solu@on. The other cases of the problem can be reduced (using recursion) to problems that are closer to stopping cases. Eventually the problem can be reduced to stopping cases only, which are rela@vely easy to solve. In general: if (stopping case) solve it else reduce the problem using recursion 36

Recursive Example - Palindrome A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direc@on. e.g., radar, level, rotator, Step on no pets, Ey Edip Adanada pide ye, 1234321, etc. Recursive Defini/on: If the string has no elements, then it's a palindrome. If the string has only one element, then it's a palindrome. If the elements in the endpoints (the first and last elements) are the same, and the internal leyers are a palindrome, then it's a palindrome. 37

Four Criteria of a Recursive Solu/on A recursive func@on calls itself. This ac@on is what makes the solu@on recursive. Each recursive call solves an iden@cal, but a smaller problem. A recursive func@on solves a problem by solving another problem that is iden@cal in nature but smaller in size. A test for the base case enables the recursive calls to stop. There must be a case of the problem (known as base case or stopping case) that is handled differently from the other cases (without recursively calling itself.) In the base case, the recursive calls stop and the problem is solved directly. Eventually, one of the smaller problems must be the base case. The manner in which the size of the problem diminishes ensures that the base case is eventually is reached. 38

Factorial Func/on Itera:ve Defini:on n! = n * (n- 1) * (n- 2) * * 2 * 1 for any integer n > 0 0! = 1 Itera/ve Defini/on in C: fval = 1; for (i = n; i >= 1; i--) fval = fval * i; 39

Factorial Func/on Recursive Defini:on To define n! recursively, n! must be defined in terms of the factorial of a smaller number. Observa@on (problem size is reduced): n! = n * (n- 1)! Base case à 0! = 1 We can reach the base case, by subtrac@ng 1 from n if n is a posi@ve integer. Recursive Defini:on: n! = 1 if n = 0 n! = n*(n- 1)! if n > 0 40

Revisi/ng the Recursive Factorial Func/on Defini/on in C /* Computes the factorial of a nonnegative integer. Precondition: n must be greater than or equal to 0. Postcondition: Returns the factorial of n; n is unchanged.*/ int fact(int n) { if (n == 0) return (1); else return (n * fact(n-1)); This fact func@on sa@sfies the four criteria of a recursive solu@on. 41

How does it Compute? printf( %d, fact (3)); 6 return 3 * fact(2) 3 * 2 return 2 * fact(1) 2 * 1 return 1 * fact(0) 1 * 1 return 1 42

Tracing a Recursive Func/on A stack is used to keep track of func@on calls. Whenever a new func@on is called For each func@on call, an ac:va:on record (AR) is created on the stack. AR consists of the func@on s parameters and local variables are pushed onto the stack along with the memory address of the calling statement (return point). To trace a recursive func@on, the box method can be used. The box method is a systema@c way to trace the ac@ons of a recursive func@on. The box method illustrates how compilers implement recursion. Each box in the box method roughly corresponds to an ac@va@on record. 43

The Box Method Label each recursive call in the body of the recursive func@on. These labels help us to keep track of the correct place to which we must return a\er a func@on call completes. A\er each recursive call, we return to the labeled loca@on, and subs@tute that recursive call with returned value. if (n ==0) else return (1); return (n * fact(n-1) ) A 44

The Box Method (cont d.) Every @me a func@on is called, a new box is created to represent its local environment. Each box contains: The values of the arguments The func@on s local variables A placeholder for the value returned from each recursive call from the current box (label in the previous step). The value of the func@on itself. 45

The Box Method (cont d.) Draw an arrow from the statement that ini@ates the recursive process to the first box. Then draw an arrow to a new box created a\er a recursive call, put a label on that arrow. printf( %d, fact (3)); A 46

The Box Method (cont d.) A\er a new box is created, we start to execute the body of the func@on. On exi@ng a func@on, cross off the current box an follow its arrow back to the box that called the func@on. This box becomes the current box. Subs@tute the value returned by the just- terminated func@on call into the appropriate item in the current box. Con@nue the execu@on from the returned point. 47

Box Trace of fact(3) The ini@al call is made, and the func@on fact begins execu@on. At point A, a recursive call is made, and the new invoca@on of fact begins execu@on. A At point A, a recursive call is made, and the new invoca@on of fact begins execu@on. A A 48

Box Trace of fact(3) (cont d.) At point A, a recursive call is made, and the new invoca@on of fact begins execu@on. A A A This is the base case, so this invoca@on of fact completes. A A A The func@on value is returned to the calling box, which con@nues the execu@on. A A A The current invoca@on of fact completes. A A A 49

Box Trace of fact(3) (cont d.) The func@on value is returned to the calling box, which con@nues the execu@on. A The current invoca@on of fact completes. A The func@on value is returned to the calling box, which con@nues the execu@on. The current invoca@on of fact completes. The value 6 is returned to the ini@al call. 50

Example: Find the reverse of an input string /* reads n characters and prints them in reverse order. */ void reverse(int n){ char next; if (n == 1) { /* stopping case */ scanf("%c", &next); printf("%c", next); else { scanf("%c", &next); reverse(n-1); printf("%c", next); return; int main(){ printf("enter a string: "); reverse(3); printf("\n"); 51

Trace of reverse(3) reverse(3); /* Assume input is abc */ n=3 3 <= 1? false read next : a reverse(2) write a return n=2 2 <= 1? false read next : b reverse(1) write b return n=1 1 <= 1? true read next : c write c return 52

Example: Fibonacci Sequence It is the sequence of integers: t 0 t 1 t 2 t 3 t 4 t 5 t 6 0 1 1 2 3 5 8 Each element in this sequence is the sum of the two preceding elements. The specifica@on of the terms in the Fibonacci sequence: t n = n if n is 0 or 1 t n- 1 + t n- 2 otherwise 53

Example: Fibonacci Sequence int fib(int n){ if (n < 2) return n; else return(fib(n-2) + fib(n-1)); This is an example of non- linear recursion. Because total number of recursive calls grows exponen@ally. fib(n- 1) expression must be evaluated completely before its value can be added to the expression fib(n- 2) which must also be evaluated completely Recursion tree is useful in tracing the values of variables during non- linear recursion. 54

Recursion Tree for Fibonacci Sequence fib(6) => 7 th Fibonacci number fib(5) + fib (4) fib(4) + fib (3) fib(3) + fib (2) fib(3) + fib (2) fib(2) + fib (1) fib(2) + fib (1) fib(2) + fib (1) fib(1) + fib (0) 55

Example: Fibonacci Sequence (Itera/ve Version) int Fib(int n) { int Prev1, Prev2, Temp, j; if (n==0 n== 1) return n; else { Prev1=0; Prev2 = 1; for (j=1; j <= n; j++){ Temp = Prev1 + Prev2; Prev2 = Prev1; Prev1 = Temp; return Prev1; 56

Recursion vs. Itera/on In general, an itera@ve version of a program will execute more efficiently in terms of @me and space than a recursive version. This is because the overhead involved in entering and exi@ng a func@on is avoided in itera@ve version. However, a recursive solu@on can be some@mes the most natural and logical way of solving a problem. Conflict: machine efficiency versus programmer efficiency. It is always true that recursion can be replaced with itera@on and a stack (and vice versa). 57