Pages

5. A Fundamental Set of Instructions Part 2

A Fundamental Set of Instructions Part 2

Recap

In the previous post we started on a short program that promised to show you a set of instructions that you would use in most of your own programs.  The program, called Fundamental, comprises of the following steps:

Fundamental
  1. ask “what is your name?”
  2. say “hello” name
  3. ask “when were you born” name “?”
  4. age is getAge(date)
  5. say “Hi name, at the end of 2017 you will be “ age “ years old”
  6. write name, dob, age to text file
  7. clear screen
  8. reset name, dob, age
  9. read name, dob, age from text file
  10. print name “was born on” dob “and so is “ age “ years old”
function getAge (date)
  1. calculate age from date
  2. return age
In the previous post we covered lines 1 and 2 and we saw how to create a string variable and use that variable to hold some data.

Integer Variable

Line 3 asks for your birth year.  We could go for the full birth date but that is more complicated than I want to tackle in this post, so let's stick to the year only; we'll call the variable yourDob for your date of birth. Add the following line after the first Dim line:

Dim as integer yourDob = 0

As we saw in the previous post, the Dim as prepares a location for our variable.  The integer tells the Dim as that the variable will contain integers (whole numbers).  And, as before, we initialize the variable with a 0 this time.  We don't put the 0 in double quotes as we want the compiler to see it as a number not a character. To be clear 0 and "0" are viewed completely differently, the first is the number zero, the second is the character 0.

String Concatenation

Enter the following line on a new line below the  previous Print line:

Print "What year were you born, " + yourName ;

This new line shows how we add two strings together.  The + symbol concatenates the strings "What year were you born, " with yourName.  You can join multiple strings together on the one line and even assign them to a new string variable.  For instance, you could write the following:

Dim as string a = "Hello "
Dim as string b=  "World "
Dim as string c= ""

c = a + b
print c

to give the familiar Hello World message. The semicolon after yourName tells the compiler not to advance the cursor to the next line but to continue from this point.  You will get the idea when you run the code below.

We have asked for a year of birth but not actually requested any input so let's add the following line after the second print:
Input ; yourDob

The semicolon after the input in this line adds a "?" character and  waits for your input.  So the two new lines, apart from declaring our variable yourDob with the Dim as statement, should look like:

Print "What year were you born, " + yourName ;
Input ; yourDob

You can compile your code now and see what it does.  Have a play with the semicolons to see what effect they have on line breaks and question marks.

Fundamental so far

Function

Often when programming we need to write a block of code that performs a discrete action.  When the block of code simply performs an action and does not return any data that block is known as a procedure.  When the action returns a value then the block of code is known as a function.

To illustrate this concept let's look at our program.  We now have yourDob and want to know how old you will be at the end of 2017.  (I don't know how you feel about it but 2016 has flown past!) This is a simple calculation where you obviously subtract yourDob from 2017.  Let's write it as a function so we get an understanding of how functions work.

Below Sleep add the word End to signify the end of the program.  Now on a new line below that add the following lines:

Function getAge (age as Integer) as Integer
     Dim as integer calcAge = 0
     
     calcAge = 2017 - age

     return calcAge
End Function

Let's work our way through this bit of code.  The word Function and tells the compiler that we are writing a function, (who would have guessed!). The next word getAge is the name of the function.  Functions, as explained earlier, perform a discrete action so often have verbs as names. The brackets surround a new integer variable called age.  When we invoke, also known as calling, the function we will be passing the variable yourDob.  Function getAge will then give age the value in yourDob so it needs to be of the same variable type.  Finally we have, as Integer; this refers to the function getAge and the value it will send back.  Since Functions return values, the compiler needs to know what data type will be returned.

The Dim line should now make sense to you; we are creating an integer variable called calcAge and setting it to 0.  The next line is our calculation, again pretty self explanatory.  The return calcAge line tells the compiler that we need to send back the value of calcAge to the calling line. Note that the data type for calcAge is the same as the that for Function getAge as we have instructed. The last line simply ends the function.


The final line we need to add to our program is one that declares the function to the compiler.  This is placed at the top of the program and above the Dim statements.  It needs to appear before you use the function.  The line is as follows:
Declare Function getAge (age as Integer) as Integer

Fundamental including function


The program compiles and runs normally but you may be surprised to find that the code for Function getAge does not appear to do anything.  That is because we have not called or invoked the function.  To call the function you need to add the following line after yourDob input line:

Print
Print "Hi " + yourName + ",  at the end of 2017 you will be "_
+ Str(getAge(yourDob))+ " years old."

There are a couple of concepts in this line that we need to focus on.  The first Print statement gives us a line break after the previous input so that the input does not run into the text we want to display next.  The start of the next line should be well understood until we get to the  underscore character.  This is used by FreeBASIC to continue long lines onto the next line.  It acts as a continuation command.  The next statement needs you to pay attention - Str(getAge(yourDob)).  Inside the first set of brackets you will see getAge(yourDob).  A function is called by naming it and giving it any parameters it needs.  In this case we pass yourDob to the getAge() function by putting it in the second set of brackets.  The function getAge will then do its arithmetic using yourDob and return calcAge.  If you had entered 1980 for yourDob then the result returned would be 37.  The number 37 is a number and cannot be added to a string. We need to convert the 37 to a string for the concatenation to be work, and that is where the FreeBASIC built in function Str() comes in.  Given a number it returns the number as a string of characters.  We can then use it to complete the sentence we wanted to create.  Go ahead and compile and run the program.

Fundamental program at end of  Post
This is a simple program so we could have written the code within getAge() in line with the rest of our code.  Having a function gives us many benefits, among them:

  1. the main part of the program is shorter and easier to read.
  2. we can reuse the function in any other program that needs to use that calculation.  Simply cut and paste the function.  You need to remember to declare it prior to using it.
  3. finding errors becomes easier.  If the age we get back is incorrect we know we need to check the logic in the function.
  4. programming becomes easier and can be shared within a team of programmers through the use of function and procedure stubs.  Essentially a stub is a simply function that does not contain any logic but returns a value as if it did. It allows us to write our main program, call functions, but not have to worry about the details of how the function works. We'll cover this aspect in a future post.


In this post we continued working on Fundamental.bas.  We know how to ask for input and display text.  We know how to concatenate strings and we know how to convert a number into a string so it can be part of a larger string. We know how to write a function and have it return the result of a calculation.  Next post we will look at reading and writing text files.  

Between posts play around with some of the commands you have been shown.  If you cause errors, find what you changed and understand why the error occurred.  Try writing other programs based on the commands shown here. This sort of experimentation will help you understand what is going on much better than simply reading the text. You cannot learn to play the guitar without practice no matter how many books you read about guitars.
Please feel free to leave a comment and see you in the next post.

1 comment:

  1. there are a couple of "o"s that should be "0"s, and a "simply function" should be "simple function"

    ReplyDelete