Programming Languages - Baldwin Wallace University

Download Report

Transcript Programming Languages - Baldwin Wallace University

PROGRAMMING
LANGUAGES
CSC 180 Lecutre 9
Dr. Adam P. Anthony
Overview




History of Programming Languages
Traditional Programming Concepts
Procedures and Functions
Compiler Basics
Generations of programming
languages
6-3
Second-generation:
Assembly language

A mnemonic system for representing machine
instructions
 Mnemonic
names for op-codes
 Identifiers: Descriptive names for memory locations,
chosen by the programmer
6-4
Assembly Language Characteristics



One-to-one correspondence between machine
instructions and assembly instructions
Inherently machine-dependent
Converted to machine language by a program
called an assembler
6-5
Program Example
Machine language
156C
166D
5056
30CE
C000
Assembly language
LD R5, Price
LD R6, ShippingCharge
ADDI R0, R5 R6
ST R0, TotalCost
HLT
6-6
Historical Progress


Not having to use binary code to program
computers was a huge step!
Assembly was created for convenience
 An
assembler is a very simple program: 1-1 translation
of words to binary codes

But programmers still have to think like the machine
 New
‘words’ can’t be made up without writing a new
assembler (programs can’t self-abstract)
Third Generation Languages

Uses high-level primitives
 Similar




to our pseudocode in Chapter 5
Machine independent (mostly)
Examples: FORTRAN, COBOL
Each primitive corresponds to a sequence of
machine language instructions
Converted to machine language by a program
called a compiler
6-8
Building Language Using Abstraction
COBOL:
assign TotalCost the value Price + ShippingCharge
LD R5, Price
LD R6, ShippingCharge
ADDI R0, R5 R6
ST R0, TotalCost
Assembly language
LD R5, Price
LD R6, ShippingCharge
ADDI R0, R5 R6
ST R0, TotalCost
HLT
Standing on the Shoulders of Giants

A command from one generation is (technically
speaking) just a combination of simpler commands from
an earlier generation


“One Line of code” becomes more and more powerful
C vs Python to make a list of the first 5 perfect squares:
C
int squares[5];
for( int i = 0; i<6; i++){
squares[i] = (i+1)*(i+1)
}

Python is written in C!
Python
squares = [X*X for x in range(1,6)]
The evolution of programming
paradigms
6-11
The composition of a typical imperative
program or program unit
6-12
Base Data Types




Integer: Whole numbers
Real (float): Numbers with fractions
Character: Symbols
Boolean: True/false
6-13
Variable Declarations

float
Length, Width;
int
Price, Total, Tax;
char
Symbol;
Most languages require the type of the variable to
be declared
 Length
can’t have the value ‘a’ and Symbol can’t have
the value 5.4

Some languages (e.g. Scratch) don’t require type
declarations
 But
all variables have a type, determined by its value
6-14
Composite Data Types
float Grades[18]; //holds 18 floats
char name[4] = “adam”; //C version
string name = “adam”; //C++ version

Arrays (lists)
 Most

lists allow only one ‘type’ of data to be stored
Strings (lists of characters)
 In
most modern languages ‘String’ is a type by itself
Literals, Constants and Variables

Literal


Constant


Hard to change,
understand
Used for values
that shouldn’t
change
Variable

Used for values
that will change
as the program
executes
int sum = 0
while x < 21
sum = sum + grade[x]
x = x + 1
average = sum/21
const int number_students = 21
int sum = 0
while x < number_students
sum = sum + grade[x]
x = x + 1
average = sum/number_students
int number_students = 21
int sum = 0
while x < number_students
sum = sum + grade[x]
x = x + 1
number_students = 5
average = sum/ number_students
A constant is the
best choice here!
Procedures

A procedure is a part of code that has a name and
can be re-used
 Also,
you can give optional input values so that it will
do something different depending on what you offer!
void demo(int val){
val = val + 1
print(val)
}
Procedural Units


We’ve already learned what a procedure is from CH 5
In programming languages, we have two different ways of
creating variables:



There’s also a bit more about parameters:




Global: every procedure can use and modify
Local: declared inside the procedure, can only be seen and
modified by the procedure itself
Formal: definition of what’s required/expected
Actual Parameters: value of the data provided as parameters
Vending machine example
Procedures versus Functions:

Procedures don’t have any output, functions do
6-21
The flow of control involving a
procedure/function
6-22
The procedure ProjectPopulation written
in the programming language C
•Procedures don’t produce any
output, so what are they good
for?
•A procedure is only as good as
its side effect
6-23
Running Example
Main Program
Procedure
main(){
int x = 5
demo(x)
print(demo)
}
Actual Parameter: Filling
void demo(int val){
val = val + 1
}
in the ‘hypothetical’ value
with a real one. Replace
‘val’ on the right with
whatever value is
provided here
Formal Parameter: a
hypothetical ‘placeholder’
for the actual data that
lets us describe, in
general, what happens to
the data
Executing the
procedure Demo
and passing
parameters by
value (copy)
Executing the
procedure Demo
and passing
parameters by
reference (no
copy)
The function CylinderVolume written in
the programming language C
6-27
The Compiler Translation Process
6-28

How we go from code to program:
•Look for basic coding
errors (typos)
•Make sure language
statements are valid
•Check Types
•Break complex statements
down into smaller, more
manageable parts
•Convert smaller parts
into assembly code
•Convert assembly code
into machine language
Scratch as a Programming Language

Scratch has all 4 basic data types, but:
Type declarations not required for variables
 Strange side effect: Strings and letters treated as 0 in
mathematical calculations


No Functions, only Procedures
Nothing can be returned by a “when I receive…” block
 “Broadcast <message> and wait” simulates a basic
procedure call



Only one-dimensional arrays
“Semi-local” variables provide two choices
Usable by all procedures for a single sprite (semi-local)
 Usable by all procedures for all sprites (global)

Scratch as a Programming Language




Scratch has some limitations (no functions, no 2D
arrays!)
Scratch also has advanced features (graphics,
audio, parallel execution)
Despite the limitations, people have taken
advantage of the advanced features to produce
very complex programs
Scratch’s (more or less) unique quality: the shapes
ARE the language!
Conclusion


Programming language: built upon a less complex
formalism
Important language concepts:
 Variable
types and declarations
 Arrays (multi-dimensional)
 Procedures and Functions


Compilers: translate programming language back
into machine language
Scratch: a legitimate programming language!