7: Functional programming: Functions, conditionals and recursion - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

7: Functional programming: Functions, conditionals and recursion

Description:

start with a letter, then any letters or numbers. No spaces or weird ... countdown(NUM-1 ) } recursion/simplification. Example: number sequence. See overheads ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 21
Provided by: markels
Category:

less

Transcript and Presenter's Notes

Title: 7: Functional programming: Functions, conditionals and recursion


1
7 Functional programmingFunctions,
conditionals and recursion
  • Mark Elsom-Cook 1999

2
Contents
  • Variables
  • Functions
  • Operators
  • Problem decomposition
  • Conditional statements
  • Recursion

3
Variables
  • Declare them
  • var myname
  • var pigs, dogs, tadpoles
  • Use them
  • pigs 23
  • Rules
  • start with a letter, then any letters or numbers
  • No spaces or weird characters
  • Case matters

4
Object variables
  • Declare from instance object
  • var myobj new object()
  • myobj.height56
  • myobj.weight7
  • Use as dotted syntax
  • writeln(myobj.height)

5
Functions
  • Use them fname() freda(3, jane, dog)
  • Declare them
  • function myfunc( myarg, myarg2, myarg3)
  • the body
  • Built in functions
  • Object methods are just functions in objects

6
Functions and variables
  • Global scope
  • Local scope
  • Automatic declaration of parameters

7
Operators
  • Same as functions, but different syntax
  • Built-in
  • , -, , /, , , -- and assignment versions
  • , gt, lt, !, !, typeof, new
  • Mostly infix - typeof and new are prefix

8
Problem decomposition
  • Key concept in computing
  • Analyse problem into major components
  • Solve the high level problem
  • and assume the other bits are already there
  • Solve smaller problems till it disappears
  • Lots of functions is good!
  • E.g. How to solve it, G. Polya

9
Example Table generation
  • function drawTimetable()
  • drawTop()
  • drawMiddle()
  • drawTail()

10
Table generation 2
  • function drawTop()
  • document.writeln("lttable border2gtlttrgtltthgtlt/thgtltt
    hgtA.M.lt/thgtltthgtP.M.lt/thgtltthgtEveninglt/thgtlt/trgt")
  • function drawTail()
  • document.writeln("lt/tablegt")

11
Table generation 3
  • function drawMiddle()
  • drawDay("Monday")
  • drawDay("Tuesday")
  • drawDay("Wednesday")
  • drawDay("Thursday")
  • drawDay("Friday")
  • function drawDay(today)
  • document.writeln("lttrgtltth bgcolorbluegt"
    today "lt/thgtlt/trgt")

12
Conditional statements
  • Actions are carried out depending on some test
  • if ( test ) if ( test)
  • actions to do actions1
  • else actions 2

13
Predicates (tests)
  • Functions which return true or false
  • isNaN(), , lt, lt, gt, gt, !, ,
  • Also a set for boolean operations
  • Used inside a conditional test part
  • Or make up your own!

14
Example browser specific code
  • Set a variable for different browsers
  • var microsoft
  • if (navigator.appName Netscape )
  • microsoftfalse
  • else
  • microsofttrue

15
Example Reacting to user input
  • An age test
  • var age
  • age window.prompt(How old are you?)
  • if (age lt 18)
  • document.writeln(You seem bit young)
  • else
  • if (age gt70)
  • document.writeln(Nice to see
    you here!)
  • else
  • document.writeln(That seems about
    right)

16
Recursion
  • A really clever idea!
  • Simplify a problem by calling a function
  • Recurse by calling a function from itself
  • A means for doing things repeatedly
  • Has initial value, stopping condition,
    simplification, accumulation

17
Recursive structures
  • Function calls itself
  • function countdown(NUM)
  • if (NUM 0) Stopping condition
  • document.writeln(0)
  • else
  • document.writeln(NUM)
  • countdown(NUM-1 ) recursion/simplification

18
Example number sequence
  • See overheads

19
Example checked user input
  • See examples

20
Summary
  • Functions, variables operators
  • Defining vs. Using
  • Conditional expressions
  • Recursion
Write a Comment
User Comments (0)
About PowerShow.com