Designing Operations And Structures - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Designing Operations And Structures

Description:

Operations can generify code, so you don't have to keep re-typing the same code over and over. ... are similar to procedures, but they also have a return type. ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 25
Provided by: awo4
Category:

less

Transcript and Presenter's Notes

Title: Designing Operations And Structures


1
Designing Operations And Structures
  • CSE 203 Lecture 4Annatala Wolf

2
Why Write Operations?
  • So far weve been writing everything in Main( ).
    This usually isnt an optimal approach. Youll
    generally want to separate your code into
    operations, for many reasons.
  • Operations can generify code, so you dont have
    to keep re-typing the same code over and over.
  • Operations can restrict changes to a single place
    in code, which helps prevent errors.
  • Operations can make it MUCH clearer what your
    code does, and help simplify the design process.

3
Arguments And Parameters
  • When you call an operation, the values you pass
    in are called arguments (or actual parameters).
    For example
  • Foo(x, 30) // x 30 are arguments to Foo
  • When an operation runs, the first thing that
    happens is variables called parameters (or
    formal parameters) are created. Then, the
    values of the arguments are copied into the
    parameters. This lets you access these values
    inside the operations body of code.

4
Example Procedure
  • Method MoveSprite ( mSprite As Sprite,
  • xMove As Integer,
  • yMove As Integer )
  • Define newX As Integer
  • Define newY As Integer
  • newX mSprite.X xMove
  • newY mSprite.Y yMove
  • mSprite.MoveTo(newX, newY)
  • End Method

5
Calling The Procedure
  • Now, consider the following code. How does using
    the procedure make the following code simpler?
  • MoveSprite(flame, 20, 10)
  • MoveSprite(ball, -50, 0)
  • MoveSprite(dino, 0, 5)
  • If (timer 20) Then
  • MoveSprite(bigBall, 100, 0)
  • End If

6
Writing Functions
  • Functions are similar to procedures, but they
    also have a return type.
  • You must include a return statement at the end of
    the function. This statement returns the value
    of the return variable to the place where the
    function was called.
  • While you can have multiple return statements,
    its a bad idea to have more than one. As soon
    as you return, the function ends. Try to have
    only one return statement, at the very end of the
    function.

7
Example Function
  • Function Square ( num As Integer ) As
  • Integer
  • Define result As Integer
  • result num num
  • Return result
  • // Nothing after this will run!
  • End Function

8
Calling The Function
  • You can call a function you write just like you
    would call any static function.
  • Define speed As Integer Square(3)
  • speed Square(Square(speed))
  • // Now speed 6561
  • While (AreWeDoneYet())
  • Print(Whee!)
  • End While

9
Parameter Passing
  • Phrogram passes primitive types (Integer, String,
    Decimal, and Boolean) by copy. This means if you
    pass in a primitive type, it cannot change the
    value of the variable you passed in. It only has
    a temporary copy.
  • Other types (arrays, classes, structs) are passed
    by reference. So if you change the value, it
    WILL change the original.

10
Passing Examples
  • Method Main()
  • Define num As Integer
  • Define spr As Sprite
  • Foo(num, spr) // num passed by copy
  • End Method
  • Method Foo (value As Integer,
  • ack As Sprite)
  • value 3 // wont change num
  • ack.Load(x.gif) // WILL affect spr
  • End Method

11
Where To Write Operations
  • To write a static procedure or function, simply
    place it inside the Program block, but outside
    Main( ).
  • In some languages, the order that operations
    appear matters. Operation order doesnt matter
    in Phrogram.
  • Program MyHappyProgram
  • Method Main()
  • End Method
  • Method setVisible()
  • End Method
  • End Program

12
Type Matching
  • Youll get an error if you try to call an
    operation with the wrong type and/or number of
    arguments. Youll also get an error if you dont
    match the return type of a function to the place
    you use the value where you call it.
  • Method Foo(a As Integer, b As String)
  • End Method
  • Method Main()
  • Foo(stuff, 3) // error!
  • End Method

13
Overloading Operations
  • However, you can write several versions of an
    operation with different operation signatures.
  • Method FancyPrint(thing As String)
  • End Method
  • Method FancyPrint(stuff As Integer)
  • End Method
  • Method Main()
  • FancyPrint(hello) // works
  • FancyPrint(12) // also works!

14
Function Signatures
  • The one thing you cant overload is a function
    return type. Function return types must be the
    same for every function with the same name.
    (This is because the program has no way to know
    which one you intend to call.)
  • Function Foo() As Integer
  • Function Foo() As String // error!

15
Identifier Hiding
  • You can write operations with the same name as
    operations in other libraries. If you do, youll
    need to use the full name of the library
    operation to use it, because the simple name is
    hidden by your version.
  • Method Print(foo As String)
  • End Method
  • Method Main()
  • Print(blah) // calls your Print
  • Drawing.Print(!) // calls other Print
  • Print(3) // also calls Drawing.Print()
  • End Method

16
Program Variables
  • One problem with operations is they only have
    access to the values they are passed. So if
    youre working with a bunch of Sprites, you might
    have to pass all those Sprites in, which could be
    a pain.
  • One solution is to declare some variables at the
    program level. These program variables can be
    seen and modified by ALL operations in the
    program, so they dont need to be passed around.

17
Example Program Variables
  • Program MyProgram
  • Define timer As Integer 0
  • Define pics As Sprite10
  • Method Initialize()
  • timer timer 1 // changes timer!
  • pics1.Load(a.gif)
  • End Method
  • Method Main()
  • Initialize() // no need to pass vars
  • Print(timer) // prints 1
  • End Method
  • End Program

18
Self-Documenting Op Names
  • Operations can provide clarity to code by having
    sensible names and replacing large pieces of code
    with simpler calls.
  • While (Not (IsKeyDown(ESC) And Not
  • flag)
  • MoveSprites()
  • flag CheckForIntersection()
  • timer (timer 1) Mod 100
  • End While

19
Avoiding Code Repetition
  • Operations can help avoid repetitious code.
  • Define ims As Sprite5
  • NewSprite(ims1, f.gif, 100, 10, True)
  • NewSprite(ims2, d.gif, 30, 45, False)
  • Method NewSprite ( spr As Sprite, pic As
  • String, xPos As Integer, yPos As
  • Integer, flipped As Boolean )
  • spr.Load(pic)
  • spr.MoveTo(xPos,yPos)
  • If (flipped) Then
  • spr.FlipHorizontal()
  • End If
  • End Method

20
Structures
  • A structure is a collection of variables.
    Theyre useful when you want to declare a bunch
    of things at once.
  • Structure NamedSprite
  • spr As Sprite
  • name As String
  • End Structure
  • This structure defines a new type, which is
    basically a String and a Sprite stuck together.

21
Using Structures
  • Structures should be defined inside the Program
    but outside of Main( ).
  • To use a structure, make a new variable of that
    type. You can use the dot operator to access and
    change elements of the structure.
  • Define mySprite As NamedSprite
  • mySprite.spr.Load(foo.gif)
  • mySprite.name My Happy Foo
  • Print(mySprite.name)

22
How Structures Work
  • When you define a structure, youre not actually
    defining any variables.
  • The variables inside a structure are created when
    you make a variable of the structures type.
  • For example, this statement creates a new String
    called bar.name, and a new Sprite called bar.spr
  • Define bar As NamedSprite

23
Big Example!
  • Program MovingSpriteExample
  • Define numSprites As Integer 10
  • Define spriteList As MovingSpritenumSprites
  • Struct MovingSprite
  • spr As Sprite
  • xSpeed As Integer
  • ySpeed As Integer
  • End Struct
  • Method Main()
  • End Method
  • Method MoveSprites(Integer delay, Integer
    frames)
  • Loop frames
  • Delay(delay)
  • Define i As Integer
  • For i 1 to numSprites

24
Good Commenting Practice
  • Always put a one-line comment before each
    program-level or structure variable, even if its
    obvious. Always put a comment before an
    operation or a structure.
  • // Global game sprites
  • Define sprites As FancySprite6
  • // Moving sprite structure (Place a blank
    line before each comment.)
  • Structure FancySprite
  • // Sprite object
  • spr As Sprite
  • // Speed of motion
  • speed As Decimal
  • // Direction of motion (radians)
  • degrees As Decimal
  • End Structure
Write a Comment
User Comments (0)
About PowerShow.com