Function I - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Function I

Description:

CGS 3460. Function I. What is a function. One named ... double CalcArea(double hight, double width) return height * width; int main() double h, w, area; ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 12
Provided by: rzh
Category:
Tags: function | hight

less

Transcript and Presenter's Notes

Title: Function I


1
Function I
  • What is a function
  • One named program module that does one primary
    task
  • Consists of a set of statements
  • Why we need functions?
  • Chop up a long program for
  • Better organization of code
  • Use same code repetitively
  • Each function acts as a building block

2
Example Area Calculation
  • double CalcArea(double hight, double width)
  • return height width
  • int main()
  • double h, w, area
  • printf(Please input height and width\n)
  • scanf(f, f, h, w)
  • area CalcArea(h, w)
  • printf(The area is f, area)
  • return 0

3
Example II
  • Compute the factorials
  • double factorial(int n)
  • double result 1
  • int i
  • for( i 2 i lt n i)
  • result result i
  • return result

4
Example factorials
  • 5! 5 4 3 2 1
  • Notice that
  • 5! 5 4!
  • 4! 4 3! ...
  • Can compute factorials recursively
  • Solve base case (1! 0! 1) then plug in
  • 2! 2 1! 2 1 2
  • 3! 3 2! 3 2 6
  • factorial(5) 5 factorial(4)

5
Example factorials
  • factorial(n) n factorial(n-1)
  • double factorial(int n)
  • double result
  • if ( n lt 1 )
  • return 1
  • else
  • result n factorial(n 1)
  • return result

Does this code work?
6
Recursive Function Call
  • double factorial(int n)
  • if ( n lt 1 )
  • return 1
  • else
  • return (n factorial(n 1))

double factorial(int n) if ( n lt 1 ) return
1 else return (n factorial(n 1))
n4
n3
return (4 factorial(4 1))
return (3 factorial(3 1))
6)
factorial( 3))
factorial( 2))
2)
double factorial(int n) if ( n lt 1 ) return
1 else return (n factorial(n 1))
double factorial(int n) if ( n lt 1 ) return
1 else return (n factorial(n 1))
n1
n2
return 1
return (2 factorial(2 1))
factorial( 1))
1)
7
Recursion
  • Recursive functions
  • Functions that call themselves
  • Can solve a base case or base cases
  • Divide a problem up into
  • Base case(s), which is what it can do
  • Others
  • The function launches a new copy of itself
    (recursion step), leading to base case(s)
  • Eventually base case gets solved
  • Gets plugged in, works its way up and solves
    whole problem

8
Recursive Approaches
  • Recursive function is called to solve a problem
  • Know how to solve only the simplest case(s), or
    so-called base case(s)
  • Resemble a complex problem in a simpler or
    smaller version of the same problem.
  • Execution
  • The recursion step execute while the original
    call to the function is still open, i.e., it has
    not yet finished.
  • May be many recursive calls.
  • All calls converge on the base case(s)

9
Example
  • Function power(base, exponent) which returns
    baseexponent
  • Recursion relationship
  • baseexponent base baseexponent-1
  • Base case
  • base1 base

10
Code
  • double power(float base, int exponent)
  • if (exponent 1)
  • return base
  • else
  • return( base power(base, exponent-1))

11
Fibonacci series
  • Fibonacci series 0, 1, 1, 2, 3, 5, 8...
  • Each number is the sum of the previous two
  • Can be solved recursively
  • fib( n ) fib( n - 1 ) fib( n 2 )
  • Code for the fibaonacci function
  • long fib( int n )
  • if (n 0 n 1) // base case
  • return n
  • else
  • return( fib( n - 1) fib( n 2 ) )
Write a Comment
User Comments (0)
About PowerShow.com