CS50 — Week 4, part 2

CS50 — Week 4, part 2


  • alternative way to pass data between functions
    • up to this pt, we’ve passed data by value (aka only passing a copy of that data)
  • with pointers, we can pass the actual variable itself into a function
    • so a change made in one function can impact what happens in a different function

computer memory

  • every file on your comp lives on your disk drive (hard disk drive HDD, solid-state drive SSD, etc.)
    • disk drives = storage space; we can’t directly work (manipulate, use data) there
    • so we have to move data to RAM to work with it (the memory I’ll be dealing with as a programmer)
    • every time the computer is turned off, RAM is wiped (thus the need to store data in disk drives)
  • memory = huge array of 8-bit wide bytes (512 MB, 1 GB, 2 GB, 4 GB…)
  • every int I create requires 4 bytes, a char requires 1 byte, a double or long long 8 bytes… etc.
  • so, memory is a big array, and arrays can store info and allow for so-called random access
    • aka it’s unnecessary to iterate thru every element until we come to the one we want – we can just indicate the index location (usually b/c of size referred to in hexadecimal rather than binary notation)
  • every location in memory has an address, and a pointer is nothing more than an address!!!

back to pointers

  • pointers are addresses to variables stored there
  • a pointer’s value = a memory address
  • a pointer’s type = the kind of data stored at that memory address
  • simplest pointer in C = the NULL pointer (which points to nothing)
  • when creating a pointer and not immediately setting its value (to some meaningful address), always set the value of the pointer to NULL
    • check using the equality operator if (pointer == NULL)
  • easy way to create a pointer = “extract” the address of already existing variable with the address extraction operator&
    • ex. if x is an int-type variable, then &x is a pointer-to-int whose value is the address of x
    • if arr is an array of doubles, then &arr[i] is a pointer-to-double whose value is the address of the i‘th element of arr
      • an array’s name = actually a pointer! (to its 1st element)
  • why do we care about where some variable lives in memory?
  • with the dereference operator *, we can look at and modify the data at the location
    • ex: if we create a pointer-to-char called pc, then *pc is the data that lives at the memory address stored inside the pointer variable pc
      • if we do *pc = D, we are assigning a new value to the data at address pointed to in pc
  • * in this context “goes to the reference” and accesses the data at that memory location (like how knowing a neighbor’s address isn’t enough to allow you to hang out with them – you have to go to their house first)
  • what happens if we try to dereference a pointer whose value is NULL? –> a segmentation fault! (can’t go to “nothing”!)
    • this is good behavior, b/c it defends against accidental dangerous manipulation of unknown pointers
    • better for your program to crash than screw up another function or another program (possibly outside of yours)
    • segmentation faults = segfault = when we try to touch memory we shouldn’t, aka a program trying to read or write an illegal memory location
  • int* p;
    • value of p is an address
    • dereferencing p (*p) will give the value at address p, which will be of type int
  • int x, y, z;
    • statement declaring 3 integer variables
  • to declare multiple pointers of the same type on the same line:
    • int* pa, *pb, *pc;  …each needs its own star!
    • pointers are a syntactic messy mess and it’s easy to make mistakes with them

twinkle twinkle little star

  • type name (as in, char*, a pointer to the 1st char in a “string”)
  • dereference operator (“go to” that address to get at that data)

64-bit machine vs. 32-bit

  • in a 32-bit machine, every memory address is 32-bits long, or 4 bytes
  • in a 64-bit machine, addresses in memory are 8 bytes
  • since pointers are just addresses, they too will either be (no matter the data type) 4 or 8 bytes in size

Screen Shot 2017-09-28 at 2.41.58 PM

Dynamic Memory Allocation

  • rather than having to know exactly how much memory we’ll need at compile time (b/c of various user input over long periods of time, for example), we can get access to dynamically allocated memory at run time (aka, as our program runs)
  • dynamically allocated memory comes from a pool of memory called the heap
  • up to now we’ve been working with named variables we create that come from pool of memory called the stack
  • if the heap and the stack run into each other, the program essentially runs out of memory!
  • standard library function malloc()
    • parameter = number of bytes requested
    • if it can obtain the requested memory for you, malloc() will return a pointer to that memory
    • if it can’t, it’ll hand back NULL
  • statically obtain an integer:
    • int x;
  • dynamically obtain an integer:
    • int *px = malloc(sizeof(int));
    • 4 bytes of memory from the heap, pointer called px, which we can dereference to access the data there
  • when using dynamically allocated memory, MUST remember to “hand back” that memory explicitly b/c it’s not automatically returned to the system for later use when the function in which it’s created finishes execution like static memory
    • failure to do so results in running out of memory or in memory leaks which can compromise performance
    • use free() that accepts pointer to some memory (but only do it one time or else weird things might happen)

char* word = malloc(50 * sizeof(char));
// do stuff with word
// now we're done working with that block of memory


  • arrays have allowed us to unify a bunch of elements of same type
  • structures allow us to bring together variables of different types
    • all put together into a new variable type w/ its own name
  • a “super-variable”!

struct car
   int year;
char model[10];
char plate[7];
int odometer;


  • usually define structure in separate header (.h) files or atop our programs outside of any functions
  • access the fields of the structure with the dot operator.

// variable declaration
struct car mycar;

// field accessing
mycar.year = 2011;
strcpy(mycar.plate, "CS50");

mycar.odometer = 50505;

…in which the data type is struct car and the variable name is mycar

  • structures, like all variables, don’t need to be created on the stack, but can use dynamically allocated memory at run time
  • if we do this tho, 1st need to dereference pointer to the structure (aka “go to” the memory address storing the structure) and then access its fields

// variable declaration
struct car *mycar = malloc(sizeof(struct car));

// field accessing
(*mycar).year = 2011;
strcpy((*mycar).plate, "CS50");

(*mycar).odometer = 50505;

  • but C programmers like shorter ways to do stuff than having to use the * operator along with the . operator, so there’s this: -> (dereferences structure and accesses field all at once):

// field accessing
mycar->year = 2011;
strcpy(mycar->plate, "CS50");

mycar->odometer = 50505;

Defining Custom Types

  • way to write shorthand or rewritten names for data types w/ C keyword typedef
    • typedef <old name> <new name>;
  • top of .c file or in separate .h file
    • ex: in CS50.h, this line of code: typedef char* string; because at first it was easier to just think of them as strings and not worry about the char* bit
  • can define the structure in the middle of my typedef!

typedef struct car
int year;
char model[10];
char plate[7];
int odometer;

  • now, when declaring my variable of this new struct type, I don’t have to write struct car mycar; but instead can use whatever new name I’ve assigned, like car_t mycar;


  • a function that calls itself
  • an “elegant” solution because of how short they can make functions and how they can help avoid long loops or calling additional functions

an example from math class

  • factorial function n! is defined over all positive integers
  • n! = the product of all positive integers less than or equal to n
  • let’s define the mathematical function in programming terms as fact(n)

fact(1) = 1
fact(2) = 2 * 1
fact(3) = 3 * 2 * 1
fact(4) = 4 * 3 * 2 * 1
fact(5) = 5 * 4 * 3 * 2 * 1

which can be reduced to

fact(1) = 1
fact(2) = 2 * fact(1)
fact(3) = 3 * fact(2)
fact(4) = 4 * fact(3)
fact(5) = 5 * fact(4)

which is the same as saying

fact(n) = n * fact(n - 1)

  • for any recursive definition,
    • a base case – simple solution to problem that stops the recursion from continuing
    • the recursive case – passes work to different call down the line

int fact(int n)
   // base case
   // recursive case

int fact(int n)
   if (n == 1)
      return 1;
      n * fact(n - 1);

hint #1: you don’t need curly braces following the conditional statement (like ifelse) when there’s only one line of code inside!

hint #2: we’re returning 1 on the base case b/c the factorial of 1 = 1 😉

  • also possible to have more than one base or recursive case if the program might recurse or terminate in different ways depending on input

example of multiple base cases

  • the Fibonacci number sequence:
    • 1st element is 0
    • 2nd element is 1
    • nth element is sum of (n – 1)th and (n – 2)th elements

int fibo(int n)
   if (n == 1)
      return 0;
else if (n == 2)
return 1;
fibo(n - 1) + fibo(n - 2);


example of multiple recursive cases

  • the “Collatz conjecture” – applies to positive integers and speculates it’s always possible to get “back to 1” by following steps:
    • if is 1, stop
    • otherwise, if is even, repeat this process on n/2
    • otherwise, if is odd, repeat this process on 3+ 1
  • recursive function collatz(n) that calculates # of steps to get to 1 when starting from n:


int collatz(int n)
// base case
if (n == 1)
return 0;
// even numbers
else if (n % 2 == 0)
return 1 + collatz(n/2);
// odd numbers
return 1 + collatz(3*n + 1);

The Call Stack

  • when a function is called, the system sets aside space in memory for that function to do its work
    • these chunks of memory are frequently called stack frames or function frames
  • more than one function’s stack frame may exist in memory at a given time
    • ex: if main() calls move(), which then calls direction(), all 3 functions have “open frames” – but only the most recently called function has an “active” frame
  • when a new function is called, a new frame is pushed onto the stack and becomes the active frame
  • when a function finishes, its frame is popped off the stack, and the frame immediately below becomes the new active function on the top of the stack, picking up where it left off in its executable steps

File Pointers

  • the data structure FILE in C represents any files you can click on in familiar GUI world
  • the ability to read data from and write data to files = primary way of storing persistent data (aka data that does not disappear when my program stops running)
    • if something went wrong, you can review what happened, for example
  • almost always when working with files, we’ll be using pointers to them in FILE* form
  • file manipulation functions all live in stdio.h (along with printf())
  • fopen()
    • opens a file and gives file pointer to it
      • FILE* ptr = fopen(<filename>, <operation>);
      • FILE* ptr1 = fopen("file1.txt", "r");
      • in this example, I’ve created a pointer-to-file called “ptr1”, and what it’s pointing to is a new text file I’ve just opened called “file1.txt” and the operation is read so we can read information from the file
      • "w" for write, which will write information to the file (or overwrite if info was already written there)
      • "a" for append, which will write new information to the end of the file
      • only one operation with one file pointer!
      • if we want to do 2 different operations, we have to create 2 separate pointers that point to the same file!
    • always check return value for NULL (even tho vast majority of time we’ll have gotten a legitimate pointer back)!!
    • fclose()
      • closes file, pass in name of the file pointer
      • fclose(ptr1);
    • fgetc()
      • reads and returns next character from file pointed to (if it’s the 1st time fgetc() is being called for a file, it’ll be the 1st character in the file)
      • when opening the file, the operator passed in as a parameter must be "r" for read, or there’ll be an error!
      • char ch = fgetc(ptr1);
    • fputc()
      • writes or appends character to pointed-to file
        • fputc(<character>, <file pointer>);
      • again, operation of the file pointer must be "w" for write or "a" for append, or there’ll be an error
    • fread()
      • since getting characters one at a time with fgetc() isn’t very efficient, fread() allows us to get an arbitrary amount of info from a file
      • reads <qty> units of size <size> from the file pointed to and stores them in memory in a “buffer” (usually array) pointed to by <buffer>
        • fread(<buffer>, <size>, <qty>, <file pointer>);
        • int arr[10];fread(arr, sizeof(int), 10, ptr);
        • double* arr2 = malloc(sizeof(double) * 80);
          fread(arr2, sizeof(double), 80, ptr);
        • char c;fread(&c, sizeof(char), 1, ptr);
      • operation of file pointer must be "r" for read!
      • the 1st parameter is an address! so while we can just use the name of an array (because that is a pointer), we have to use the address extraction operator if we’re just dealing with a regular variable like a char
    • fwrite()
      • writes <qty> units of size <size> to the file pointed to by reading them from a buffer (usu. array) pointed to by <buffer>
        • int arr[10];fwrite(arr, sizeof(int), 10, ptr);
        • in this case, there’d be some numbers stored in the array arr that you wanted to write to your file that’s pointed to by ptr
      • operation of file pointer must be "w" or "a"
  • and there’s more:

Screen Shot 2017-09-29 at 11.58.46 AM

Today’s Resources:



Well, summer break happened.

Well, summer break happened.

In the teaching world, summer break is a thing. And here in Japan, it’s basically all of August. Nobody really likes studying over vacation (except perhaps the Japanese). That being said, I could have continued my studies in computer programming. Sure, it would have been hard – there’s no wifi on the beach, after all. But I could have made some effort.

I did not.

So here I am, after a month-long break. I’ve relaxed, I’ve procrastinated a (maybe more than) tiny bit, and now I’m back. Ready to go. Ready to embrace my uber-noobness and struggle through what seem, on the surface, to be the simplest of problems (CS50 pset2, anyone?).

For, as it has been said by many wise people in many ways, the only way I can fail at this is if I give up.

CS50 — Week One Notes, part 2

CS50 — Week One Notes, part 2

I had to break these up into two different days because it was information overload.

Side note to self: If I’m ever in my workspace and I accidentally made a never-ending program that’s trying to force me into infinite loops of the same banal GetInt() function, use ctrl-z!!!

Conditional Statements

  • conditional branches/expressions/statements allow programs to make decisions depending on variable values or user input

if (boolean expression)



  • (mouse down) (x < 10) etc.
  • boolean expression evaluates to true? all lines of code between curly braces will execute from top to bottom
  • boolean expression false? those lines of code will not execute

if (boolean expression)






  • boolean expression evaluates to true? all lines of code between 1st set of curly braces will be executed, top to bottom
  • false? follow instructions in 2nd set of curly braces
  • I can create chains of mutually exclusive branches by using if, else if (* n), else with as many different boolean expressions
  • I can layer on as many if statements as I like (creating a chain of non-mutually exclusive branches), but the else will only bind to the nearest if


  • allows me to specify distinct cases rather than having to rely on boolean expressions
  • must break between each case, or I’ll “fall thru” each case and execute them all in order (unless that’s what I’m trying to do)

int x = GetInt();
case 5:
case 4:
case 3:
case 2:
case 1:
printf("Blast off!\n");

The ternary operator ?:

  • useful when writing very short if-else statements
these 2 code snippets act identically
int x = GetInt();
if (b == 0)
x = 5;
x = 6;
int x = GetInt();
x = (b == 0) ? 5 : 6;


  • allow programs to execute lines of code repeatedly (saves me from needing to copy/paste or repeat lines of code)

while (true)



  • infinite loop: lines of code between curly braces will execute repeatedly top to bottom forever (because true is always true)

while (boolean expression)



  • if boolean expression evaluates to true, all lines of code within curly braces will execute repeatedly top to bottom, until the boolean expression evaluates false




while (boolean expression);

  • executes all lines of code in curly braces, then checks boolean expression – if true, goes back and repeats code block until boolean exp. evaluates to false

for (int i = 0; i < 10; i++)



  • repeats the body code a specified number of times (above, for example, 10 times)
  • what the parts?!
    • the counter variable(s) (above, i) is set (above, to 0)
    • the boolean expression is checked (above, i < 10)
      • if true, body code executes
      • if false, body code does not execute
    • after getting to the end of the body code, the counter variable i is incremented (above, by 1, using the shorthand var++)
    • the boolean exp. is checked again and either goes thru the body code again or does not
  • more generally: for (start; expr; increment) where there can be more than one statement in start and increment


CS50 — Week One Notes

CS50 — Week One Notes

From Scratch to C.

  • same elements, different language to implement
  • write a bit of code, run/test it, then move on once its debugged (vs trying to write all your code at once and then not really knowing which part is making the whole thing crash, etc.)
  • any time I’m copying and pasting code from one place to another without changing a thing, there’s probably a better way to do it (a cleaner design)
  • source code –> pass as input to a compiler –> compiler turns it into source code that computer can read
    • “compiling” = umbrella term
    • preprocessing
      • find and (re)place
      • ex. “#include”s in C are in this step
    • compiling
      • source code –> assembly code
      • assembly code = arcanely worded sets of instructions that sets of 0s and 1s are mapped to
    • assembling
      • assembly code –> 0s and 1s
    • linking
      • computer gets the 0s and 1s from the code I wrote in my .c file, CS50 staff’s 0s and 1s from what they wrote in the CS50 library, printf()’s 0s and 1s from the standard input/output library, etc. (whatever was used to write my program) and combines together in my program “hello” (or w/e the name is)

The imprecision of number representation due to a computer’s limitations

  • we humans can think of and create an infinite number of numbers
  • computers are limited by how much memory’s available in their RAM
    • think about it – without any tricks or extra code, a byte can only store integers of value 0 thru 255!
  • we humans need to be aware of this limitation
    • ex: when The Patriot anti-air missile system was programmed to count time by 1/3 of a second, and so after running for many hours straight its timing was off and lives were lost
  • by creating a simple program in C, we can see that the computer’s representation of the decimal created by dividing 1 from 10 (should be 0.10) proves itself to be fundamentally imprecise when taken to too many decimal points
    • printf(“n is %.55f”, 1.0 / 10.0); yields “n is 0.1000000000000000055511151231257827021181583404541015625”

The Linux Command Line

  • Mac OS is a descendent of the Unix operating system (“a Unix-based system”); Linux was developed independently but is similar to Unix; Windows was built from ground up in a completely different way
  • the default command line in the CS50 IDE is terminal called “workspace”
  • shell = software interface (often a command line interface) that allows users to interact with the computer
  • bash = “Bourne-Again Shell”, the default shell environment in Linux and Mac OS X

name of command <argument>


  • short for “list”, gives readout of all files and folders in current directory
    • blue = directory (that I can navigate into)
    • black = text or source code file
    • green = executable file

cd <directory>

  • short for “change directory”, changes current directory to (a specified)<directory> in my workspace or on my operating system
    • shorthand for current directory = .
    • shorthand for parent directory of current directory = . .
    • pwd (present working directory) gives name of current directory
    • cd on its own will bring me back to original directory location
  • if the name of a directory has multiple words separated by spaces, must enclose name in quotes when calling it up

mkdir <directory>

  • short for “make directory”, creates a new subdirectory called <directory> located in the current directory
  • analogous to: in GUI operating system, right click and in prompt click “New Folder”

cp <source> <destination>

  • short for “copy”, creates duplicate of file I specify as <source>, which it saves in <destination>
  • cp -r <source directory> <destination directory>
    • copies entire directories (“-r” flag stands for recursive, tells cp to copy everything inside directory, including any subdirectories it may contain)

rm <file>

  • short for “remove”, deletes <file> after asking me to confirm (y/n)
  • rm -f <file>
    • skips the confirmation (but can’t undo so beware)
  • rm -r <directory>
    • deletes entire directory (can combine with -f flag: “rm -rf <directory>”)

mv <source> <destination>

  • short for “move”, renames file, moving it from <source> to <destination>

chmod <options> <permissions> <file>

  • short for “change mode”, changes the permissions of files or directories (aka who can access the file and how they can access it)
  • options are like the -f flag we saw before, plus others
  • permissions can be defined for:
    • user (owner of file)
    • group (members who own the file with you)
    • others (anyone else)
  • permissions can include:
    • read (4), write (2), execute (1), no permission (0)
  • example in symbolic permissions notation:

    chmod u=rwx, g=rx, o=r myfile

  • same example in octal permissions notation:

    chmod 754 myfile

ln <target> <linkname>

  • short for “link”, creates links between files
  • if <linkname> is omitted, link to <target> is created in current directory using the name of <target>
  • different from cp – same data is being pointed to by both file names
    • if go into data and change it from either, the data is changed for both
    • if one of the files is deleted, data is still safe in other file which points to it
  • use the tag -s to create symbolic links (file points to other file/directory where data/files are stored)

touch <file>

  • changes file timestamps (access, modification times) to current system time

rmdir <directory>

  • short for “remove directory”, removes directories that are empty

man <argument>

  • short for “manual”, shows the system’s reference manual for that argument (usually a program, utility or function)

diff <file> <file>

  • short for “different”, analyzes two files and outputs instructions to change 1st file to match the second

sudo <command>

  • short for “superuser do”, allows a permitted user to execute command as another user


  • clears the screen
  • same as ctrl + l in bash


  • short for “TELNET protocol”, used for communication with a remote host or device
    • ex: “telnet hostname” would connect user to a host named hostname
    • unsafe to transfer data in this way because others could grab data as it’s being transmitted

Data Types

  • in C, an older language, we have to specify each data type before we can work with it

Native to C:


  • used for variables that will store integers
  • integers always take up 4 bytes (32 bits) of memory
    • roughly, that means we can store -2 billion to 2 billion
  • unsigned int
    • unsigned = qualifier that doubles positive range at the expense of disallowing negative values
      • other qualifiers = long, short, const
    • if I know my value will never be negative, I can use this to count up to ~4 billion


  • used for variables that will store single characters
  • characters always take up 1 byte (8 bits) of memory
    • thanks to the ASCII system, different numbers have been assigned to characters (ex: “0” = 48)


  • used for variables that will store floating-point values (real numbers)
  • floating point values always take up 4 bytes (32 bits) of memory
    • some of these bits are used for the integer part and some for the decimal portion
    • we are limited to how precise we can be


  • used for variables that store floating-point values
  • double precision -> always take up 8 bytes (64 bits) of memory


  • a type, but not a data type (can’t assign a value to it)
  • void functions don’t return a value
  • a function’s parameter list can also be void if the function takes no parameters
    • like “main”, when we haven’t been passing any arguments into main (we could tho)

Thanks to the CS50 Library:


  • used for variables that store a Boolean value (true or false)
  • booleans are a standard default (native) data type in many modern programming languages, but not in C


  • used for variables that will store a series of characters (a “string”)
  • can be words, sentences, paragraphs or even books!
  • not a native data type in C, so doesn’t come up as purple in the IDE

And later on…


  • used to group ints and strings into one unit


  • “defined types” that allow me to create my own data types


  • to create (“declare”), specify the data type, give it a name, and slap a semicolon to the end
  • to create multiple variables of same type, specify the type once and then list as many variables as desired
    • int height, width;
      • creates two integer type variables with respective names “height” and “width”
  • good practice = create variable right when I need it
  • after a variable’s been declared, no need to specify variable type again
  • simultaneously declaring and setting the value of a variable? (“initializing”)
    • int price = 17; // we’ve specified an integer, named the variable “price” and assigned it a value of 17


  • in C, we can add, subtract, multiply and divide numbers, as well as get the remainder of left number divided by right (modulus)
    • int m = 13 % 4; // m is now 1 (13 / 4 = 3.25 or 3 1/4)
    • the modulus operator is more useful than you’d think (like in a random number generator, whereby you want to limit the range of the random number you generate)
  • shorthands
    • x = x * 5 –> x *= 5; // works with all 5 basic operators
    • x++; // incrementing variable by 1
    • x–; // decrementing variable by 1
      • x = x + 1 == x += 1 == x++

boolean expressions

  • in C, every nonzero value = true and every zero = false
  • don’t always have to use type bool when we are working with boolean expressions
  • logical operators
    • logical AND (&&) is true if and only if both operands are true, otherwise false
      • if x && y {..go down this path..}
      • x is true and y is true? true
      • x is true but y is false? false …etc.
    • logical OR (||) is true if and only if at least one operand is true, otherwise false
      • if x || y {…}
    • logical NOT (!) inverts the value of its operand (called “bang”, “exclamation” and “not”)
      • x is true? !x is false (and vice versa)
  • relational operators
    • less than (x < y)
    • less than or equal to (x <= y)
    • greater than (x > y)
    • greater than or equal to (x >= y)
    • equality (x == y)
    • inequality (x != y)


basics of higher-level software programming languages

basics of higher-level software programming languages


Computers are linear – they are reading top to bottom, left to right. If you want to make two things happen at once, you are creating the illusion that the computer is doing this. (But it’s not, it’s doing one thing at a time still.)

Everything’s possible with programming, but it takes some cleverness to accomplish it.

Everything has a position – the file, the program, etc.

Frames: everything you want to have happen “at the same time” has to happen within the same frame – calculate a little, show a little, calculate a little, show a little.

Anything that happens first tells the computer what’s coming next. (For example, in CSS the “.” before “.thick-outline” tells me that it’s going to be read by the computer as a class, and not something else.)

Always think about everything possible that people could do. If you have some data they can input, think about all the different data they could give you, even outside your expectations (hackers use data exceptions to break programs, for example).

If you ask the computer, what data is in that position, and that position does not exist, it’ll be “null”.

Array = database without a pointer, instead it just has numerical names to identify the location of various elements. (No “next line”, just “data piece 4” or “data piece 7”.)

Native data types = what’s already there. If it’s not a native data type (like strings in C), it’s going to take an extra cleverness to make it work.

Because the computer reads left to right, there’s precedence. Whatever comes first sets the stage. For example, it quotes tell the computer there’s text coming, and then I type “//”, this will be shown as text and not create a comment.

In C, “;” is not needed at the end of pre-processor directives (#blah input library blah), comments, function definitions, and controllers.

In C, you must start with the function “main” (like how you have to put everything you want the user to see inside the <body> tag in HTML).

4 basics:


  2. data

  3. functions

  4. variables/names


  • text not read by the compiler or the interpreter
  • // – line comment (end comment by going to new line)
  • /* – block comment (end with */)
  • for me, for other people looking at my code, and for remembering things that for the moment aren’t visible (I create some variable but what am I using it for? etc.)


  • number types
    • boolean
      • 0 or 1 – one binary digit
      • for example: “true” returns a boolean 1
      • use it every time you can
    • integers (C: “int”)
      • shorts, longs
      • if you can stick with integers, do! otherwise it’s a big mess
    • floating points/floats
      • decimals are infinite! (and therefore can get very messy)
  • character types (C: “char”)
    • strings = a string of characters/a blip of text
      • in C, there’s support for how you can make your own strings, but it’s not it’s own type like it is in C++
        • one way: make an array of letters
        • a bunch of things stored in your memory, accessed thru pointers telling your computer where in the memory to look for that piece of data
        • can use functions like “printf”
  • pointer types
    • every type of data is really just a position on the memory that your computer is accessing
    • the computer does not hold anything in its hands – it goes line by line and accesses the memory
    • even RAM is used in the same way, where the computer has to call it up from memory to access it, but it’s just a smaller amount and therefore easier/faster for the computer to get through it
    • pointers tell the computer where to look in the memory


  • basic, native functions
    • aka the actual building blocks
      • examples: +, int, return, true, const char, etc. (everything pink in Xcode)
      • “int” locates, sets up memory space that’s the right size to store integers
  • library/included functions
    • a group of other people’s functions (built off native functions)
    • problem could be that I don’t know how it works, since someone else made it
      • documentation (description, examples, source code) or testing can help
  • custom functions
    • my own (also built off native functions)
  • functions work with data
    • functions that work with integers only, for example
  • 3 things functions do:
    • input data into the function from the code (either actual user input I type in and can see or from variables I’ve set up with some stored memory of some data) = parameters
      • a function can have no parameters, many (a lot of things to input), etc.
    • output data into the code = return
      • for example, if I am using the function “+” and I input the data (3, 3) the output will be (6)
        • if I set a variable y = 3+3, then y will have a value of 6 anywhere it appears in my code
      • a function can only have one return -> need many returns? use multiple functions
      • the “return” function helps other functions return
    • do
      • changing a pixel in a game, putting a command prompt on the screen, making a sound
      • many functions could come first and pass data around before you ever come to a “do” that the user can see
      • can be a do function just on its own, for example “delay” that makes the program wait
      • code pointer movement functions, just moving code pointers around, like “if” “then” “loop” etc. are do
      • you can’t program a functionality – every function with a “do” is native
    • some functions will do all 3, many will have parameters and then do something or give a return
    • syntax of a function: name (parameters)
    • most of the time, my functions will call other functions – functions inside functions
      • subdividing functions into smaller little pieces is useful because then I can reuse them other places – allows for flexibility & allows me to work on smaller tasks at a time for more gratification/feeling of progress
    • when you use a function, it’s a “call
    • before you ever use it, you need to define a function (tell the computer what to do)


  • there’ll be many custom things I’ll make – functions, variables, arrays & structs of different types
    • variables = holding data directly
    • arrays = bunch of different data each with a number to identify it
    • structs = multiple pieces of data held in a single location (using different sub-names to access them within that location)
  • start by declaring (its type), then space and the name of the variable
    • ex: “int variable_name”

Today’s resources:

A 4-hour FaceTime with my older brother.