Statement = Atomic unit of code
Statement \(\approx\) a single command/line of code
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
pi=3.1415926535*2;
python/matlab/R -> implicit declaration
julia -> optional declarations
<stdio.h>
include {
double pi; // declaration
= 3.15926535; // Assigment
pi return 2*.1145926535; // execution
(%f, pi);
printf}
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) {
= in*in
outreturn out
}
{
int A = 3;
=square(A);
B}
octave/matlab
function out=square(in)
=in*in;
outend
disp(square(2));
Binding
Assignment
Scope
Namespace
Addres space
example 1
One variable, two different assignments, single namespace
{
int A=3;
=4;
A}
example 2
Two different variables with the same bindings and assignment, two namespaces
int return_A (){
int A = 3;
return A;
}
{
int A = 3;
}
example 3
One variable, two different assignments
=uint8(3);
A=uint8(4); A
Two variables…
=uint8(3);
A=3; A
function out=add_three(in)
A=3;
out=in+A;
end
int A = 10;
C=add_three(A);
int add_three (in) {
=3;
Areturn A+3;
}
int A = 10;
=add_three(A); C
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
You can only
Create statements
(Declare, Execute, and/or Assign )
with statement elements
(Variables, Routines, or Operations)
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
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
Aggregate types
Two parts:
String
has pointer to "string" of characters in heap
Arrays
has pointer to pointers
example
#include <stdio.h>
int main() {
char *myStr = "This is a string";
("%s\n", myStr );
printf
}
// * indicates a pointer, char defines the size of data being pointed to
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
example
int main() {
char *myStr = "This is a string";
("%s\n", myStr );
printf(myStr)
free}
Abstracted away from memory
Julia has these features optionally