// lesson plan

Programming
Fundamentals

Functions in Python · Building a Calculator · C++ basics with #include & iostream · Variables & Strings

functions.py
calculator.py
hello.cpp
variables_strings.py

Use the arrow keys, the dots below, or swipe to move through the deck →

01

Python Functions

Packaging up a piece of work so you can run it by name, as many times as you like.

01.1

What is a function?

A function is a named, reusable block of code that performs a task. Instead of retyping the same steps every time, you write them once and call the function whenever you need them.

  • Defined once with def
  • Called (used) as many times as needed
  • Can accept inputs, called parameters
  • Can send back an output with return
functions.py
def greet(name):
    print("Hello, " + name + "!")

# calling the function
greet("Prince")
greet("Maria")
01.2

Anatomy of a function

def add(a, b):
    return a + b
keyworddef tells Python "a function starts here"
nameadd — how you'll call it later
parametersa, b — the inputs it expects
bodyindented lines — the work it does
returnsends a value back to whoever called it

Calling add(2, 5) runs the body with a = 2 and b = 5, then hands back 7.

01.3

A few shapes functions come in

No input, no output

def welcome():
    print("Welcome!")

Input, no output

def shout(msg):
    print(msg.upper())

Input and output

def square(n):
    return n * n

Default parameter

def power(n, exp=2):
    return n ** exp
02

Building a Simple Calculator

Putting functions to work in one small, complete program.

02.1

The process, step by step

  1. 1
    Write a function per operation
    One small function each for add, subtract, multiply, divide.
  2. 2
    Ask the user for two numbers
    Use input(), then convert the text to numbers with float().
  3. 3
    Ask which operation to run
    Take a symbol like +, -, *, or /.
  4. 4
    Branch with if / elif / else
    Match the symbol to the right function call.
  5. 5
    Print the result
    Show the answer back to the user.
  6. 6
    Loop (optional)
    Wrap it in a while loop so it runs again until the user quits.
02.2

Putting it together

calculator.py
def add(a, b): return a + b
def subtract(a, b): return a - b
def multiply(a, b): return a * b
def divide(a, b):
    if b == 0:
        return "Error: cannot divide by zero"
    return a / b

x = float(input("First number: "))
op = input("Operation (+ - * /): ")
y = float(input("Second number: "))

if op == "+":
    result = add(x, y)
elif op == "-":
    result = subtract(x, y)
elif op == "*":
    result = multiply(x, y)
elif op == "/":
    result = divide(x, y)
else:
    result = "Unknown operation"

print("Result:", result)
02.3

Why each piece matters

  • input() always returns text (a string), even if the user types a number.
  • float() converts that text into a real number you can do math with.
  • if / elif / else picks exactly one branch based on the symbol typed.
  • Each operation lives in its own function — easy to test and reuse on its own.
  • The divide function checks for zero first, so the program never crashes.
▶ running calculator.py
First number: 12
Operation (+ - * /): *
Second number: 4
Result: 48.0
03

C++ Basics

#include and iostream — how a C++ program gets its tools.

03.1

What does #include do?

#include is a preprocessor directive — an instruction handled before your code is even compiled. It copies in code from another file so you can use tools you didn't write yourself.

  • Starts with #, no semicolon at the end
  • Angle brackets <iostream> mean "look in the standard library"
  • Quotes "myfile.h" mean "look in my own project files"
  • iostream = input/output stream — gives you cin and cout
hello.cpp
#include <iostream>
using namespace std;

int main() {
    cout << "Hello, world!";
    return 0;
}
03.2

Reading the line-by-line

#include <iostream>
using namespace std;

int main() {
    cout << "Hi!";
    return 0;
}
#includepulls in the input/output toolkit
using namespace stdlets you write cout instead of std::cout
int main()every C++ program starts running here
cout <<sends text to the screen
return 0tells the system the program finished OK

cin >> works the opposite way — it reads input from the keyboard, e.g. cin >> age;

04

Variables & Strings

The basic containers every program is built from.

04.1

Variables

A variable is a named box that holds a value in memory. You give it a name once, and use that name to read or change the value later.

  • Python: no type keyword needed, just name = value
  • C++: you declare the type up front, e.g. int age = 20;
  • Names should describe what they hold: score, not x
  • Reassigning changes the value the box holds — the box itself stays the same
variables.py
# Python
age = 20
name = "Prince"
is_student = True
variables.cpp
// C++
int age = 20;
string name = "Prince";
bool isStudent = true;
04.2

Strings

A string is text — any sequence of characters wrapped in quotes. Used for names, messages, anything that isn't pure numbers to calculate with.

  • Python: single or double quotes both work, 'hi' or "hi"
  • Join strings with +, called concatenation
  • Grab a character by position: name[0]
  • C++ strings need #include <string> and the type string
strings.py
# Python
first = "Prince"
last = "Reyes"
full = first + " " + last

print(full)        # Prince Reyes
print(full[0])     # P
print(len(full))    # 12

Recap

Functions

Reusable blocks defined with def, called by name, can take input and return output.

Calculator

Small functions + input() + if/elif/else = a complete working program.

#include / iostream

Pulls in the input/output toolkit so C++ can use cin and cout.

Variables & Strings

Named boxes for values; strings are text you can join, index, and measure.