240-222 CPT: Functions/31 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to describe C functions –illustrate recursion.

Slides:



Advertisements
Similar presentations
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Advertisements

 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
ICS103 Programming in C Lecture 11: Recursive Functions
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 CSE1301 Computer Programming Lecture 27 Recursion (Part 1)
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Functions Gabriel Hugh Elkaim Spring 2012.
Computer Science 210 Computer Organization Introduction to C.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
1 Lecture04: Basic Types & Function Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
Senem Kumova Metin // CS115 // FUNCTIONS continues CHAPTER 5.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
ASET RECURSION. ASET RECURSIVE FUNCTIONS A recursive function is a function that calls itself to solve a smaller version of its task until a final call.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Functions-Recall 1. 2 Parameter passing & return void main() { int x=10, y=5; printf (“M1: x = %d, y = %d\n”, x, y); interchange (x, y); printf (“M2:
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Function. Outline Intro. Functions Examples of Functions Prototypes of a Functions Local and Global Variables.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
Administrative things
Chapter 5 Modular Design and Function C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Programming Languages -1 (Introduction to C) functions Instructor: M.Fatih AMASYALI
Senem Kumova Metin // CS115 // FUNCTIONS CHAPTER 5.
1 Recursion. 2 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.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Recursion Function calling itself
Recursion.
Functions Course conducted by: Md.Raihan ul Masood
Computer Science 210 Computer Organization
Computer Programming Techniques Semester 1, 1998
C Functions -Continue…-.
Computer Programming Techniques Semester 1, 1998
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Formatted and Unformatted Input/Output Functions
Computer Science 210 Computer Organization
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6 - Functions Outline 5.1 Introduction
Programming application CC213
Recursion Recursion is a math and programming tool
Functions Recursion CSCI 230
Recursion.
שיעור רביעי: פונקציות, מבוא לרקורסיה
Unit 3 Test: Friday.
Module 1-10: Recursion.
Computer Programming Techniques Semester 1, 1998
Java Programming Review 1
Introduction to Problem Solving and Programming
Main() { int fact; fact = Factorial(4); } main fact.
ICS103 Programming in C Lecture 11: Recursive Functions
Presentation transcript:

CPT: Functions/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to describe C functions –illustrate recursion 3. Functions

CPT: Functions/32 Overview 1.Examples 2.Type Coercion 3.Call by Value 4.Scope Rules 5.Recursion 6.A Skeleton with Functions

CPT: Functions/33 1. Examples /* Using a square function (Fig. 5.3) */ #include int square(int y); int main() { int x; for (x = 1; x <= 10; x++) printf("%d ", square(x)); printf("\n"); return 0; } continued function prototype

CPT: Functions/34 int square(int y) /* returns the square of y */ { return y * y; } function definition

CPT: Functions/35 /* Finding the maximum of three integers (Fig. 5.4) */ #include int maximum(int x, int y, int z); int main() { int a, b, c; printf("Enter three integers: "); scanf("%d%d%d", &a, &b, &c); printf("Maximum is: %d\n", maximum(a, b, c)); return 0; } continued

CPT: Functions/36 int maximum(int x, int y, int z) /* returns the biggest of x, y and z */ { if (x > y && x > z) return x; else if (y > z) return y; else return z; }

CPT: Functions/37 Execution Enter three integers: Maximum is 85

CPT: Functions/38 2. Type Coercion #include double sqroot(double n); int square(int y); int main() { printf("%f %f\n", sqroot(4.0), sqroot(4)); printf("%d %d\n", square(4), square(4.5)); return 0; } continued

CPT: Functions/39 double sqroot(double n) /* Use Newton Raphson to return the square root of n */ { float x0, eps = ; x0 = n; while ( abs(x0*x0 - n)/n > eps) x0 = (x0 + n/x0)/2; return x0; } int square(int y) { return y*y; }

CPT: Functions/310 Type Ordering (simplified) long double double float int char can be coerced to

CPT: Functions/311 Why Simplfied? Simplified in the sense that int can also be prefixed with the keywords: –short –long –unsigned l These refer to the size and range of the integer.

CPT: Functions/ Call By Value #include int compute_sum(int x); int main() { int n = 3, sum; printf("%d\n",n); sum = compute_sum(n); printf("%d\n",n); printf("%d\n",sum); return 0; } continued

CPT: Functions/313 int compute_sum(int x) /* sum the integers from 1 to x */ { int tot = 0; for ( ; x > 0 ; --x) tot += x; printf("%d\n", x); return tot; }

CPT: Functions/ Scope Rules l A block is a compound statement with declarations: { int x; s1; s2; }

CPT: Functions/315 l An identifier is accessible only within the block where it is declared. l The scope of an identifier is that part of the program where the identifier is accessible.

CPT: Functions/316 Examples int main() { int a = 2; printf("%d\n", a); { int a = 7; printf("%d\n", a); } printf("%d\n", ++a); return 0; }

CPT: Functions/317 A similar piece of code: int main() { int a_outer = 2; printf("%d\n", a_outer); { int a_inner = 7; printf("%d\n", a_inner); } printf("%d\n", ++a_outer); return 0; }

CPT: Functions/318 #include int compute_sum(int n); int main() { int n = 3, sum; printf("%d\n",n); sum = compute_sum(n); printf("%d\n",n); printf("%d\n",sum); return 0; } continued

CPT: Functions/319 int compute_sum(int n) /* sum the integers from 1 to n */ { int sum = 0; for ( ; n > 0 ; --n) sum += n; printf("%d\n", n); return sum; }

CPT: Functions/ Recursion Sec Factorial 5.2.The Fibonacci Series 5.3.Depth Tester

CPT: Functions/ Factorial l Mathematical Definition: fac n =1,if n <= 1 =n * fac (n - 1),otherwise

CPT: Functions/322 The C version: long int factorial(long int n) { if (n <= 1) return 1; else return (n * factorial(n - 1)); }

CPT: Functions/323 /* full program (fig. 5.14) */ #include long int factorial(long int n); int main() { int i; for (i = 1; i <= 10; i++) printf("%ld\n", factorial(i)); return 0; } continued

CPT: Functions/324 long int factorial(long int n) /* return the factorial of n */ { if (n <= 1) return 1; else return (n * factorial(n - 1)); }

CPT: Functions/325 Recursion Uses Lots of Memory fact(10) fact(9) fact(8) fact(1)

CPT: Functions/326 A Common Error long factorial(long n) { if (n <= 1) return 1; else return (n * factorial(--n)); }

CPT: Functions/ The Fibonacci Series l 0, 1, 1, 2, 3, 5, 8, 13, 21,... l Mathematical definition: fib n = 0if n = 0 = 1if n = 1 = fib (n-1) + fib (n-2)if n > 1

CPT: Functions/328 The C version long int fib(long int n) { if (n == 0 || n == 1) return n; else return fib(n - 1) + fib(n - 2); }

CPT: Functions/329 /* full program (fig. 5.15) #include long int fib(long int n); int main() { long int result, number; printf("Enter an integer: "); scanf("%ld", &number); result = fib(number); printf("Fibonacci(%ld) = %ld\n", number, result); return 0; } continued

CPT: Functions/330 long int fib(long int n) /* return the nth fibonacci number */ { if (n == 0 || n == 1) return n; else return fib(n - 1) + fib(n - 2); }

CPT: Functions/331 Executions Enter an integer: 5 Fibonacci(5) = 5 Enter an integer: 20 Fibonacci(20) = 6765

CPT: Functions/332 Recursion can be Inefficient fib(5) fib(4)fib(3) fib(2) fib(1) fib(3) fib(2) fib(1)fib(0) fib(1)fib(0) 10 fib(1)fib(2) fib(1)fib(0)

CPT: Functions/ Depth Tester /* Test the depth of recursion for sum() in steps of 100 */ #include long int sum(long int n); int main() { long int n = 0; for ( ; ; n +=100) printf("Recursion Test: n = %ld sum = %ld\n", n, sum(n)); return 0; }

CPT: Functions/334 long int sum(long int n) /* sum the integers between 1 and n */ { if ( n <= 1) return n; else return ( n + sum(n - 1)); }

CPT: Functions/335 Results on a PC : : Recursion Test: n = 7900 sum = Recursion Test: n = 8000 sum = Recursion Test: n = %ld sum = %ld > /* DOS prompt */

CPT: Functions/336 Execution Stack sum(8001) sum(8000) sum(7999) sum(7998) sum(1) 1 ?

CPT: Functions/337 But on a workstation : Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = : :

CPT: Functions/ A Skeleton with Functions /* Author & program details */ #include int func1(int x); double func2(double y); /*... more prototypes */ int main() { /* declare variables */ /* do something with functions */ return 0; } continued

CPT: Functions/339 int func1(int x) /* some comments */ { /* do something */ return /* an integer */ ; } continued

CPT: Functions/340 double func2(double y) /* some comments */ { /* do something */ return /* a double */ ; } /*... more function definitions */