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

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

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

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

1.7 Recursion. Department of CSE

EE 368. Weeks 4 (Notes)

15 FUNCTIONS IN C 15.1 INTRODUCTION

NCS 301 DATA STRUCTURE USING C

Data Structures And Algorithms

Recursion. Chapter 5

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

recursive algorithms 1

Recursive Definitions

Functions. Chapter 5

Chapter 15: Recursion

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

Recursion vs Induction

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Fundamentals of Programming Session 12

Unit 7. Functions. Need of User Defined Functions

CSE 230 Intermediate Programming in C and C++ Functions

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

Copyright The McGraw-Hill Companies, Inc. Persion required for reproduction or display. What is Recursion?

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

C: How to Program. Week /Apr/16

Scope: Global and Local. Concept of Scope of Variable

Dept. of CSE, IIT KGP

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Chapter 17 Recursion

Recursion. Jordi Cortadella Department of Computer Science

Last week. Another example. More recursive examples. How about these functions? Recursive programs. CSC148 Intro. to Computer Science

C Functions. Object created and destroyed within its block auto: default for local variables

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

What is Recursion? Chapter 17 Recursion. Executing RunningSum. High-Level Example: Binary Search

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

Recursion vs Induction. CS3330: Algorithms The University of Iowa

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

Functions. Systems Programming Concepts

CS 310 Advanced Data Structures and Algorithms

COMP 250 Fall Recursive algorithms 1 Oct. 2, 2017

Recursion. Chapter 17 CMPE13. Cyrus Bazeghi

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

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.

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

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

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

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Functions and Recursion

Monday, March 6, We have seen how to translate void functions. What about functions that return a value such as

Chapter 17 Recursion

Reduction & Recursion Overview

Chapter 9. Variable Scope and Functions

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

UNIT 3 FUNCTIONS AND ARRAYS

Lecture 8 Recursion. The Mirrors

APCS-AB: Java. Recursion in Java December 12, week14 1

CSC 273 Data Structures

Chapter 6 Recursion. The Concept of Recursion

Chapter 4 Functions By C.K. Liang

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Gaddis:

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

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself?

Lecture 10: Recursion vs Iteration

An Elegant Weapon for a More Civilized Age

EK131 E5 Introduction to Engineering

Pace University. Fundamental Concepts of CS121 1

UNIT 5A Recursion: Basics. Recursion

EC 413 Computer Organization

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


Linear Data Structure

Chapter 5: Recursion. Objectives

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

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

Function Call Stack and Activation Records

FUNCTIONS OMPAL SINGH

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

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Object-Oriented Programming and Design D0010E. Interactive lecture 2. Overview of the lecture. Writing larger classes Documentation Recursion

CS 137 Part 9. Fibonacci, More on Tail Recursion, Map and Filter. November 22nd, 2017

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

CSC 1052 Algorithms & Data Structures II: Avoiding Recursion

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

CSE 214 Computer Science II Recursion

Scope, Functions, and Storage Management

Programming. Functions and File I/O

Recursion. February 02, 2018 Cinda Heeren / Geoffrey Tien 1

Recursion. Overview. Mathematical induction. Hello recursion. Recursion. Example applications. Goal: Compute factorial N! = 1 * 2 * 3...

LECTURE NOTES ON DATA STRUCTURES USING C

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? contains a reference to itself.

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Recursion Chapter 3.5

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

What is Recursion? ! Each problem is a smaller instance of itself. ! Implemented via functions. ! Very powerful solving technique.

Loops / Repetition Statements

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Recursion. Lecture 13. Recursion

Recursion. Comp Sci 1575 Data Structures. Introduction. Simple examples. The call stack. Types of recursion. Recursive programming

Transcription:

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

Recursion A process by which a function calls itself repeatedly. Either directly. X calls X. Or cyclically in a chain. X calls Y, and Y calls X. Used for repetitive computations in which each action is stated in terms of a previous result. fact(n) = n fact (n-1) 2

Contd. For a problem to be written in recursive form, two conditions are to be satisfied: It should be possible to express the problem in recursive form. The problem statement must include a stopping condition fact(n) = 1, if n = 0 = n fact(n-1), if n > 0 3

Recursive Functions : Purpose Recursive functions are used to reduce a complex problem to a simpler-to-solve problem. The simpler-to-solve problem is known as the base case Recursive calls stop when the base case is reached 4

Stopping the recursion A recursive function must always include a test to determine if another recursive call should be made, or if the recursion should stop with this call A different value is passed to the function each time it is called Eventually, the parameter reaches the value in the test, and the recursion stops 5

Examples: Factorial: fact(0) = 1 fact(n) = n * fact(n-1), if n > 0 GCD: gcd (m, m) = m gcd (m, n) = gcd (m%n, n), if m > n gcd (m, n) = gcd (n, n%m), if m < n Fibonacci series (1,1,2,3,5,8,13,21,.) fib (0) = 1 fib (1) = 1 fib (n) = fib (n-1) + fib (n-2), if n > 1 6

Example 1 :: Factorial int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); 7

Example :: Calculating fact(4) First, the function calls will be processed: fact(4) = 4 * fact(3) fact(3) = 3 * fact(2) fact(2) = 2 * fact(1) fact(1) = 1 * fact(0) The actual values return in the reverse order: fact(0) = 1 fact(1) = 1 * 1 = 1 fact(2) = 2 * 1 = 2 fact(3) = 3 * 2 = 6 fact(4) = 4 * 6 = 24

Example 1 :: Factorial Execution fact(4) 24 if (4 = = 1) return (1); else return (4 * fact(3)); 6 if (3 = = 1) return (1); else return (3 * fact(2)); 2 int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); if (2 = = 1) return (1); else return (2 * fact(1)); 1 if (1 = = 1) return (1); else return (1 * fact(0)); 9

Example 2 :: Fibonacci number Fibonacci number f(n) can be defined as: f(0) = 0 f(1) = 1 f(n) = f(n-1) + f(n-2), if n > 1 The successive Fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8, 13, 21,.. Function definition: int f (int n) { if (n < 2) return (n); else return (f(n-1) + f(n-2)); 10

Tracing Execution How many times is the function called when evaluating f(4)? f(3) f(4) f(2) f(2) f(1) f(1) f(0) Inefficiency: Same thing is computed several times. f(1) f(0) called 9 times 11

Performance Tip Avoid Fibonacci-style recursive programs which result in an exponential explosion of calls.

Notable Point Every recursive program can also be written without recursion Recursion is used for programming convenience, not for performance enhancement Sometimes, if the function being computed has a nice recurrence form, then a recursive code may be more readable 13

Example 3 :: Towers of Hanoi Problem 1 2 3 4 5 LEFT CENTER RIGHT 14

The problem statement: Initially all the disks are stacked on the LEFT pole. Required to transfer all the disks to the RIGHT pole. Only one disk can be moved at a time. A larger disk cannot be placed on a smaller disk. CENTER pole is used for temporary storage of disks. 15

Recursive statement of the general problem of n disks. Step 1: Move the top (n-1) disks from LEFT to CENTER. Step 2: Move the largest disk from LEFT to RIGHT. Step 3: Move the (n-1) disks from CENTER to RIGHT. 16

#include <stdio.h> void transfer (int n, char from, char to, char temp); int main() { int n; /* Number of disks */ scanf ( %d, &n); transfer (n, L, R, C ); void transfer (int n, char from, char to, char temp) { if (n > 0) { transfer (n-1, from, temp, to); printf ( Move disk %d from %c to %c \n, n, from, to); transfer (n-1, temp, to, from); return; 17

18

19

Recursion vs. Iteration Repetition Iteration: explicit loop Recursion: repeated function calls Termination Iteration: loop condition fails Recursion: base case recognized Both can have infinite loops Balance Choice between performance (iteration) and good software engineering (recursion). 20

How are function calls implemented? The following applies in general, with minor variations that are implementation dependent. The system maintains a stack in memory. Stack is a last-in first-out structure. Two operations on stack, push and pop. Whenever there is a function call, the activation record gets pushed into the stack. Activation record consists of (1) the return address in the calling program, (2) the return value from the function, and (3) the local variables inside the function. At the end of function call, the corresponding activation record gets popped out of the stack. 21

STACK int main() {.. x = gcd (a, b);.. int gcd (int x, int y) {.... return (result); Activation record Local Variables Return Value Return Addr Before call After call After return 22

int main() {.. x = ncr (a, b);.. int ncr (int n, int r) { return (fact(n)/ fact(r)/fact(n-r)); 3 times int fact (int n) { return (result); 3 times LV2, RV2, RA2 LV1, RV1, RA1 LV1, RV1, RA1 LV1, RV1, RA1 Before call Call ncr Call fact fact returns ncr returns 23

What happens for recursive calls? What we have seen. Activation record gets pushed into the stack when a function call is made. Activation record is popped off the stack when the function returns. In recursion, a function calls itself. Several function calls going on, with none of the function calls returning back. Activation records are pushed onto the stack continuously. Large stack space required. Activation records keep popping off, when the termination condition of recursion is reached. 24

We shall illustrate the process by an example of computing factorial. Activation record looks like: Local Variables Return Value Return Addr 25

Example:: main() calls fact(3) int main() { int n; n = 3; printf ( %d \n, fact(n) ); return 0; int fact (int n) { if (n = = 0) return (1); else return (n * fact(n-1)); 26

TRACE OF THE STACK DURING EXECUTION n = 0 1 RA.. fact main calls fact n = 2 n = 1 - RA.. fact n = 2 n = 1 - RA.. fact n = 2 n = 1 1*1 = 1 RA.. fact n = 2 n = 2 fact returns to main - - - - 2*1 = 2 RA.. fact RA.. fact RA.. fact RA.. fact RA.. fact n = 3 n = 3 n = 3 n = 3 n = 3 n = 3 n = 3 - - - - - - 3*2 = 6 RA.. main RA.. main RA.. main RA.. main RA.. main RA.. main RA.. main 27

Do Yourself Trace the activation records for the following version of Fibonacci sequence. X Y #include <stdio.h> int f (int n) { int a, b; if (n < 2) return (n); else { a = f(n-1); b = f(n-2); return (a+b); int main() { printf( Fib(4) is: %d \n, f(4)); Local Variables (n, a, b) Return Value Return Addr (either main, or X, or Y) main 28

Storage Class of Variables 29

What is Storage Class? It refers to the permanence of a variable, and its scope within a program. Four storage class specifications in C: Automatic: auto External : extern Static : static Register : register 30

Automatic Variables These are always declared within a function and are local to the function in which they are declared. Scope is confined to that function. This is the default storage class specification. All variables are considered as auto unless explicitly specified otherwise. The keyword auto is optional. An automatic variable does not retain its value once control is transferred out of its defining function. 31

#include <stdio.h> int factorial(int m) { auto int i; auto int temp=1; for (i=1; i<=m; i++) temp = temp * i; return (temp); int main() { auto int n; for (n=1; n<=10; n++) printf ( %d! = %d \n, n, factorial (n)); return 0; 32

Static Variables Static variables are defined within individual functions and have the same scope as automatic variables. Unlike automatic variables, static variables retain their values throughout the life of the program. If a function is exited and re-entered at a later time, the static variables defined within that function will retain their previous values. Initial values can be included in the static variable declaration. Will be initialized only once. An example of using static variable: Count number of times a function is called. 33

EXAMPLE 1 #include <stdio.h> int factorial (int n) { static int count=0; count++; printf ( n=%d, count=%d \n, n, count); if (n == 0) return 1; else return (n * factorial(n-1)); int main() { int i=6; printf ( Value is: %d \n, factorial(i)); 34

Program output: n=6, count=1 n=5, count=2 n=4, count=3 n=3, count=4 n=2, count=5 n=1, count=6 n=0, count=7 Value is: 720 35

EXAMPLE 2 #include <stdio.h> int fib (int n) { static int count=0; count++; printf ( n=%d, count=%d \n, n, count); if (n < 2) return n; else return (fib(n-1) + fib(n-2)); main() { int i=4; printf ( Value is: %d \n, fib(i)); 36

Program output: n=4, count=1 n=3, count=2 n=2, count=3 n=1, count=4 n=0, count=5 n=1, count=6 n=2, count=7 n=1, count=8 n=0, count=9 Value is: 3 f(4) f(3) f(2) f(2) f(1) f(1) f(1) f(0) [0,1,1,2,3,5,8,.] f(0) 37

Register Variables These variables are stored in high-speed registers within the CPU. Commonly used variables may be declared as register variables. Results in increase in execution speed. The allocation is done by the compiler. 38

External Variables They are not confined to single functions. Their scope extends from the point of definition through the remainder of the program. They may span more than one functions. Also called global variables. Alternate way of declaring global variables. Declare them outside the function, at the beginning. 39

#include <stdio.h> int count=0; /** GLOBAL VARIABLE **/ int factorial (int n) { count++; printf ( n=%d, count=%d \n, n, count); if (n == 0) return 1; else return (n * factorial(n-1)); int main() { int i=6; printf ( Value is: %d \n, factorial(i)); printf ( Count is: %d \n, count); 40

Program output: n=6, count=1 n=5, count=2 n=4, count=3 n=3, count=4 n=2, count=5 n=1, count=6 n=0, count=7 Value is: 720 Count is: 7 41