N5 TrueBasic Programming Theory

Download Report

Transcript N5 TrueBasic Programming Theory

A function is an instruction which takes a
value or values, “does something” to these
values and returns an answer.
You can write your own functions but
TrueBasic has many functions already built
into the language.
Here are some examples to show you how
a function works.
1
N5 TrueBasic Programming (V1)
The function
SQR(number)
takes the value number and calculates the
square root of that number.
LET answer = SQR(64)
would calculate the square root of 64 and assign the value 8
to the variable answer.
A function always ends up with a single value and this is
generally assigned to a variable or displayed on the screen.
2
N5 TrueBasic Programming (V1)
The function
ROUND(number, 2)
takes the value number and works out what
it would be rounded to 2 decimal places.
LET number = 55.2361
PRINT ROUND(number,2)
would display the value
55.24
on the screen.
3
N5 TrueBasic Programming (V1)
The function
INT(number)
takes a number and returns only the whole number part.
Let number = 54.9
PRINT INT(number)
would display the number 54.
LET number = 54.9
would also display
LET number = INT(number) the number 54.
PRINT number
4
N5 TrueBasic Programming (V1)
The function RND is used to create random numbers
The mathematics of this function is quite
complicated but use the example below to
see how it can be easily used and adapted.
RANDOMIZE
LET random_number = INT(RND * 10) + 1
PRINT “Your random number is “; random_number
Would generate a random number between 1 and 10.
5
N5 TrueBasic Programming (V1)
The word RANDOMIZE always has to be put in
before this function is used in a program.
If it is not there then the program will always come
up with the same sequence of random numbers.
RANDOMIZE
LET random_number = INT(RND * 10) + 1
PRINT “Your random number is “; random_number
The 10 in the brackets tells you the maximum number
possible and the 1 outside the brackets tells you the minimum
number it would create.
6
N5 TrueBasic Programming (V1)
What would these lines of code produce?
RANDOMIZE
LET random_number = INT(RND * 15) + 5
PRINT “Your random number is “; random_number
Answer: A random number between 5 and 15.
7
N5 TrueBasic Programming (V1)
The function
MOD(number1, number2)
Returns the remainder when you divide number 1 by number 2.
PRINT MOD(14,3)
would display the remainder which is 2.
8
N5 TrueBasic Programming (V1)
The function
LEN(string)
tells you how many characters there are in a string.
PRINT LEN(“hello”)
would display the value 5.
LET name$ = “Joseph”
PRINT “Your name has“;LEN(name$);” characters)
would display Your name has 6 characters.
9
N5 TrueBasic Programming (V1)
The function
MAX(number1, number2)
would return the larger value of the 2 numbers.
PRINT MAX(19,14)
would display the value 19.
LET first= 12
LET second= 98
PRINT “The larger number is “;MAX(first,second)
would display The larger number is 98
10
N5 TrueBasic Programming (V1)
A variable can only store one value.
Town$
What would we do if we
wanted to store the names
of 100 towns?
We could create 100 variables and call them town1$, town2$,
town3$, town4$, town5$, town6$ etc, etc,
Would you like to have to write the code for this?
Think about it?
11
N5 TrueBasic Programming (V1)
PRINT “What is the name of the first town?
INPUT town1$
PRINT “What is the name of the second town?
INPUT town2$
PRINT “What is the name of the third town?
INPUT town3$
PRINT “What is the name of the fourth town?
INPUT town4$
Only another 96 to go !!!!!!
12
N5 TrueBasic Programming (V1)
Fortunately there is a better way of doing this.
We can use an array to store a list of data which is all
of the same type.
We must first tell the computer how many items we want
to have in the list. The computer then reserves space in
the RAM for them.
The following line of code says reserve 100 spaces for
towns in the program.
DIM town$(100)
13
N5 TrueBasic Programming (V1)
DIM town$(100)
town$()
Array
(1)
DIM is for Dimension i.e. The size of the array. (2) New York
(3)
We put the DIM statement at the start
(4)
of the program
(5) London
An array looks very like any other variable
(6)
but it always has brackets after it.
(7)
PRINT town$(5)
(8) Paris
(9)
would return the value “London”
etc
14
N5 TrueBasic Programming (V1)
PRINT “Please enter a town name”
INPUT town$(6)
Would let the user enter a town name and
then assign it to position 6 in the array.
The real benefit of arrays is when we use
them with loops.
This really cuts down on the lines of code
we need to process data.
15
town$()
Array
(1)
(2) New York
(3)
(4)
(5) London
(6)
(7)
(8) Paris
(9)
etc
N5 TrueBasic Programming (V1)
FOR counter = 1 TO 100 DO
PRINT “Please enter a town name”
INPUT town$(counter)
NEXT counter
Notice that we have used the variable
counter in the bracket.
First time round the loop,
counter has the value 1.
Counter
1
The town which the user enters will go into
position (1) in the array.
16
town$()
Array
(1)
(2) New York
(3)
(4)
(5) London
(6)
(7)
(8) Paris
(9)
etc
N5 TrueBasic Programming (V1)
town$()
Array
FOR counter = 1 TO 100 DO
PRINT “Please enter a town name”
INPUT town$(counter)
NEXT counter
Second time round the loop counter has the
value 2.
The town which the user
enters will go into position
(2) in the array.
17
Counter
2
(1)
(2) New York
(3)
(4)
(5) London
(6)
(7)
(8) Paris
(9)
etc
N5 TrueBasic Programming (V1)
Displaying the names of all the towns is
easy too.
PRINT “Here are the towns”
FOR counter = 1 TO 100 DO
PRINT town$(counter)
NEXT counter
How would display only the tenth to the
twentieth town?
FOR counter = 10 TO 20 DO
18
town$()
Array
(1)
(2) New York
(3)
(4)
(5) London
(6)
(7)
(8) Paris
(9)
etc
N5 TrueBasic Programming (V1)