CS253: Software Development with C++

Fall 2020

Programming Paradigms

Show Lecture.ProgrammingParadigms as a slide show.

CS253 Programming Paradigms

Overview

Imperative programming

Declarative programming

Functional programming example

Here are parallel JavaScript implementations, taken from Wikipedia:

Traditional imperative look:

const numList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let result = 0;
for (let i = 0; i < numList.length; i++) {
  if (numList[i] % 2 === 0) {
    result += numList[i] * 10;
  }
}

Functional style:

const result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
               .filter(n => n % 2 === 0)
               .map(a => a * 10)
               .reduce((a, b) => a + b);

Sure, down at the machine instruction level, it’s all imperative. However, the programmer doesn’t have to think that way.

C++ declarative programming

Here are two ways to add some numbers in C++:

vector<int> v = {11,22,33,44,55};
int sum = 0;
// imperative style
for (size_t i=0; i<v.size(); i++)
    sum += v[i];
cout << sum;
165
vector<int> v = {11,22,33,44,55};
int sum = 0;
// declarative style
for (int n : v)
    sum += n;
cout << sum;
165

It might even be the same instructions both times. I don’t care. Certainly, there are still imperative aspects.

The point is that, in the second program, I don’t have to think about the mechanics of “start at element 0, increment by 1 until I get to the last element of the array”. I just think “do this for each element”.

Event-driven programming

Clock example

An imperative clock program might work like this pseudocode:

int main() {
    while (true) {
        sleepms(1000);
        display_time();
    }
}

Or, it might be event-driven, where the event is a clock triggering:

int main() {
    schedule_periodic_callback(1000, &display_time);
    do_nothing_but_service_interrupts();
}

The event-driven code could be easily modified to handle other event-driven tasks: keyboard input, mouse clicks, etc.