1. FORTRAN was influential for science and engineering, COBOL for business data processing, LISP for logic and AI, and BASIC as a simple language.
2. Programming languages have code/source code, syntax, output, and a console. Compiling translates source code into machine code, while interpreting runs source code directly.
3. C was developed in 1973 for UNIX and is now widely used. It has a simple core and uses pointers for low-level memory access. Python allows multiple paradigms and uses dynamic typing and garbage collection.
3. Programming basics
code or source code: The
sequence of instructions
in a program.
syntax: The set of legal
structures and commands
that can be used in a
particular programming
language.
output: The messages
printed to the user by a
program.
console: The text box
onto which output is
printed.
3
4. Compiling and interpreting
Many languages require you to
compile (translate) your
program into a form that the
machine understands.
Python is instead directly
interpreted into machine
instructions.
4
compile execute
output
source code
Hello.java
byte code
Hello.class
interpret
output
source code
Hello.py
5. Dimensions of aPL
• Functional
• Treatscomputation as the evaluation ofmathematical functions
(e.g. Lisp, Scheme, Haskell, etc.)
• Imperative
• describes computation in terms of statements that changea program
state (e.g. FORTRAN, BASIC, Pascal, C, etc. )
• Logical (declarative)
• expresses the logic of a computation withoutdescribingits control flow
(e.g. Prolog)
• Object oriented
• uses "objects"– data structures consistingof data fields and methods
together with their interactions– to design applications and computer
programs (e.g. C++, Java, C#, Python, etc.)
6. Slide credit: Thomas J. Cortina
C (1973)
• Developed by Ken Thompson and Dennis Ritchie at
AT&T Bell Labs for use on the UNIXoperating system.
– now used on practicallyevery operatingsystem
– popular languagefor writing system software
• Features:
– An extremely simple core language, with non-essential
functionality provided by a standardizedsetof library routines.
– Low-level access to computer memory via the use of pointers.
• C ancestors: C++, C#, Java
9
7. Slide credit: Thomas J. Cortina
Python
• Created by Guido van Rossum in the late 1980s
• Allows programming in multiple paradigms: object-
oriented, structured,functional
• Uses dynamic typing and garbage collection
8. 8
Building a simple program in
Python (as compared to C)
• Compilers versusinterpreters
• Variable declarations
• Whitespace
• The printf() function
• Functions
9. 9
Compilers versus interpreters
• One major difference between C and Python is how the
programs written in these two languages are executed.
• With C programs, you usually use a compiler when you
are ready to see a C program execute.
• By contrast, with Python, you typically use an interpreter.
10. 10
Compilers versus interpreters
• An interpreter reads the user-written programand
performs it directly.
• A compiler generates a file containing the translation
of the program into the machine's native code.
– The compiler does not actually execute the program!
– Instead, you first execute the compiler to create a native
executable, and then you execute the generatedexecutable.
11. 11
Variable declarations
• C requires variable declarations, informing the compiler
about the variable before the variable is actuallyused.
• In C, the variable declaration defines the variable'stype.
• No such thing in Python!
12. 20
Declaring a Variable
• Declaring a variable is simpleenough.
• You enter the variable's type, somewhitespace,
the variable's name, and a semicolon:
double x;
• Value assignment is similar toPython:
x=3;
• x will actually hold the floating-pointvalue 3.0 rather than the
integer3.
• However, once you declare a variable to be of a particular
type, you cannot change itstype!
13. 13
Declaring a Variable
• In C, variable declarations belong at the top of the
function in which they areused.
• If you forget to declare a variable, the compiler will
refuse to compile theprogram:
– A variable is used but is notdeclared.
• To a Python programmer, it seems a pain to have to
include these variable declarations in a program, though
this gets easier with morepractice.
14. 14
Whitespace
• In Python, whitespace characters like tabs andnewlines
are important:
– You separate your statements by placingthem on separate
lines, and you indicate the extent of a block using
indentation.
– like the body of a while or if statement
• C does not use whitespace except for separatingwords.
• Most statements are terminated with a semicolon ';', and
blocks of statementsare indicated using a set of braces, '{'
and '}'.
15. Whitespace
15
C fragment
disc = b * b - 4 * a * c;
if (disc < 0)
{
num_sol = 0;
}
else
{
t0 = -b / a;
if (disc == 0)
{
num_sol = 1;
sol0 = t0 / 2;
}
else
{
num_sol = 2;
t1 = sqrt(disc) / a;
sol0 = (t0 + t1) / 2;
sol1 = (t0 - t1) / 2;
}
}
Python equivalent
disc = b * b - 4 * a * c
if disc < 0:
num_sol = 0
else:
t0 = -b
if disc
/ a
== 0:
num_sol = 1
sol0 = t0 / 2
else:
num_sol = 2
**
t1 = disc
sol0
sol1
= (t0 +
= (t0 -
0.5 / a
t1) / 2
t1) / 2
16. 16
Whitespace
• As said, whitespace is insignificant in C.
• The computer would be just as happy if the previous code
fragment is written as follows:
disc=b*b-4*a*c;if(disc<0){
num_sol=0;}else{t0=-b/a;if(
disc==0){num_sol=1;sol0=t0/2
;}else{num_sol=2;t1=sqrt(disc/a;
sol0=(t0+t1)/2;sol1=(t0-t1)/2;}}
• However, do not write your programs like this!
17. 30
Functions
• Unlike Python, all C code must be nested within functions,
and functions cannot be nested withineach other.
• A C program's overall structure is typicallyvery
straightforward.
• It is a list of function definitions, one after another, each
containing a list of statements to be executed when the
function iscalled.
18. 18
Functions
• A C function is defined bynamingthe return type, followed bythe function
name, followed bya set of parentheses listing the parameters.
• Each parameter is described by includingthe type of the parameter and the
parametername.
• Here's a simple example of a function definition:
float expon(float b, int e)
{
if (e == 0)
{
return 1.0;
}
else
{
return b * expon(b, e - 1);
}
}
This is a function named
expon, which takes two
arguments, first a floating point
number and next an integer,
and returns a floating point
number.
19. 19
Functions
• If you have a function that does not have any useful
return value, then you'd use void as the return type.
• Programs have one special function named main, whose
return type is aninteger.
• This function is the “starting point” for the program:
– The computer essentially calls the program's mainfunction
when it wants to execute theprogram.
– The integer return value is largely meaningless; we'll
always return 0 rather than worrying about howthe return
value might beused.
20. Functions
20
C program
int gcd(int a, int b)
{
if (b == 0)
{
return a;
}
else
{
return gcd(b, a % b);
}
}
int main()
{
printf("GCD: %dn“, gcd(24,40));
return 0;
}
Python program
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
print("GCD: " + str(gcd(24, 40)))