Computer Science Canada

Restart program

Author:  canada123 [ Sat Jun 03, 2006 11:43 am ]
Post subject:  Restart program

Does anyone know how to resart a program without putting a loop or restarting the file by stopping it and running it?

Author:  Anonymous [ Sat Jun 03, 2006 11:54 am ]
Post subject: 

If you mean by stopping and restarting from the beginning, I mae a Main Menu procedure. Then when the game ends, you call the Main Menu and reset all data. So at the beginning of your progam, declair all of your variables:
code:

var x, y, s, choice : int

procedure Game

    %Initialize/Reset data here
    x := 1
    y := 50
    s := 100

    %Game begins
    put "Game is started!"
    Input.Pause %place your game here
    put "Game has ended, play again?"
    get choice
    if choice = 1 then
        Game %goes back to Game
    else
        quit  %terminates program
    end if

end Game

Game %first call of procedure


Author:  wtd [ Sat Jun 03, 2006 12:02 pm ]
Post subject: 

Any partiular reason the procedure "Game" cannot accept x, y and s as parameters?

Author:  Anonymous [ Sat Jun 03, 2006 1:17 pm ]
Post subject: 

Or if you wish to give specific parimeters for each call, then you can change the code to this:

code:

var choice : int

procedure Game (x, y, s : int)

    %Game begins
    put "Game is started!"
    Input.Pause %place your game here
    put "Game has ended, play again?"
    get choice
    if choice = 1 then
        Game (1, 50, 100) %goes back to Game
    else
        quit  %terminates program
    end if

end Game

Game (1, 50, 100) %first call of procedure


Sorry I don't work with parimeters much, but they are very handy. For instance, making a procedure that needs to be modified frequently in different cases should use parimeters.

Author:  wtd [ Sat Jun 03, 2006 1:31 pm ]
Post subject: 

You should generally avoid global variables. You should also try to avoid procedures, generally speaking. Instead write functions that can take values as parameters and output new values based on those.

Author:  Anonymous [ Sat Jun 03, 2006 1:58 pm ]
Post subject: 

Out of all my months of doing Turing, probably 6 months around, I've never really used a function. Only to answer the teachers questions which require them. And I thought processes were the ones to avoid... Now procedures and global variables? It doesn't really matter in any of the programs that me or this person makes because were Ub3r N00b3rz.

Author:  Delos [ Sat Jun 03, 2006 2:26 pm ]
Post subject: 

vahnx wrote:
It doesn't really matter in any of the programs that me or this person makes because were Ub3r N00b3rz.


Denial will not allow you to progress from therein. Don't sweat it, fcns are really quite awsome. Start using them and you'll realize quite quickly. You do enough programming in your spare time to warrant adding some more indepth ideas to your arsenal.

Author:  MysticVegeta [ Sat Jun 03, 2006 7:13 pm ]
Post subject: 

wtd wrote:
You should generally avoid global variables. You should also try to avoid procedures, generally speaking. Instead write functions that can take values as parameters and output new values based on those.


just out of curiousity, I know to avoid global variables where possible but is it just for the sake of tidy code? or does it have to do with compiling time too?

Author:  TheOneTrueGod [ Sat Jun 03, 2006 7:21 pm ]
Post subject: 

I know that it detracts from quite a bit from modularity, but other than that, I've no clue.

Author:  Cervantes [ Sat Jun 03, 2006 7:26 pm ]
Post subject: 

The trouble with global variables is that anyone and their brother can modify their state. If you have a global variable and it gets changed by some nasty player, the rest of the code that uses that global variable will act differently, possibly crashing.

Global constants, on the other hand, aren't so bad. Because they are constant, no one can change their value. A global constant is sort of like an unchangeable parameter, except you don't have to go to the trouble of passing it in as a parameter.

Even so, parameters > global data.

Author:  wtd [ Sat Jun 03, 2006 7:36 pm ]
Post subject: 

Compiling time is not something you should be concerned about. Development time is something you most definitely should be concerned about.

Global variables and procedures operating on them lead to spaghetti code.

Take a big mass of spaghetti. Now, extract a single strand and inspect it. Not easy, is it? Smile

The same happens with global variables.

Problem solving is about breaking big problems down into smaller problems that are easier to solve. Now, you try to do this with procedures, but it's not really terribly effective. The reason?

Well, when we break a problem down, we really want to create a "black box." This is a chunk of code that takes some input, does something to it, and provides some output. It does this reliably because the program doesn't care how it does what it does, and it doesn't care about the state of the program.

The beauty of this is that when you have a problem with that function, you can look just at that function, and ignore the rest of the program.

Imagine you have 10,000 lines of code. You have a 5 line procedure that's misbehaving. It's not producing the expected results. It uses the global variable "foo." So the first thing you do is track down where "foo" is declared. You realize that's ok. Now, you track down every other procedure that could possibly modify "foo" and make sure they aren't doing anything funky with it.

Now, take two Tylenol and hope the headache goes away.

Or, if your procedure were a function, you could just look at those five lines, and chances are good, if you use functions like a good programmer, the problem never would have arisen.

Author:  TheOneTrueGod [ Sat Jun 03, 2006 9:43 pm ]
Post subject: 

Yarr, thats an answer that i've been waiting a good half a year now to hear. Thanks wtd! Very Happy

+Bits
I am aware that you are a mod, I'm still giving them to show my appreciation.

Author:  wtd [ Sat Jun 03, 2006 11:11 pm ]
Post subject: 

You're quite welcome.

Thanks for the meaningless bits. Wink


: