Functional vs Imperative Programming

Paradigm Similarities vs. Differences
Functional It is based on the concept of functions as primary building blocks of a program. Functions are designed to be “pure”, which means return values only depend on argument values.
Imperative It accomplishes a task based on sequence of steps that the computer should follow to reach a desired outcome. Side effects are used (unlike the functional programming paradigm)

I/O and Output Side Effects

Input: touch screen, voice, camera

Output: screen (display), sounds

(In this course, we only use simple text-based I/O)

Some special cases to remember for the format specifiers

int main() {
	pritnf("%%"); // % sign
	printf("\\\\"); // backslash
	printf("\\""); // quotation mark
}

<aside> 💡 In general, a programming side effect is when the state of something “changes”.

ChatGPT’s explanation: In programming, a side effect is any change that a function or expression makes outside of its own scope. It means that when a function is called and it modifies some external state or resource, it has a side effect (ex. printing something to the console, changing a global variable, modifying a file, etc.)

</aside>

Documenting side effects

// noisy_sqr(n) computes n's square
// effects: produces output
int noisy_sqr(int n) {
	printf("Yo! I'm squaring %d!\\n", n);
	return n * n;
}

Three types of C statements:

Compound statements (blocks) {} ⇒ a sequence of statements Expression statements ⇒ for generating side effects Control flow statements ⇒ Control the order in which other statements are executed (return if, else)


Mutating Side Effects

Two more side effects: