Intro to Programming

The main concepts, ideas, and practices of coding

Introduction

As we write essays on our laptops and snap friends with our smartphones, it's easy to forget how complicated these devices really are. A modern smartphone has more billions of transistors, along with light sensors, infrared cameras, gyroscopes, accelerometers, microphones, antennas, and tons of other technological components that let it do all the things we can use smartphones for.

How are we able to operate these complicated machines without needing an in-depth understanding of electrical engineering, abstract mathematics, digital signal processing, and all the other complexities involved?

Simple: programming!

Thanks to engineers and software developers applying different kinds of programming at varying levels of complexity, we are able to use polished, human-friendly interfaces to control unbelievably sophisticated systems, without having to worry about how it is all accomplished.

But what exactly is programming? And what do you need to know if you want to learn to program/code?

This manual will explain what programming is, explore some concepts that are common to most programming languages, and give real-world examples of how programming and coding are used today.

What Does "Programming" Even Mean?

It's easy to think of programming as an arcane, hard-to-understand skill that only highly trained people can perform. But programming just means writing step-by-step instructions for how to accomplish a task, using language that a computer can understand.

Even though it can feel extremely complicated, when you break it down, programming really consists of just three simple actions, combined in different ways:

  1. Getting information from somewhere
  2. Doing something to the information (aka Processing it)
  3. Putting information somewhere

There are countless ways to combine and execute them, but fundamentally, all programming is just different combinations of those three steps.

These steps are written out in special code that a computer can interpret, and the finished product is called a computer program.

Computer Program

A computer program is any collection of code that has been written to instruct a computer how to do something. Computer programs go by many names, including scripts, applications/apps, algorithms, and more.

Anything from a simple 1-line script to a professional application containing millions of lines of code can be considered a program.

Computer programs have data and behavior.

Data is the information the program has access to while running.

Behavior is what the program does with the data it has access to.


Getting Information

For a computer program to do anything useful, it usually requires some amount of data, or information to work with.

This information can come from a variety of places:

The actual information can take many forms:

The only requirement is that it is in (or can be converted into) a format the computer can understand: 1s and 0s.

Processing Information

Manipulating or processing data makes up the majority of the behavior of most computer programs. Writing the instructions for this behavior is often what people have in mind when they thing of 'computer programming'.

Computers can perform data-related operations millions or billions of times faster than a human, with extreme precision, which makes them great at repetitive or calculation-intensive tasks like counting/math, quickly finding specific items in large amounts of data, etc.

All of these tasks involve the computer looking at information and doing something to it according to the instructions given to it by the programmer.

Fast, accurate processing of information is what makes computers useful.

Putting Information Somewhere

Once we've given the computer some information and done something to it, it's not especially useful until we get it out of the computer memory and put it somewhere else.

As with getting the information, there are a lot of places for a computers to output information it has processed:


That's it!

Everything we do with computers is the result of programmers combining those three steps - getting, processing, and outputting information - in different ways.

Computers, Data, and Abstraction

When learning how to program, it can be helpful to have a little background knowledge of how computers work. This section will explain what information is and how computers process it, but it's not necessary to know any of these details to be a successful programmer.

The following section offers background technical knowledge if you are curious about the bigger picture of programming, but this information is NOT a prerequisite to learn how to code.

It is not necessary to know the technical details of how computers work if you want to be a programmer.

They can be helpful if you get into particularly complex or high-performance programming, but you can absolutely succeed as a coder without knowing what a transistor is or how binary code works.

What is a computer, and what does it compute?

At its most basic, a computer is a machine that can process information using logic.

In our everyday usage of the term, information can mean a lot of things. In this context, information is that which can be communicated about the state of something. The most basic building block of information is a binary state, or choice between two things: yes-no, true-false, on-off.

As we use it, logic is the process of using simple comparisons to evaluate how pieces of information relate to each other, specifically, using true-false comparisons to determine the output from a given set of inputs.

By using basic logical comparisons to evaluate simple pieces of information, you can quickly start to represent much more complicated information.

In a computer, these logical comparisons are achieved with circuits. Circuits are electrical systems - tiny wires and switches - that allow electricity to flow along a given path. The electricity's path changes based on different conditions (like whether a given switch is on or off), and that path allows for those simple logical comparisons to be made, which can be used to represent and process more complicated information.

A computer processor is basically just lots and lots of circuits working together in specific ways, extremely quickly. The information they process comes in the form of bits.

Bits

The fundamental unit of information is called a bit, which is short for binary digit.

Bit

A bit, which is short for binary digit, is a unit of information that can be in one of two states (on/off, true/false, 0/1, etc.).

It is the fundamental building block of computing and is the basis of binary code, which is how computers read and interact with information.

Any object that can be in two different states can be used to represent this unit of information:

People have even created functional computing devices inside the game Minecraft using an in-game substance called redstone, which can be turned on or off by changing certain items in the game.

In computers, this unit of information is represented using electrical devices called transistors. A transistor is a small electronic on-off switch that turns on or off depending on how much electricity is flowing through it.

This on/off state of the transistor represent a bit, and by setting up banks of multiple bits in a row, you can represent larger numbers using binary code.

Binary Code

Because the transistors in computers can only be in one of two states (on or off) they need to use a counting system that is base two to represent numbers larger than 1. This base-two system is also known as binary code.

Base-Ten vs. Base-Two (Binary)

The system of counting that we use in our everyday lives is called base ten, because it uses 10 possible digits: 0 through 9. It has enough different values to count up to 9 (which is ten total values, because it starts at zero), but if we want to represent anything higher than 9, we add a new column to the left to keep track of how many 10s we have, and start back at 0 in the first column.

As we count up, each time we get to 9 and need to add 1 more, we increase the value in that second column by 1. If we get to 9 in that second column and 9 in the first column and need to add 1 more, we need to add a third column, to show how many hundreds we have, and start the whole process again. Once we get ten 10s in the hundreds column, we add a new column for how many thousands we have and start again.

If you are familiar with the math concept of exponents and raising numbers to a power, each new column is 10 raised to the next highest power e.g. the tens place is 102 (10x10), the hundreds place is 103 (10x10x10), etc.

When we write a number like 32,706 each digit is showing how many we have of each of these multiples of 10:

32706
Place:10,0001000100101
How many 10s:10x10x10x10x1
(104)
10x10x10x1
(103)
10x10x1
(102)
10x1
(101)
0
(100)
Value:30,0002,00070006

30,000 +
2,000 +
700 +
00 +
6 =
32,706

When we are using base-two, it's the exact same process, but there are only 2 possible values - 0 and 1 - so each column represents a multiple of 2 rather than a multiple of 10. Instead of starting over when we get to 9, we start over when we get to 1.

To represent the number 53 in binary code, which is base two, we would write 110101

110101
Place:32168421
How many 2s:2x2x2x2x2
(25)
2x2x2
(24)
2x2x2
(23)
2x2
(22)
2
(21)
0
(20)
Value:32160401

32 +
16 +
0 +
4 +
0 +
1 =
53

Because you only need 2 values for each digit's place, you can use a simple on/off switch like a transistor to represent each bit and store its place value.

Use the buttons on the counter below to see the binary representation of the decimal (base ten) numbers from 0 to 32.

Notice how the base-ten numbers can all be represented by adding up the values of each individual binary digit (0 or 1), which are in powers of 2.

0 000000

0
000000
000000

As you increase the number of bits, you increase the size of the number that can be represented. The maximum value that can be represented by a binary number is 2 to the power of how many digits long it is. For example, the maximum value of a binary number with 8 bits (aka 8 digits long) is 28, or 256. Each additional digit of length doubles the maximum value that can be represented by that number. A 9 digit (or 9-bit) number can represent 512, a 10-bit number can represent 1024, and so on.

How Binary Code Becomes Data

If computers only really understand 1s and 0s, how are we able to store and process human-friendly data like text, images, and video?

Bytes

8 bits stacked together are called a byte, which is the basic unit of data storage and processing for modern computers.

Byte

A byte is the fundamental unit of computer storage and information processing. Bytes (usually) consist of 8 bits, and form the smallest unit that can be stored in computer memory.

Computer memory and storage space are measured in bytes, usually with a prefix indicating a multiple of ten (kilobyte = approximately one thousand bytes, gigabyte = 1 billion bytes, etc.)

Bytes are saved in a computer's memory by turning individual transistors (in groups of 8) on or off corresponding to the 0 or 1 value of each respective bit. Even the simplest modern computers can contain millions and millions of these transistors.

Depending on the context, a given byte can represent different things.

To represent text, there is a system known as ASCII (pronounced ACE-key) that standardizes which letters and numbers are represented by which bytes. Each numerical value stored as an 8-bit byte corresponds to a letter of the alphabet, a number, or a symbol. The byte stored by the 8-bit number 01000010 (which is the number 66 in base-ten) is a capital B, while the byte 01001100 (76) stands for capital L, and 01001101 (77) stands for capital M.

The 3-letter phrase BLM stored as ASCII text in computer memory would look like the following:
01000010 01001100 01001101

Other kinds of data, for example images or video, use bytes in a different way. Digital images are made up of thousands of individual dots known as picture elements or pixels. The color of each individual pixel is stored as 3 bytes, representing the red, green, and blue values for that pixel (see sidebar).

In a pixel, that same set of bytes that represented the letters BLM:
01000010 01001100 01001101
would represent a dark grey color:

By stacking up many, many (many!) bytes of a given type, computers can store and process massive amounts of information, which we call data. We can then program computers to manipulate this data in all sorts of ways.

Abstraction

Dealing with individual bits and bytes themselves can be extremely complicated, and doing so successfully requires intimate knowledge of the electrical machines and circuits that computers are built on. Fortunately, most programmers never have to directly touch the bits and bytes that represent the data we work with, thanks to a concept called abstraction.

Abstraction

Abstraction is the process of providing a way to accomplish a task, which does not require knowledge of how the task is achieved.

It allows users to employ potentially complicated, multi-step technical processes without having to know the implementation details of how the task is accomplished.

This is where the more complicated parts of a process (which are sometimes called implementation details) are hidden away or abstracted behind a more simplified interface, which allows them to be accessed in a more user-friendly way.

A real-world example of abstraction is the steering wheel of a car.

When a driver turns the steering wheel of a car, they are kicking off a complicated series of mechanical processes. The steering wheel is attached to the steering column, which manipulates the car's axles, which uses a differential to cause the car's tires to be able to rotate at different speeds in a precise relationship, which ultimately results in the car moving in the intended direction.

The driver doesn't need to know anything about these implementation details to successfully drive the car; they just need to learn the behavior of the steering wheel.

Implementation Details: An exploded diagram of a steering column showing many, many complicated parts
Abstraction: A simplified vector drawing of a steering wheel showing the two options for turning the wheel left or right

The engineers who built the car required complex technical knowledge to solve the mechanical challenge of making the car turn. By applying that knowledge, they were able to create a much simpler, abstracted interface that everyday drivers could operate without issue.

This same process also applies when working with the electrical machines we know as computers.

Virtually all programming relies on one or more layers of abstraction to control the computer. Unless they are manually changing the transistors and re-routing circuits by hand, programmers are using abstraction to get the computer to compute.

Abstraction and Programming

Let's say we have a simple program in Python that prompts the user to enter their name, then outputs how many letters long their name is.

The code itself would look something like this:

print('What is your name?')
name = input()
nameLength = len(name)
print(nameLength)

This simple-seeming task actually involves many technical processes shown in the following list.

Hover over this list to see which steps the programmer actually needs to write, and which steps have been done for them via abstraction:

The simplicity we enjoy is possible because so much of the complexity has been abstracted away from us as programmers.

Thousands of person-hours have been spent researching, implementing, and troubleshooting all of the electronic systems and digital components involved in these processes. The people who have performed that labor poured their effort into creating simplified, abstracted interfaces that we as programmers can use to accomplish tasks and efficiently solve problems.

One practical example of the abstraction process letting us harness the complexity of computers and use them in accessible, understandable ways can be seen in the form of programming languages.

Programming Languages

Programs are sets of instructions that a computer can follow to perform a given task. These instructions are provided in one or more programming languages.

Programming Languages

A programming language is a medium that instructions can be written in that a computer can interpret to execute a given task.

Like human languages, most programming languages have certain structures and concepts in common but each has its own syntax and vocabulary.

Different languages are used for different things. Python, Java, and C++ are good general-purpose languages and can be used for a wide range of tasks. JavaScript is mainly used to give interactivity to web pages. Some languages have very specific functions: R is built for statistical analysis, and MATLAB is meant for matrix-based math.

Low-level vs. High-level

A programming language may be referred to as a low-level or high-level language. This refers to how much abstraction there is between the code you write and what it's telling the computer hardware to do.

A low-level language like C++ has fewer layers of abstraction between the code and hardware, which means it can be used for more performance-intensive tasks because less of the computer's processing power is needed to interpret and execute the code. This is why high-performance programs like Adobe Photoshop and many video games are written in C++.

A higher-level language like JavaScript has more layers of abstraction. It is not as good at maximizing performance, but in exchange, many technical tasks are handled automatically (like managing program memory, clearing out old variables, etc.) so the programmer doesn't need to worry about them.

Popular Languages and Their Uses

Name Used for Pros Cons Who should learn How hard to learn
C/C++ General programming, especially good for applications where it's important to get the most out of hardware, such as media applications and video games Very efficient, can be compiled and run on many platforms Moderately complex, requires programmers to manage many things themselves like memory allocation, interacting with hardware devices, etc. People doing performance-intensive coding, systems programmers ★★★★☆
Java One of the most widely used programming languages today. It is used for everything: servers, medical equipment, Android apps, games, Blu-Ray players, smart-home accessories, etc. All-purpose lanaguage, "write once, run anywhere" philosophy. The syntax can be unnecessarily lengthy, and there is a lot of conflicting information about best practices. CS majors, Android developers, people who want to learn all aspects of programming. ★★★☆☆
JavaScript Providing interactivity and functionality on the web. Making web pages interactive, creating many of the elements we associate with the internet today. Reasonably straightforward to learn, and you can see results right away. Good for newer programmers. Some of the syntax rules can be inconsistent. Not always great for performance. Need to know HTML and CSS to get the most out of it. Requires a web browser (usually). Anyone interested in learning to code. ★★★☆☆
Python One of the most popular general-purpose programming languages in recent times. Widely used in artificial intelligence, machine learning, data analysis, and many other data-related tasks. Syntax is easier to read than many other languages. Has a huge number of libraries and code snippets that make life easier. Very approachable for people with no previous coding experience. Because it's so popular, it can be hard to know where to start, or where to find answers to specific questions. People learning to code, machine learning/AI coders, data scientists ★★☆☆☆
HTML Creating and structuring content for web pages. Simple syntax, extremely practical skill to have. Nearly all rich-text editors (e.g. Canvas, WordPress, etc) offer a way to directly edit HTML, which can be helpful when formatting content online Not a 'traditional' programming language, so it lacks things like variables, functions, loops,conditionals, etc. People interested in making websites, or programmers who will be making apps used on the web. ★☆☆☆☆
CSS Applying visual style to HTML content. Flexible, rich set of tools for styling text, images, and providing limited interactivity Not a 'traditional' programming language. Can be tricky to understand how style rules get applied. Designers who want to learn relevant coding skills, people interested in making websites, or programmers who will be making apps used on the web. ★★☆☆☆
PHP Creating web-based apps and dynamic web sites. Extremely popular choice for building dynamic web sites, pretty straightforward to get started with, can be integrated into existing web pages without much fuss, good for working with MySQL databases Syntax can be unintuitive, some confusing elements in the built-in functions, People looking to get immediate results, built dynamic websites, work with databases, or learn to develop tools for platforms like WordPress and Drupal ★★★☆☆

Learning a Programming Language

Becoming proficient in a programming language is similar to learning a spoken language:

  1. Learn the syntax
  2. Build a vocabulary
  3. Become familiar with the patterns, idioms, and style of the language

Syntax

The way the different parts of a programming language go together is called the language's syntax.

Correct syntax is essential with programming. Unlike with spoken languages, where most of the time you can still communicate even if things are phrased incorrectly, computer programs will not function with incorrect syntax.

Most programming languages are made up of the same components: variables, functions, loops, etc. The specific way you assign a variable or declare a function varies from language to language according to its syntax. The first step in learning a programming language is to become familiar with how these fundamentals are written.

Vocabulary

Once you know how to write statements correctly, the next step is to build a vocabulary. This usually starts with learning the functions and operations most commonly used in that language.

For example, in JavaScript, programmers frequently need to identify a particular HTML element by its ID attribute and save it to a variable.

A very common way this is done is by using the function document.getElementById() like so:

var myElement = document.getElementById("article-body");

The first few times you encounter this construction, it will most likely take some effort to step through each piece and figure out what is going on.

As you become more familiar with the language, you'll be able to look at this statement and understand at a glance what it's doing, rather than having to stop and puzzle through each piece.

As your vocabulary grows and more phrases become second nature, you'll require less and less active mental energy to read and write code. You won't need to consciously translate your intent (e.g. "I want to save a variable so I can use it later - what function do I need to use for that again?") and instead you will be able to express your intentions fluidly in code on the fly.

Patterns, Idioms, and Style

Once you have decent working vocabulary in a language, you may start to notice different constructions and conventions pop up.

For example, in JavaScript there is a very common design pattern called a callback function, where an argument passed to one function is an entire function in itself:

document.getElementById("article-body").addEventListener("click", function() {
    document.getElementById("demo").innerHTML = "Hello World";
  });

There are other ways to accomplish this same task, but knowing that the preferred way of handling a function-within-a-function is to use one of these callbacks, you can write concise, understandable code structures that are more efficient and easier for other JavaScript programmers to make sense of.

Every language has its own distinct patterns and conventions. The more familiar you become with a language, the better you are able to use it in a more native, expressive way.

Code Anatomy: Data

Every programming language is different, but there are some concepts and ideas that are common to most languages we encounter. This section will explore some of the most common and relevant elements of programming.


Variables

Variables are containers that store data so that it can be used or processed somewhere else in the program.

A variable has two parts:

Variables

Variables are virtual containers that are used to hold data so that it can be used elsewhere in a program.

They have a name (or identifier), and contain a value.

Data is almost always assigned to a variable using the assignment operator, which is a single equals sign: =

Examples:

x = 23
            var myName = "Bucky"
        

Most of the time data is assigned to a variable using the equals sign (=), which is sometimes called the assignment operator.

Different languages have different rules around how variables are named and assigned, but the process usually looks like one of the following:

Python:

x = 3

PHP:

$my_name = "Bucky";

Java:

int myAge = 20;

JavaScript:

var yourAge = 21;

Each of these has the identifier on the left and the value on the right. Some languages use a keyword like var or a data type designator (e.g. int, String, etc.) when assigning a variable a value. Some languages require variable names to start with symbol like $.

The specific syntax will change from language to language, but nearly all languages assign variables using =

Data Types

An important concept to understand in programming is the idea of a data type.

Computers have instructions built into them that tell them how to manipulate data, but they are very particular about the kind of data they operate on. Trying to perform an operation on the wrong kind of data could cause the program to work incorrectly or not at all, or could even cause other parts of the system to malfunction.

Just like how you can pour a liquid (e.g. milk), but you can't pour a solid (e.g. a piece of pizza), some computing operations can only be performed on certain types.

A computer can multiply two numbers (with data type integer) together, but if we asked it to multiply two words (which have the data type of String), it would crash because it doesn't know how to handle that data type for that operation.

Data Types

A data type indicates the kind of information that is being referenced/stored.

Common data types include integers (numbers with no decimals), strings (text), floats (numbers with decimals), Boolean (true/false), and arrays or lists (containers with multiple items).

Some programming languages require the programmer to explicitly indicate the type of any data they are storing or accessing. Other languages will try to infer it based on the data being stored/accessed.

Some languages like C++ and Java require programmers to specify the data type for every variable they assign, which is called static typing.

This is an example of assigning variables using Java (a static-typed language), where we declare a numeric variable of type integer or int and a text variable of type String:

int myAge = 20;
String myName = "Jamie";

Other languages like Python and JavaScript try to infer the data type of a variable at the time the program is run, based on the value stored in it. This is called dynamic typing.

Here is an example of assigning int and string variables in the dynamic-typed language Python:

myAge = 20
myName = "Jamie"

Notice how the variables do not need a type designation at the start.

Common Data Types

The following are some of the most common types of data you are likely to encounter as a programmer:

TypeDescriptionExamples
StringA string of letters, numbers, and/or symbols. These are usually written in quote marks 'like this' or "like this""Jamie"
"1234"
'Wait 3-5 minutes'
IntegerWhole numbers without a decimal component, up to approximately 4 billion (232) values.7
1024
LongAn integer that can represent numbers over 4 billion.7
6,4096,1024,2048
Float (or floating-point)Numbers that have a decimal point component2.5
117.0
3.1415
Double (or double-precision float)A more precise version of a float 2.5
117.0
3.1415
BooleanA special data type that can only store one value: true or false.true, false
Character (or char)A single letter, number, or symbol character."g"
"Y"
Array (also called a List)A special data type that can store multiple values, in a specific order. Often written using square brackets [ ], with the values separated by commas. The individual items are often accessed using the index, which is their position in the list, e.g. myData[3] ["apple","banana","cherry"]
Dictionary (also called an Associative Array)Like an array, but each item is stored as a key-value pair. Similar to an array, but order does not matter, and values are accessed using their key instead of their position in the list, e.g. myData["name"]["name": "Bucky",
"age": 20,
"city":"Madison, WI"]

Code Anatomy: Behavior

Operators

Most programming languages have a set of built-in functions that can be used to manipulate or evaluate data in basic ways. These include things like performing math operations, making logic comparisons like AND and OR, or incrementing numbers.

Mathematical Operators

Most languages use specific characters for performing math operations on data. The following are commonly used math operators across languages

SymbolOperationExample
+ Addition 2 + 3
- Subtraction 3 - 1
* Multiplication 4 * 8
/ Division 8 / 2
^ Exponent 2^5
** Exponent (Python only) 2**5

These operations can be used on raw numbers:

x = 3 + 2

They can also be used on variables:

x = 3
y = 2

z = x * y 
            

Logical and Comparison Operators

Comparing different statements or expressions is another extremely common operation in programming languages, and most languages have dedicated operators for making these comparisons.

Some of them compare quantities (less than/greater than, equals, etc.), while others do logic comparisons, for things like AND and OR.

The following table shows common comparisons and their respective operators in different languages:

Operation Definition Operator Examples
Equals Evaluates whether two items (numbers, strings, booleans, arrays) are equivalent. Returns the value TRUE if so, or FALSE if not.
This is often used for IF statements and other program control flow.
==
(Note the TWO equals signs - not one!)
3 == 2+1
(True)

3 == 7
(False)
Is Not Equal To Evaluates whether two items (numbers, strings, booleans, arrays) are NOT equivalent. Returns the value TRUE if so, or FALSE if not.
This is often used for IF statements and other program control flow.
!= 3 != 1
(True)

3 != 2+1
(False)
Greater Than Evaluates whether the item on the left is greater than the item on the right and returns TRUE if so, FALSE if not. > 7 > 3
(true)

2 > 5
(false)
Less Than Evaluates whether the item on the left is less than the item on the right and returns TRUE if so, FALSE if not. < 3 < 7
(true)

5 < 2
(false)
AND Used in logic comparisons to evaluate whether multiple statements are true/false. Returns TRUE if all statements are true, otherwise returns false. Java: &&
JavaScript: &&
C/C++: &&
Python: and
13 < 17
&&
pizza == "food"
(true)
OR Used in logic comparisons to evaluate whether any statement is true, when given multiple statements. Returns TRUE if any statement is true, otherwise returns false if all are false. Java: ||
JavaScript: ||
C/C++: ||
Python: or
13 > 17
||
"pizza" == "food"
(true)
Logical NOT Inverts the truth of the statement it precedes (e.g. !true is equal to false).
This is often used when evaluating the result from a function that returns a true/false response.
! !("pizza" == "food")
(false)

= and == are different!

= (a single equals sign) is the assignment operator, used to assign values to variables.

== (two equals signs) is the equality comparison operator, used to see whether two items are equal.

They are not the same thing!

This is one of most common and easiest mistakes for programmers to make. Even seasoned professionals will accidentally mix these up from time to time.

Specialty Operators

Some operations are (relatively) common in programming languages but not so much in non-programming contexts. It's helpful to know the symbols for these operations as you learn to program, even if you may not need to use them frequently.

Symbol Operation Definition Example
++ Increment (add 1) Increases an integer or variable by 1. Frequently used in loops and when counting iterations of a task. For variable myNumber:
myNumber++
-- Decrement (subtract 1) Decreases an integer or variable by 1. Frequently when repeating a task a set number of times. For variable myNumber:
myNumber--
% Modulo (sometimes called modulus or remainder) Returns the whole-number remainder of division between two integers 15 % 6
returns 3
(15 ÷ 6 = 2, remainder 3)

Functions

Functions are one of the foundational concepts in programming. Nearly every language has some concept of functions, though the syntax varies from language to language.

Functions

A function is a block of instructions that can be reused in a program without needing to be rewritten every time.

The programmer first declares the function, which means giving the function a name and writing the code that it will execute.

Once it has been declared, the programmer can then call the function elsewhere in the program.

Functions can take input, called arguments or parameters, and give output, called return values.

A function can take one or more arguments, or input. These are a way to get data from outside the function, into the function so it can be processed. They are also sometimes called parameters.

Functions can also have a return value, or output. This is data the function spits out when the program calls it.

Not every function needs a return value, but it is generally good practice to have a function return when it is done running its code.

For most programming languages, functions are called by using the function name followed by parentheses, e.g. myFunction() which contain any arguments the function may take, or are blank if no arguments are needed.

An example of a function (with no arguments) being declared and called in JavaScript:

function myFunction() {
    console.log("I am a function!");
    return;
}

myFunction(); // Prints out 'I am a function!' to the browser console.
        

An example of a function (with an argument and a return value) being declared and called in Python:

def myFunction(myName):
    outputText = print("My name is" + myName)
    return outputText 
    
myText = myFunction("Bucky") # Assigns the return value of the function to the variable myText
print(myText) # Prints out "My name is Bucky" to the screen

Examples

Let's say we are writing a program and want to take a person's name as input and spit out a line of text that says, "Hello, [person's name]!". We'll be outputting this line many places in our program, so it would be helpful to write a function that we can reuse and save ourselves time.

To create the function we need to do the following:

  1. Give the function a name (or declare it) so we can call it later.
  2. Make sure it can accept input data (an argument).
  3. Write the behavior of the function (aka what it does: assemble the text, formatted the way we want it)
  4. Output (or return) our line of text

This is how you might write that function in JavaScript:

var sayHello = function(myName) {
    var outputName = myName; // Create a variable and set it to be the value from the argument
    var outputText = "Hello, " + outputName + "!"; // Create the string of text to return.
    return outputText; // Output the variable as a return value
}
    
var ourText = sayHello("Bucky"); 
// Creates a variable named 'ourText', and assigns it a value
// The value being assigned is the output of the function - in this case, 'Hello, Bucky!'

console.log(ourText); // Output 'Hello, Bucky!' to the browser console.
        

In Python, the code might look like this:

def sayHello(myName):
    outputName = myName # Create a variable and set the function's argument as its value
    outputText = "Hello, " + outputName + "!" # Create the string of text to output
    return outputText # Output the variable as a return value

ourText = sayHello("Bucky") # Create a variable, 'ourText', and assign the function output to it

print(ourText) # Output the contents of the 'text' variable to the command prompt.

Code Anatomy: Syntax and Program Structure

Expressions and Statements

Just as words need to be combined into sentences to fully convey meaning, Variables, Data types, and Operators are not particularly useful until they are combined into statements or expressions. These are units of code that convey an instruction to the computer.

They are subtly different (specifically, an expression has a value associated with it, while a statement does not), but for the purposes of this manual, they can be used interchangeably.

Statements and Expressions

Statements and expressions are short blocks of code that produce some behavior in a program and usually involve variables and/or operators.

They are slightly different (specifically, a statement is a unit of code that produces behavior, an expression has a value associated with it), but most of the time the terms can be used interchangeably.

Most of the code that programmers write is a series of statements or expressions.

Ending a Statement

Different languages end statements in different ways.

Languages like JavaScript and Java whose syntax is based on the language C usually use a semicolon to end their statements. This means a single statement or expression can extend over multiple lines, and only ends when the interpreter encounters the semicolon character.

In the example below, the statement (which is assigning 3 variables in a single statement), is split over multiple lines.

var myName = 'Bucky',
    myAge = 20,
    currentCourse = 'CS 400';

Python uses the line-endings to indicate the end of a statement. That means statements can't extend onto multiple lines.

myName = "Bucky"
myAge = 20

Blocks

Most programming languages have some concept of a block, or a section of code that is treated as a unit. Things like functions and IF statements (which we will cover later) use blocks to differentiate their code from the rest of the program.

Languages based on C, like Java and JavaScript, use curly braces { } to indicate a block.

An example of an IF block in JavaScript:

if (x == 3) {
    x = x + 1; 
    console.log("X now equals 4!");
}

Python is different, in that it uses indentation to indicate blocks:

The same block as written in Python:

if x == 3:
    x = x + 1
    print("X now equals 4")
    

Whitespace

Spaces and linebreaks in code are referred to as whitespace.

Some languages like Python care about how the whitespace in statements is formatted and structured, while others like Java and JavaScript do not.

Consider the following examples that use JavaScript. These three bits of code will produce identical results even though the formatting is dramatically different:

if (x == 3) {
    x = x + 1; 
    console.log("X now equals 4!");
}
if 
(x == 3) 
{
x = 
x + 1; 
console
.log("X now equals 4!");
}
if(x==3){x=x+1;console.log("X now equals 4!");}

The spacing is there to improve readability for the humans working on the code, but as far as the computer is concerned, it does not matter.

Python, on the other hand, cares a lot about spacing and indentation.

Python sees the end of a line as the end of a statement, and it uses indentation to recognize where a block starts and stops.

The following bit of code will produce an error if you try to run it because the IF statement block is formatted incorrectly:

if x == 3:
x = x + 1
print("X now equals 4")

The correct formatting is as follows (note the indentation):

if x == 3:
    x = x + 1
    print("X now equals 4")

Many people consider Python easier to read because it requires programmers to use a certain level of formatting consistency.

Comments

Nearly all programming languages have a way to add comments to a program.

Comments are bits of text that are ignored by the computer but help humans who are reading the code understand what it the program is doing.

Different languages have different ways to indicate comments.

Python uses the # character:

myName = 'Bucky' 
# Comments are ignored by the computer.

JavaScript, Java, C++, and many others use // to create a comment.

var myName = 'Bucky';
// Comments are ignored by the computer

It is good practice to add comments to code that you are writing, to make it clear why that part of the program is coded the way it is.

Comments are essential if you are working with other people on the same code base, but they are also important when working on projects by yourself. Every programmer has had the experience of writing code, then coming back to it a few weeks later and thinking "What does this do again? Why did I write it this way?" and having to re-discover what their own code is doing.

Comments save time and help you stay organized, even when it's just you working on a project yourself.

Objects

A concept that is extremely common in modern programming is something called an object.

An object is a structure in programming that has its own self-contained data and behavior - its own variables and functions. Objects are extremely helpful for keeping code organized and limiting the complexity of a program by abstracting away details that the programmer doesn't need to know about in order to use the code.

Variables and functions have special names when they belong to objects. A variable that belongs to an object is a property, and a function that belongs to an object is a method.

Dot Syntax

In JavaScript, Python and many other languages, properties and methods are referenced by using the object's name, followed by a dot and then the name of the property or method. This is known as dot syntax.

They can be referenced different ways in different languages, but dot syntax is what you are most likely to encounter.

The following examples use an imaginary object, Bucky (which is an instance of the class Badger), that has the property name and the method sayHello()

Bucky = new Badger;
console.log(Bucky.name); // Outputs the property 'name'.
Bucky.sayHello(); // Calls the method/function sayHello() and executes its code

You will frequently run into Objects when using different libraries, or pre-written packages of code that can be incorporated into your own projects.

For example, there is a Math object in JavaScript that allows us to use common math functions like rounding and max/min and constants like pi without having to write them ourselves.

var p = Math.PI; // Assigns the variable p the value of pi
var x = Math.round(3.7); // Assigns 4 to the variable x
var y = Math.sqrt(16); // Assigns the square root of 16 (4) to variable y

Classes

Every object is an instance of a class. A class is basically a blueprint that specifies the properties and methods object built constructed from it will have.

Programmers only need to write the definition for the class once, and then every time they create an object based on that class, the object will automatically inherit all of that code for free.

As you get started learning how to program, you will probably not immediately need to know how to write classes of your own, but it is helpful to have an idea of what they are and to know how an object is made or instantiated from one.

Instantiating Objects

Most of the time an object is brought into the program by assigning it to a variable using the new keyword, and referencing a class name:

Bucky = new Badger;

This is saying "create an instance of an object called Bucky, and build it according to the blueprint laid out in the class called Badger."

You will see this construction frequently when using libraries written by other programmers.

Code Anatomy: Control Flow

This section will look at different ways to direct the code towards the outcomes we want, using two extremely common programming concepts: conditionals and loops


One of the most useful things we can do with programming is have different code execute based on different conditions.

The most common way to trigger different code paths is using an if statement.

IF statements

Say we are writing a weather app and want to customize the message that the user receives depending on the current temperature.

If the current temperature is higher than average, we want it to say one thing, and if it's lower than average, we want it to say something else.

To accomplish this, we use something called a conditional, also known as an IF statement.

For the weather example, the conditionals might look like the following:

(Note: this example does not use a real programming language. It is written in 'pseudocode', which means it is using normal human language, but formatted as it would be in a program).

If the temperature is higher than normal:
    Say, "It sure is warm today!"

Or, if the temperature is lower than normal:
    Say, "It's cold out!"

Otherwise, 
    Say "It's typical weather for today."
        

Syntax

The specific syntax for IF statements varies from language to language, but they all tend to follow a basic convention.

We will now step through and explain the following example of a conditional written in JavaScript:

if (todayTemp > averageTemp) {
    console.log("It sure is warm today!");
}
else if (todayTemp < averageTemp) {
    console.log("It's cold out!");
}
else {
    console.log("It's typical weather for today.");
}

The first line of the code sets up the conditional:

if (todayTemp > averageTemp) {

The if keyword tells the computer that the next block should only be run if a certain condition is met.

The condition that determines whether the code will run is laid out in the parentheses immediately after the if keyword. This is basically just a yes-or-no question, written out in code. This expression is an example of a Boolean, meaning the computer will look at whatever is in the parentheses and evaluate as being true or false.

In this example, the question we are asking is "Is the number in the variable todayTemp larger than the number in the variable averageTemp?"

If the answer is yes - meaning the expression in parentheses evaluates to true - the computer will execute the code block laid out by the curly braces { }

If the expression in the first set of parentheses is true, the computer executes the code in the first set of braces, ignores the next two blocks, and moves on with its life.

If the expression is false, the computer looks to see if any other conditions have been specified and proceeds to check those.

The ways we can set up those additional conditions are using the else if and else keywords.

The else if keyword is setting up another yes-no question that will only be asked if the first conditional in the initial if statement is false.

The else keyword sets up code to execute in the event that none of the conditions laid out in the if or else if statements are true.

If (condition A) is true, do this.

Otherwise, check if condition B is true. If so, do this other thing.

If neither A nor B is true, check if condition C is true. If so, do this third thing.

If none of those are true do this last thing.

The else code is only executed if none of the other conditions that have been specified are true.

Loops

One of programming's superpowers is the ability to quickly do the same task multiple times with a single block of code.

Say you had a list of 100 people. In this list, each entry has three pieces of information: their first name, their last name, and their job title.

You want to output each person's name and job title as a single item, formatted a particular way:

Last, First: Title

Doing this by hand would require multiple steps for each entry:

That's a 9-step process, for each name in the list - 900 steps in total!

Even if a human were able to perform each step at a consistent (also unrealistic) rate of half a second each, it would still take them 7 and a half minutes to complete.

With a computer, we could go through and complete that task in a fraction of a second using something called a loop.

Loop

A loop is a way to have a computer repeat a block of code multiple times according to a set of conditions provided by the programmer.

The types of loops you will most often see are for loops (most common) and do/while loops.

With loops, we only need to tell the computer how to do the 'last name-first name-job title' process once, and then as long as the list of data we give it is formatted in a predictable way, it can repeat it as many times as we need.

For Loops

One of the most common loops we see in programming is called a for loop.

These specify a set of condition using the for keyword (as in, 'for the following conditions, do X'), and then have a block of code that will be repeated until the conditions are fulfilled.

In pseudocode, a for loop is the following:

for (these conditions), repeat the following:
    code statement 1
    code statement 2
    code statement ...

    

To write a for loop for the above example (e.g. take a list and output each item in the format "Last name, First name: Job Title") in JavaScript, the code would look something like this:

for(var i = 0; i < 30; i++) {
    outputText = myList[i].lastName + ", " + myList[i].firstName + ": " + myList[i].jobTitle;
    console.log(outputText)
    }

This initial statement sets up the whole for block:

for(var i = 0; i < 100; i++)

This construction is common in languages that use syntax based on the language C, like C++, Java, and JavaScript. Python does for loops a little bit differently, and we'll go through that in a moment.

The for keyword lets the computer know it's a loop, and the piece inside the parentheses sets the conditions.

But what do those conditions mean? The code in the for( ... ) piece is a little unusual at first - let's unpack it.

It's actually three separate statements:

var i = 0;
i < 100;
i++;

The first statement:

var i = 0;

initializes a variable, i, and sets it to 0. This is just a regular variable and could be called anything, but the convention with for loops is to name it i (for index or iterator). If there are loops within loops (which is not uncommon), the subsequent variables are often called j, k, etc. as a cue to people looking at the code that it is a sub-loops within the first i.

The second piece:

i < 100;

sets up the condition when the loop should end. In this case, as long as i is less than 100, the loop should continue. Once i reaches 100, the loop will stop repeating.

But how does i change?

That's what the third piece does:

i++;

That statement increments i by one - meaning it adds one to the current value of i. It is functionally the same as i = i + 1, but it is such a frequently done operation that it has its own operator, ++.

So summing up the for statement construction:

for(var i = 0; i < 100; i++) {
    [code statements]
}
    
  1. for(...) tells the computer to initiate the loop
  2. var i = 0; runs before the loop starts and sets up the index/iteration counter that will be used to see whether to continue running the loop
  3. i < 100; runs before each iteration and checks whether the condition has been met (e.g. if i is still less than 100). If not, it executes the next iteration
  4. i++ runs after each iteration and increments the counter.
  5. The { } block contains the code that will be repeated on each iteration.
myList = [
    {"firstName": "Bucky", "lastName" : "Badger", "jobTitle": "Mascot"},
    {"firstName": "Terry", "lastName" : "Tortoise", "jobTitle": "Slowpoke"},
    {"firstName": "Spongebob", "lastName" : "Squarepants", "jobTitle": "Fry cook"}
]
    

The Coding Process

Editors and IDEs

Most programming languages use standard, plain text files for their code scripts. Because these are just regular text, you can open and view most code files with any text editor - Notepad, TextEdit, or even Microsoft Word.

Even though it's technically possible to write programs using Notepad or TextEdit, the experience of writing code is improved immensely by using an editor that's specifcally intended for code.

Some of the quality-of-life enhancements that code editors provide include:

  1. Syntax highlighting. Different parts of the code using different colors or fonts to let you visually differentiate between things like variable names, functions, IF statements, etc. at a glance.
  2. Code completion. Many code editors will auto-complete things like function names or variables that you have created as you type them. This can saves time and can help prevent bugs that come from mis-typing the name of a variable or function.
  3. Linting/syntax-checking. Code editors let you use something called linting, which is the term for making sure your code follows all correct syntax and style conventions. Linters help you find errors before running the program, so you don't need to spend time debugging easy to miss errors like forgetting a semicolon at the end of a statement or not closing an IF block.
  4. Debugging Tools. Many code editors offer tools like debuggers that let you step through code line by line and see what each statement is doing. These can help you figure out why a program isn't working as expected.
  5. Project/file management. Many code editors make it easy to find files and see the folder structure for your project at a glance, as well as things like having multiple files open side-by-side in the editing window.

Code Editors

There are a variety of editors available, and they all have different strengths and uses.

Some of the most popular general-purpose code editors as of early 2021 include:

  1. Visual Studio Code
  2. Sublime Text
  3. Atom
  4. Brackets
  5. Nova
  6. Notepad++
  7. BBEdit

Most of these are free and support many different programming languages, from the big ones (C, Java, JavaScript, Python, etc.) to more esoteric ones (Go, Rust, etc.)

Many code editors support extensions or plugins, which allow you to expand the functionality of the editor to do things like custom code highlighting, pre-written snippets, API documentation, and more.

IDEs

Some languages are made easier by using an IDE, or Integrated Development Environment.

An IDE is a code editor with more features. They include things like comprehensive syntax-checking, code-completion, documentation for the current programming langage/API, as well as tools like compilers and package managers. These are extremely important for compiled languages like Java, C++, and Swift, which require the additional step of compiling the code into an application before it can be run.

Many IDEs will have a tool set that focuses on a primary language like Java or Objective-C, but support the syntax and tooling for many other languages.

Examples of IDEs include:

  1. Eclipse (Java)
  2. IntelliJ (Java)
  3. PhpStorm (PHP)
  4. XCode (Swift and Objective-C)
  5. Aptana (general purpose)

Libraries and APIs

It's rare for a programmer to write every single line of code that a program uses, especially when working on a large code base. Most programmers incorporate libraries into their programs. Libraries are packages of code written by other people, that solve a particular problem or perform a certain function so the programmer doesn't have to write it themselves from scratch.

Libraries

Libraries are collections of code that are meant to be incorporated into other programs to add functionality or make the programmer's job easier.

Libraries can range from simple one-or-two line utilities that add basic features, to comprehensive tool kits that provide extensive new functionality to a language.

Standard Library

Most languages have something called a standard library that is packaged with the interpreter/compiler. The specific functions in a standard library vary from language to language, but they are all doing the same thing: providing features that are not present natively in the core language syntax.

Features in a standard library can include things like file input/output, functions to sort the items in an array or list, math functions like min/max, randomize, etc.

Some languages have comprehensive standard libraries. Others include only a few basic functions in the standard library, and programmers will include additional packages and libraries when they need more features.

APIs

Another term that is common in programming the Application Programming Interface, or API.

An API is a set of features that provide access to certain functions without the programmer having to write the code themselves. An API is similar to a library, but often more standardized and more comprehensive. It is an example of abstraction, where it allows the programmer to use functionality without having to know the details of how it is implemented.

For example, the API for Swift and Objective-C (the programming languages used for MacOS and iOS app development) includes functions (also called methods) that let the programmer do things like take a photo with the camera in an iPhone without having to know anything about the technical details of the camera itself.

The process of an iPhone camera producing a digital photograph involves many, many complicated steps and requires a complex interplay between the phone's hardware and software. Engineers at Apple did the hard work of finding solutions to these processes, and they provided a straightforward way to use that functionality via a command in the API. An everday programmer coding an iPhone app can use the function in the API without having to know any of the complicated aspects of the mechanisms involved.

In JavaScript, the common function document.GetElementByID() is part of the API for working with documents in a web browser.

Coders don't need to know anything about how the browser manages page memory, interacts with the operating system, parses the structure of the HTML, or any of the other countless things involved in displaying and accessing the contents of a website. They can simply use that command and it will return the requested item within the page.

Version Control

In situations where the code base grows more complicated than one person can easily keep track of or when multiple people are working on a project, it is essential to incorporate version control into the development process.

Version control means keeping a record of all the changes that have been made over the lifespan of a project. This is helpful if the coder wants to refer to a previous version of the program, or if they make a change that breaks something and want to identify where the bug occured. When new code is added or old features are removed, so the coder can revert to previous

One of the other main features of version control is the ability to create branches of a project. A branch is a parallel version of the code that have different sections included. Branches are helpful for testing out new features without affecting the rest of the code.

Version control is essential when multiple people are working on a project, because it allows everyone's changes to be consolidated into one central set of code without overwriting the work of the other collaborators.

git and GitHub

The most common version control system in use today is called git. Git is an open-source tool that can be installed in a project folder and can be used to keep track of all files, chnages, and updates over the lifetime of the project. A git project is known as a file repository or repo for short.

GitHub is a popular online platform for hosting and collaborating on shared git projects. It is used extensively for collaboration on open source code projects, but it is also used by individuals as a cloud-based way to manage their code.

Git and GitHub are Different!

Git is an open source tool that anyone can download and use locally on their computer.

GitHub is an online service/platform that is owned by Microsoft that is used to facilitate working together on shared git projects.

The terms are often used interchangeably, but you do not need GitHub to use git on your projects. GitHub is the most popular platform for collaborating on code projects, but there are other services you can use as well, such as BitBucket, SourceForge, and Launchpad.