Programming Language and Paradigms
Programming Language and Paradigms
Paradigms
Introduction
Topics
2
PROG0101 Fundamentals of Programming
Computer Program
3
PROG0101 Fundamentals of Programming
Programming Language
• A vocabulary and set of grammatical rules (syntax)
for instructing a computer to perform specific tasks.
• Programming languages can be used to create co
mputer programs.
• The term programming language usually refers to
high-level languages, such as BASIC, C, C++, CO
BOL, FORTRAN, Ada, and Pascal.
4
A Brief History of
Programming Languages
Brief history: Machine language
• Machine language – the sequence of bits that
directly controls a processor
• Add, compare, move data from one place to another,
and so forth at appropriate times
55 89 e5 53 83 ec 04 83 e4 f0 e8 31 00 00 00 89 c3 e8 2a 00
00 00 39 c3 74 10 8d b6 00 00 00 00 39 c3 7e 13 29 c3 39 c3
75 f6 89 1c 24 e8 6e 00 00 00 8b 5d fc c9 c3 29 d8 eb eb 90
6
Brief history: Assembly language
• Assembly language – expressed with mnemonic
abbreviations, a less error-prone notation
pushl % ebp jle D
movl %esp, %ebp subl %eax, %ebx
pushl %ebx B: cmpl %eax, %ebx
subl $4, %esp jne A
andl $-16, %esp C: movl %ebx, (%esp)
call getint call put int
movl %eax, %ebx movl -4(%ebp), %ebx
call getint leave
cmpl %eax, %ebx ret
je C D: subl %ebx, %eax
A: cmpl %eax, %ebx jmp B
8
Brief history: high-level language
• Fortran – first high-level language in the mid-
1950s
• Machine-independent language
• Compiler – system program for translating
from high-level language to assembly or
machine language
• Not one-to-one correspondence between
source and target operations.
9
Why Study Programming
Languages?
Why study programming languages?
• Understand obscure features
• Choose among alternative ways to express things
• Simulate useful features in languages that lack
them
• Make it easier to learn new languages
• Help you choose a language
11
The Art of Language Design
What is a language for?
13
Why are there so many?
• Evolution - learn better ways of doing things over time
– goto-based control flow (Fortran)
– structured programming (Pascal, C)
– object-oriented structure (C++, Java)
• Special purpose
– symbolic data
– character strings
– low-level system programming
– reasoning, logical relation
• Socio-economic factors - proprietary interests, commercial advantage
• Personal preference - diverse ideas about what is pleasant to use
• Special hardware
14
What makes a language successful?
• Expressive power – easy to express things, to use once fluent (C, APL,
Algol-68, Perl)
• Ease of use for novice – easy to learn (BASIC, Pascal, LOGO)
• Ease of implementation – (BASIC, Forth)
• Standardization – (C, Java)
• Open source - wide dissemination without cost (Pascal, Java)
• Excellent compilers – possible to compile to very good (fast/small)
code (Fortran)
• Patronage - backing of a powerful sponsor (COBOL, PL/1, Ada, Visual
Basic)
15
Language Evaluation Criteria
Language Evaluation Criteria
• Readability: the ease with which
programs can be read and understood
• Writability: the ease with which a
language can be used to create programs
• Reliability: conformance to specifications
(i.e., performs to its specifications under
all conditions)
17
Evaluation Criteria: Others
• Cost
– the ultimate total cost
• Portability
– the ease with which programs can be moved from one
implementation to another
• Generality
– the applicability to a wide range of applications
• Well-definedness
– the completeness and precision of the language’s official
definition
18
Programming Paradigms
• Imperative
– Central features are variables, assignment statements, and iteration
– Examples: C, Pascal
• Object-oriented
– Data abstraction (Encapsulate data objects with processing) ,
inheritance, dynamic type binding
– Examples: Java, C++
• Functional
– Main means of making computations is by applying functions to
given parameters
– Examples: LISP, Scheme
• Logic
– Rule-based (rules are specified in no particular order)
– Example: Prolog
• Markup
– New; not a programming per se, but used to specify the layout of
information in Web documents
– Examples: XHTML, XML
19
Programming Language
Paradigms
IMPERATIVE PROGRAMMING PARADIGM
Imperative programming is a programming paradigm that uses
statements that change a program's state.
An imperative program consists of commands for the computer to
perform.
Imperative programs describe the details of HOW the results are to
be obtained.
HOW means describing the Inputs and describing
how the Outputs are produced.
Examples are: C, C++, Java, PHP, Python, Ruby etc.
DECLARATIVE PROGRAMMING PARADIGM
gcd(A,B,G) :- A = B, G=A.
gcd(A,B,G) :- A > B, C is A-B, gcd(C,B,G).
gcd(A,B,G) :- B > A, C is B-A,
gcd(C,A,G). %Prolog