Sunday, February 24, 2019

Programming for school teachers 1

Something useful for teachers is keeping a spreadsheet. It may be used for recording test scores and student class attendance. A commonly used free (but probably userdata-mined) resource is available as part of the GoogleDocs suite, namely Google Sheets. Here we will use that to show the accessibility of powerful programming tools available to teachers to make their work more efficient.

We start by creating a blank spreadsheet, and then we attach a Google Apps script project to this spreadsheet by clicking on Tools > Script Editor.

 

A new window will open and this appears



Here, you would notice the cursor inside myFunction(). This is where we can define what we want to do with our code, which is similar to a recipe in a cookbook. For the mathematically inclined, this is exactly the same notation we use when writing a typical math function, e.g. f(x) = 2x. The only thing different is that for myFunction(), we allow for zero argument functions, arguments being variables inside the brackets.

Similar to math functions, myFunction() can also accept two arguments, e.g. f(x, y) = x + y. You can try this by replacing myFunction() with this:

myFunction(x, y) {
    return x + y;
}



and clicking then remember to click the Save button (shown inside the red box). Once you saved your code, go back to the Google Sheets page, put in some values in some cells, and try the function out.



Now you can create your own functions and link them to Google Sheets. You are no longer bounded by limited space available in Google Sheets where you can only effectively write single line functions.

Continuing from here we visit the idea of variable types which will be influenced by what you use as arguments and the results of your own custom defined function.

z = f(x,y)

Mathematically, it is clear that all x, y and z are numerical. However, a computing function can also accept string values as arguments and return string values.

What are strings? A string is a sequence of characters, e.g. a b c d e... The character "a" for example is represented in a computer by the number 97 in decimal and 01100001 in binary. The character "1" is represented by the number 49 in decimal and 00110001 in binary. Here's a website for you to try to play with conversion.

This is necessary because a computer can only understand numbers as it is fundamentally built with on-off switches or zeroes and ones. So please remember the number 1 and the character "1" are different for a computer.

Let's talk about the addition operator +. For numbers, it is obvious it with result in the sum of two numbers. For strings, they are typically defined to connect two different strings together, or concatenation. Note that typically we represent strings in a computer program as characters encased between double quotes.

myFunction() {
  var first = "Hello";
  var second = "world";
  var result = first + second;
  return result;
}

If you define a function with the above, and try it on your Google Sheet document, you will get the output "Helloworld". How would you add a space in between the two words to make "Hello world"?

Another thing you might want to try is to add "1" and "2". You will see that the answer is not "3" but "12".

You may try to add a character and a number but you will run into problems. It is because such operations are not defined.

There is a very useful construct in logic called the if-else statement. It provides a way of branching your logic into different paths when certain conditions hold true (or false). In computer programs not unlike in mathematics, we also use it to provide different algorithms (or recipes) when some conditions are met. For example:

if(x is less than 0) {
  y = 1;
} else {
  y = 0;
}

which is obvious to you what it does. We try to write in a syntactically correct way below:

myFunction(x) {
  var y = -1;
  if(x < 0) {
    y = 1;
  } else {
    y = 0;
  }
  return y;
}

Note that we need to declare the variable y in the beginning. We need to do this for all variables we use so that the compiler (a separate program to translate human readable code to binary) can check our program for typos. Another way of writing this would be:

myFunction(x) {
  var y = 0;
  if(x < 0) {
    y = 1;
  }
  return y;
}

This program looks shorter now but it does the same thing. It is usually better to write shorter programs whenever possible but not at the expense of readability.

No comments: