1 / 24

Designing Operations And Structures

So far we've been writing everything in Main( ). This usually isn't an optimal approach. You'll generally want to separate your code into operations, for many reasons.Operations can generify code, so you don't have to keep re-typing the same code over and over.Operations can restrict changes t

mohawk
Download Presentation

Designing Operations And Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

    2. So far we’ve been writing everything in Main( ). This usually isn’t an optimal approach. You’ll generally want to separate your code into operations, for many reasons. Operations can generify code, so you don’t 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. Why Write Operations?

    3. 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 operation’s body of code. Arguments And Parameters

    4. 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 Example Procedure

    5. 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 Calling The Procedure

    6. 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, it’s 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. Writing Functions

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

    8. 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 Calling The Function

    9. 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. Parameter Passing

    10. 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 // won’t change num ack.Load(“x.gif”) // WILL affect spr End Method Passing Examples

    11. 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 doesn’t matter in Phrogram. Program MyHappyProgram Method Main() … End Method Method setVisible() … End Method End Program Where To Write Operations

    12. You’ll get an error if you try to call an operation with the wrong type and/or number of arguments. You’ll also get an error if you don’t 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 Type Matching

    13. 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! Overloading Operations

    14. The one thing you can’t 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! Function Signatures

    15. You can write operations with the same name as operations in other libraries. If you do, you’ll 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 Identifier Hiding

    16. One problem with operations is they only have access to the values they are passed. So if you’re 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 don’t need to be passed around. Program Variables

    17. Program MyProgram Define timer As Integer = 0 Define pics As Sprite[10] Method Initialize() timer = timer + 1 // changes timer! pics[1].Load(“a.gif”) End Method Method Main() Initialize() // no need to pass vars Print(timer) // prints “1” End Method End Program Example Program Variables

    18. 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 Self-Documenting Op Names

    19. Operations can help avoid repetitious code. Define ims As Sprite[5] NewSprite(ims[1], “f.gif”, 100, 10, True) NewSprite(ims[2], “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 Avoiding Code Repetition

    20. A structure is a collection of variables. They’re 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. Structures

    21. 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) Using Structures

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

    23. Program MovingSpriteExample Define numSprites As Integer = 10 Define spriteList As MovingSprite[numSprites] 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 Define newX As Integer = spriteList[i].spr.X + spriteList[i].xSpeed Define newY As Integer = spriteList[i].spr.Y + spriteList[i].ySpeed spriteList[i].spr.MoveTo(newX, newY) Next End Loop End Method End Program Big Example!

    24. Always put a one-line comment before each program-level or structure variable, even if it’s obvious. Always put a comment before an operation or a structure. // Global game sprites Define sprites As FancySprite[6] // 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 /* This procedure handles the loading of sprite data. */ Method initialize() … Good Commenting Practice

More Related