These notes are intended to be used AFTER the “myFirstFunction Typescript” assignment.
A programming language, like any language, consists of a vocabulary, syntax, and a set of grammar rules for instructing a computer to perform tasks. Unlike a human language, in a programming language these all need to be unambiguous, consistent, and predictable; computers don’t understand emotion or sarcasm!4.3.3
Computers don’t speak the programming languages directly; computer processors only understand a very small number of very specific instructions and all programming languages must be translated to that machine language4.3.5.
Programming languagues come in LOTS of different shapes and sizes, with different purposes and capabilities.
Multi-line comments start with a /*
symbol and end with a */
symbol. Anything between those symbols will be ignored by the program and are only for humans, to add readability.
A single-line comment begins with a //
symbol and lasts to the end of the line.
A variable is a named place in memory that can hold a piece of data.4.3.6 In TypeScript (and java), variables are statically typed which means a particular variable name can only be associated with one type of data - for example a number, string (word, usually written in quotes), or boolean (true/false) value. In javascript (and other languages, like Python) variables are dynamically typed and can be associated with any type of data.
A function is a named “section” of code that can be run as many times as you want. A function takes one or more inputs (also known as parameters) inside of parentheses and sends back one output (also known as the return value). For example, the function below will add two numbers and return the result as a number. Sometimes we think of functions as “sub-programmes” - little programs that run inside the main one and make it easier to understand the code. They are also called methods.4.3.12
The example below is a function in TypeScript
1
2
3
4
5
function addTwoNumbers(num1:number, num2:number):number {
return num1 + num2;
}
let s:number = addTwoNumbers(2, 3); // The variable s now has the value 5
The java equivalent (which will only work with integers, not any number) would look like:
1
2
3
4
public int addTwoNumbers(int num1, int num2) {
return num1 + num2;
}
public int s = this.addTwoNumbers(2, 3); // The variable s now has the value 5
While the javascript version would look a lot like the TypeScript one, but with no mention of numbers (so it would even work for words!)
1
2
3
4
5
function addTwoValues(val1, val2) {
return val1 + val2;
}
let s = addTwoValues(2,3); // the variable s now has the value 5
let j = addTwoValues("Bob","Smith"); // the variable j now has the value "BobSmith"
Try to answer these in your head WITHOUT looking back, then check your answers. This is called recall practice and has been shown in many, many studies to be a far more efficient way of learning than if you skip the attempt to recall. Answers for each one can be found by hovering over the blank area below them.
What are the features of a programming language?
A fixed vocabulary, syntax, and set of grammar rules.
What does it mean to compile a program?
To convert the code into another language, such as machine language or javascript
What does it mean that TypeScript and java have static variable types?
Each variable can only hold one type of data, decided when the variable is made.
What is the output value of a function called in programmer speak?
the return value
Can you write a function in TypeScript or java that accepts two numbers (integers) as input variable, and returns the difference between them?
1
2
3
function diff(a:number, b:number) {
return a-b;
}
Create flash cards or a
We have a quizlet class at https://quizlet.com/join/S8XGx3k93 and you can add your decks there if you make them, but you REALLY should make your own deck before using those of others. You learn as much from making the flashcards as using them, if not more.