Learning Code

I’m glad that it helped you out.

I will attempt to provide some answers in ways that someone that has no programming experience would understand. (keep in mind, I’m using pseudo-code, not any actual programming language to demonstrate my point)

A “function” is what you would use to group some instructions together. Why do we use functions? Well in the early days of coding we didn’t. We just had one long list of instructions running down the screen. Something like

PRINT "HELLO" ON SCREEN
PRINT "WORLD" ON SCREEN
WAIT FOR KEY PRESS
MOVE CURSOR DOWN
DRAW A CIRCLE
PRINT "HELLO" ON SCREEN
PRINT "WORLD" ON SCREEN
WAIT FOR KEY PRESS
MOVE CURSOR TO END OF SCREEN
DRAW A RECTANGLE
etc..

As you can see this could get really long and if you look closely, there are some repeated instructions there. There must be a simpler way to present these instructions! That’s where functions come in handy. We can group instructions that will be repeated into a function and we can give that function a name. That means anywhere in the code we can mention the function’s name and all the instructions inside it will be executed.

FUNCTION SAY_HELLO
   PRINT "HELLO" ON SCREEN
   PRINT "WORLD" ON SCREEN
   WAIT FOR KEY PRESS
END

Now we can cut down our original set of instructions by replacing groups of code with a function name

SAY_HELLO
MOVE CURSOR DOWN
DRAW A CIRCLE
SAY_HELLO
MOVE CURSOR TO END OF SCREEN
DRAW A RECTANGLE
etc..

What if I want to print GOODBYE WORLD? We can make another function for that.

FUNCTION SAY_GOODBYE
   PRINT "GOODBYE" ON SCREEN
   PRINT "WORLD" ON SCREEN
   WAIT FOR KEY PRESS
END

What if I want to print GOOD MORNING WORLD? We could make a 3rd function. But if you look closely, all those sentences are the same apart from the words before WORLD. It would be simpler if we could represent all those sentences with just the one function. Well you can with parameters! A parameter is some kind of data that you pass to the function. It can be a number, a word, a sentence, even an image. Here we will pass a greeting parameter

FUNCTION SAY_SOMETHING (greeting)
   PRINT greeting ON SCREEN
   PRINT "WORLD" ON SCREEN
   WAIT FOR KEY PRESS
END

Now we can use it like this:

SAY_SOMETHING(hello)
DRAW CIRCLE
SAY_SOMETHING(goodbye)
DRAW RECTANGLE

Thanks @PunkOffice

Whoops! I just edited my previous post to use proper formatting for code snippets.
Here’s my rundown of what IF statements are for (apologies Goatboy if you already know this stuff, but others might appreciate it)

Often when writing code you don’t know what direction your program should be going. You don’t know if the race car in your game will turn left or right until the user presses the LEFT or RIGHT key. So while the game is running, you will need code that can determine which path to take.

GET USER KEY PRESS
IF USER PRESSED "LEFT"
   STEER CAR LEFT
END
IF USER PRESSED "RIGHT"
   STEER CAR RIGHT
END

A condition is the statement that results to TRUE or FALSE, so the condition here is USER PRESSED “LEFT”. So if I asked DID THE USER PRESS “LEFT”? You can answer with either TRUE or FALSE.

I thinnk it would help people more if it was on its correct syntx.