Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Proper Format
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Flea




PostPosted: Mon Oct 18, 2004 6:22 pm   Post subject: 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. Confused

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?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Mon Oct 18, 2004 6:55 pm   Post subject: (No subject)

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.

code:
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.

code:
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.

code:
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.

code:
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




PostPosted: Tue Oct 19, 2004 1:23 pm   Post subject: (No subject)

how is he going to use pointers, turing doesnt support pointers, ... or does it Thinking 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




PostPosted: Tue Oct 19, 2004 1:31 pm   Post subject: (No subject)

A very simple example that does work:

code:
class Foo
    export bar
    procedure bar
        put "baz"
    end bar
end Foo

var i : pointer to Foo
new Foo, i
i->bar
Flea




PostPosted: Tue Oct 19, 2004 4:24 pm   Post subject: (No subject)

That info is extremely useful and easy to understand. You can definately explain things well, wtd, thanks a ton =D

even compwiz is learning Very Happy
wtd




PostPosted: Tue Oct 19, 2004 4:32 pm   Post subject: (No subject)

Flea wrote:
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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: