Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2007ACS-1805 Ron McFadyen1 Functions and if-else A function is a collection of statement, similar to a method, but a function is defined to return.

Similar presentations


Presentation on theme: "Fall 2007ACS-1805 Ron McFadyen1 Functions and if-else A function is a collection of statement, similar to a method, but a function is defined to return."— Presentation transcript:

1 Fall 2007ACS-1805 Ron McFadyen1 Functions and if-else A function is a collection of statement, similar to a method, but a function is defined to return a value to the point where it is called. Many built-in functions We can also define our own. Last statement in a function is a “return” statement. Methods are designed to ask objects to perform actions. Functions are designed to inform us regarding some aspect of the state of the world

2 Fall 2007ACS-1805 Ron McFadyen2 Functions and if-else An if-else is a decision control structure that is used in methods or functions to select one set, or another set, of instructions for execution.

3 Fall 2007ACS-1805 Ron McFadyen3 Functions and if-else A function is said to accept a number of inputs (argument values passed to parameters) and produces a single output (the return value)

4 Fall 2007ACS-1805 Ron McFadyen4 Types of Functions The type of a function depends on the type of value it returns. a number a specific object a color a Boolean (true or false) other property values…

5 Fall 2007ACS-1805 Ron McFadyen5 Example A common task in sports game programs is to bounce a ball. The ball has been positioned 1 meter from the net.) The ball should move up and forward and then down and forward in a pattern that looks something like this:

6 Fall 2007ACS-1805 Ron McFadyen6 Storyboard for example A design for a world-level method: World.ballOverNet: Do in order toyball turn to face the net Do together toyball move up toyball move forward Do together toyball move down toyball move forward

7 Fall 2007ACS-1805 Ron McFadyen7 Example BallOverNet.a2w a built-in height function is used to determine the height of the net. The ball moves up enough to clear the net. the toyball's up and down movements are relative to the ground.

8 Fall 2007ACS-1805 Ron McFadyen8 Example: Rolling a ball Another sports game action involves rolling a ball along the ground. We want a realistic motion. The ball must simultaneously move and roll. realisticRoll Do together move ball forward 1 meter turn ball forward 1 revolution BallRolling.a2w

9 Fall 2007ACS-1805 Ron McFadyen9 Number of revolutions The number of revolutions depends on the size of the ball The number of revolutions is distance/(  * diameter) But there is no built-in function to return the number of revolutions one revolution four revolutions

10 Fall 2007ACS-1805 Ron McFadyen10 Parameters We want to return the value computed as distance / (  * diameter) We need the ball’s diameter there is a built-in width function the distance the ball must travel

11 Fall 2007ACS-1805 Ron McFadyen11 Levels of Functions As with methods, functions can be either class-level or world- level. The guidelines for class-level methods also apply to class- level functions: No references to other objects. No references to world-level functions you have written, but built-in world-level functions are fine to use


Download ppt "Fall 2007ACS-1805 Ron McFadyen1 Functions and if-else A function is a collection of statement, similar to a method, but a function is defined to return."

Similar presentations


Ads by Google