The Absolute Basics Of Programming For Beginners (Part 2)
In part 2 of our absolute beginners guide to programming, I’ll be covering the basics of functions, return values, loops and conditionals. Make sure you’ve read part 1 before tackling this, where I explained the concepts of variables and datatypes. You won’t need to do any actual programming yet – this is all still theoretical and language-independent.
Next time, we’ll start putting all this into practice with some real code. If you’ve always promised yourself you’re going to do some programming some day, now is a great time to start.
To quickly recap, last time I explained what variables were and some of the basic types data they can store. You should be able to explain what the following datatypes are:
We also looked at the difference between strongly-typed, and weakly-typed programming languages; and the advantages and disadvantages of each. Lastly, let’s make sure you know the difference between statements of assignment, and of equality. Which of the following assigns variable B to variable A, and which tests if they have the same value?
- a = b;
- a == b;
Great! If you’ve come this far, that’s an amazing achievement and you’re well on your way to making your own software! Let’s move on today’s lesson.
Conditionals and Loops
There are three structural building blocks of a program that you need to know, and they are almost universally referred to as: IF, FOR, and WHILE.
IF is one-time test. “IF a is true, then do this”. IF a isn’t true, the program ignores whatever comes next and carries on with the rest of the code. You can also provide an alternative with ELSE. “If a is true, then do this, ELSE do this”. It allows you to make decisions in the program depending on a variable. Remember the test for equality that we learnt about earlier?
That could be used in an IF clause, for example:
if(a==b) print “a is equal to b” else print “a is not equal to b”
FOR is a way to loop over the same code a set number of times. There is no testing involved – it just repeats the same block of code however many times we tell it to.
WHILE is also a loop, but instead of performing the options a pre-determined number of times, it performs a test each time the loop is performed and continues to loop until the test fails. If the test continues to be true, it never stops looping. This can cause problems if you have a bug in your WHILE look, leading to unresponsive programs that crash in an endless loop.
These 3 basic structures create the traffic lights and road diversions that control the logical flow around an application.
A function is just a group of code that’s been given a name. By grouping a block of code together and naming it, we can re-use it later and throughout the application without having to rewrite the whole code block again. Not only does it save time and reduces the overall code size of an application, it also means that if there was something wrong, we would only need to change it in one place.
Nearly all programming languages come with a built in set of functions that you can use in your application. For instance, ECHO is a function found in many languages that displays some text on screen. There are usually also some functions to deal with reading and writing files, graphical or audible output, keyboard and mouse input, and mathematics. You can use all these built-in functions without knowing how they actually work – all you need to know is the correct function name, and the parameters it requires. Wait.. parameters?
Parameters and Return Values
We often refer to functions as “taking” certain variables and “returning” something back to us. It might help to consider a function as a machine you can’t necessarily see inside of. You can put things into it, and you might get something out again, but you don’t actually have to know what goes on inside the machine. The things you put in are the parameters – variables of data that the function will work with. The return value is the output of the function – the data that will be given back to you once it’s finished running through it’s logic.
In the example above, the “signature” of the function is one that accepts two variables (a and b), and returns one (c).
Sometimes functions won’t return any values at all, though it’s common practice in those cases just to return a Boolean value of true or false to show whether it was successful or not. If you called a function to save a file for example, you wouldn’t necessarily want any variables back, but you would like to know if the file was written correctly or if there was an error.
In all applications, variables and data are constantly being “passed around” through thousands of different functions, each of which performs it own distinct purpose in the big scheme of things.
Check What You Learnt Today
To recap today’s points, you should understand what the following do in any programming language, and how they control the flow of the application:
You should also understand what a function is, and what I mean when I say “this function accepts a string and returns a Boolean”.
That’s all for this lesson. Next time, we’ll try to put some of this knowledge into practice using real code to write a small application, though I haven’t decided which language to tackle first. If you have any requests, then perhaps you could post in the comments. What programming languages do you think are the most important today?