
-----------------------------------
Flea
Mon Oct 18, 2004 6:22 pm

Proper Format
-----------------------------------
I use a lot of procedures in my bigger programs.  In fact, my programs are almost entirely procedures with just a little code to call them.  :? 

I know that "goto" in other languages is really frowned upon by a lot of people (university profs @ mac for example).. but to me, procedures seem the same, since a procedure is (im guessing) just two goto lines with code in between.

So my question is, should i keep building my programs like this or will it cause problems later on?  I mean i could have like a zillion nested loops in a big mash of code... I know its probably up to my discresion, but which is the proper thing to do?

-----------------------------------
wtd
Mon Oct 18, 2004 6:55 pm


-----------------------------------
You should rewrite as many of those procedures as possible as functions.

But even if you keep the procedures, you should make sure that you don't have any reliance on global variables.

Consider something like a program where you want to move something around the screen, and you're keeping track of x and y coordinates.  You might have a procedure to move them diagonally, where each coordinate gets increased by 1.

var x, y : int := 0

procedure moveDiagonally
   x += 1
   y += 1
end moveDiagonally

moveDiagonally

Instead of relying on those variables existing, let's pass in any two variables.

procedure moveDiagonally(var x, y : int)
   x += 1
   y += 1
end moveDiagonally

var x, y : int := 0

moveDiagonally(x, y)

The other major improvement you can make is to learn to use records to tie related pieces of information together.

Instead of passing in the x and y coordinates separately, we create a record which holds both of them and lets us treat the two integers together as a single piece of data.

type Point : record
   x : int
   y : int
end record

procedure moveDiagonally(var p : Point)
   p.x += 1
   p.y += 1
end moveDiagonally

var p : Point

moveDiagonally(p)

Of course, we can improve on even that, by incorporating classes, where we hide the actual data, and only allow very select access to it.  This prevents unintended data corruption.

class Point
   export init, getX, getY, moveDiagonally

   var x, y : int

   procedure init(init_x, init_y : int)
      x := init_x
      y := init_y
   end init
  
   function getX : int
      result x
   end getX

   function getY : int
      result y
   end getY

   procedure moveDiagonally
      x += 1
      y += 1
   end moveDiagonally  
end Point

var p : pointer to Point
new Point, p
p->init(0, 0)
p->moveDiagonally

-----------------------------------
apomb
Tue Oct 19, 2004 1:23 pm


-----------------------------------
how is he going to use pointers, turing doesnt support pointers,  ... or does it  :think:  that is a good question, cuz i copied that last bit of wtd's code and it made pointer bold ... hmmm, i never knew turing could use pointers... can someone pleasehep me out with this one.  sorry flea for hijacking your thread, but i really want to know if wtd's code would work... cuz theres a couple of errors

-----------------------------------
wtd
Tue Oct 19, 2004 1:31 pm


-----------------------------------
A very simple example that does work:

class Foo
    export bar
    procedure bar
        put "baz"
    end bar
end Foo

var i : pointer to Foo
new Foo, i
i->bar

-----------------------------------
Flea
Tue Oct 19, 2004 4:24 pm


-----------------------------------
That info is extremely useful and easy to understand.  You can definately explain things well, wtd, thanks a ton =D

even compwiz is learning  :D

-----------------------------------
wtd
Tue Oct 19, 2004 4:32 pm


-----------------------------------
That info is extremely useful and easy to understand.  You can definately explain things well, wtd, thanks a ton =D

Thank you, and you're welcome.
