Theory Part 3 - Code Anatomy

Dave White

Code Anatomy

Statement = Atomic unit of code
Statement \(\approx\) a single command/line of code

Types

Declarations - create a label, assigning a type
Executions - simplify/do
Assignments - group/label things

int A = 8;
int A;
A=8;

A='this my name'
B=8 float

Octave/Matlab example

pi=3.1415926535*2;

python/matlab/R -> implicit declaration
julia -> optional declarations

C Example

include <stdio.h>
{
    double pi; // declaration
    pi = 3.15926535; // Assigment
    return 2*.1145926535; // execution
    printf(%f, pi);
}

Statement Elements

Variables - labeled/grouped data
Operations - interactions between variables
Routines - labeled/grouped code

function B = exponent(A,n)
B=A.n
B=sqrt(B )
end

Hierarchy
Operations act on variables
Routines act on variables and Operations

examples

int square (in) {
    out= in*in
    return out
}

{
    int A = 3;
    B=square(A);
}

octave/matlab


function out=square(in)
    out=in*in;
end
disp(square(2));

Element properties

Binding
Assignment
Scope
Namespace
Addres space

Examples

  1. example 1

    One variable, two different assignments, single namespace

    {
      int A=3;
      A=4;
    }
  2. example 2

    Two different variables with the same bindings and assignment, two namespaces

    int return_A (){
      int A = 3;
      return A;
    }
    
    {
      int A = 3;
    }
  3. example 3

    One variable, two different assignments

    A=uint8(3);
    A=uint8(4);

    Two variables…

    A=uint8(3);
    A=3;

octave/matlab

function out=add_three(in)
    A=3;
    out=in+A;
end

int A = 10;
C=add_three(A);

C examples

int add_three (in) {
    A=3;
    return A+3;
}

int A = 10;
C=add_three(A);

Summary

You can only
create statements that
declare, execute, and/or assign
using
variables, routines, and/or operations
which each have their own
Binding, Assignment, Scope, namespace

pointers
Aliases/References
stack/heap

C basics

You can only
Create statements
(Declare, Execute, and/or Assign )
with statement elements
(Variables, Routines, or Operations)

Variables

Declaration

Declaration/type assignment/memory allocation

I need to create a variable
Where should I put it?
I need to find it again
How much space Is it going to take up?
I don't want any of it to get overwritten

creating a prototype

  1. Primitive C types

    Assigned to stack

    boolean/logical
    integer (signed/unsigned)

    float (signed/unsigned)

    char
    void - an empty type
    pointer - points to an address to somewhere else in memory
    reference - like a constant pointer, cannot be reassigned

  2. Aggregate types

    Two parts:

    1. A variable in stack with a pointer to
    2. data stored in heap

    String
    has pointer to "string" of characters in heap
    Arrays
    has pointer to pointers

    1. example

      #include <stdio.h>
      
      int main() {
          char *myStr = "This is a string";
          printf("%s\n", myStr );
      
      }
      // * indicates a pointer, char defines the size of data being pointed to
  3. Deletion

    Aka garbage-collection (GC)

    Stack variables automatically gets deleted at end of their scope
    e.g. if variable was declared in a a routine, will be deleted at end of routine
    Heap variables do not

    1. example

      int main() {
          char *myStr = "This is a string";
          printf("%s\n", myStr );
          free(myStr)
      }

How do high level languages compare?

Abstracted away from memory

Julia has these features optionally

General rules for variables