Data Types, Variables, and Functions – CS 101, Coding Fundamentals – Study Notes
offline

Difficulty: Beginner | Prerequisites: None

This is the foundation of everything else in CS 101. Data types, variables, and functions are the building blocks every other topic in the course depends on. If you are coming into this cold, this is the right place to start. You need to understand what kinds of values a program can work with, how it stores them, and how it packages logic into reusable pieces before any discussion of control flow or object-oriented design will make sense.


TL;DR

Programs store values in variables, and every variable has a data type that determines what kind of value it can hold (whole numbers, decimals, text, true/false, etc.). Functions let you bundle a set of instructions into a named, reusable block so you can call it whenever you need that logic instead of rewriting it.


Key Terms

Integer (int)

A whole number with no decimal point. Can be positive, negative, or zero.

Think of it as: a counting number. Ages, quantities, indices.

Float (float)

A number that includes a decimal (floating-point) component.

Think of it as: any number that needs precision beyond whole units, like 3.14 or 9.81.

String (string)

A sequence of characters, typically enclosed in quotation marks. Used for text: names, messages, labels.

In simple terms, this means: anything you would type as words or sentences in a program is a string.

Boolean (bool)

A value that can only be true or false. Used for conditions and logic.

Think of it as: a yes/no switch. Either something is the case, or it is not.

Array

A collection of multiple values stored under a single variable name, accessed by index (position).

In simple terms, this means: a numbered list where each slot holds a value and you refer to each slot by its position (starting from 0 in most languages).

Object

A complex data type that bundles related properties (data) and methods (functions) together.

Think of it as: a container that holds both the information about something and the actions it can perform. A "Person" object might store a name and age, and also know how to introduce itself.

Variable

A named storage location in memory that holds a value of a particular data type.

In simple terms, this means: a labelled box. You choose the label (the variable name), decide what kind of thing goes in it (the data type), and then put a value inside.

Function

A reusable block of code that performs a specific task. It can accept inputs (parameters), do work, and hand back a result (return value).

Think of it as: a recipe. You give it ingredients (parameters), it follows a set of steps, and it produces a dish (return value).

Parameter

A variable listed in a function's definition that acts as a placeholder for the value you will pass in when you call the function.

In simple terms, this means: the slots a function expects you to fill when you use it.

Return value

The output a function sends back to wherever it was called.

Think of it as: the answer a function hands you after it finishes its work.


Core Content

Data Types – What Values a Program Can Store

  • Every value in a program has a type. The type determines what operations you can perform on it and how the computer stores it in memory.

  • The six common data types covered in this unit:

    • int for whole numbers

    • float for decimal numbers

    • string for text

    • bool for true/false

    • array for ordered collections

    • object for bundled data and behaviour

Variables – Naming and Storing Values

  • When you declare a variable, you specify two things: the data type and the name.

  • The name should be descriptive. age is better than x; studentCount is better than n.

  • Example declarations (C++ style syntax):

int age = 25;
float pi = 3.14;
string name = "John Doe";
bool isCodingFun = true;
  • The left side tells the compiler what to expect. The right side is the value being assigned.

Functions – Packaging Reusable Logic

  • A function has four parts: a return type, a name, a parameter list, and a body.

  • The return type says what kind of value the function produces. If it produces nothing, the return type is void.

  • Example, a function that adds two integers:

int addNumbers(int num1, int num2) {
    return num1 + num2;
}
  • Example, a function that calculates the area of a circle:

float calculateCircleArea(float radius) {
    return 3.14 * radius * radius;
}
  • Functions reduce repetition. Write the logic once, then call the function by name wherever you need it.


Formulas / Diagrams

Area of a circle

A = π × r²

In C++ with a float approximation: 3.14 radius radius


Real-World Applications

Variables and data types map directly to any software you use daily. A weather app stores the temperature as a float, your username as a string, and whether notifications are enabled as a boolean. Functions are how programmers avoid copying and pasting the same logic: every button press on your phone triggers a function somewhere.


Common Misconceptions

  • Students often confuse int and float when a number looks like a whole number. 25 is an int; 25.0 is a float. The decimal point matters.

  • Students sometimes assume a string of digits, like "123", is a number. It is not. Because it is wrapped in quotes, the program treats it as text, not something you can do arithmetic with.

  • Students often think a variable name changes the behaviour of the code. It does not. int x = 5; and int age = 5; do exactly the same thing, but the second is readable.

  • A common error is forgetting the return type when writing a function. The compiler needs to know what kind of value the function hands back.


Why It Matters / Exam Flags

⚠️ You will likely be asked to identify the correct data type for a given value (e.g., "What type is 3.14?"). Know the distinctions cold.

⚠️ Function syntax, especially the order of return type, name, and parameters, is a staple of early exam questions.

⚠️ Expect a question that asks you to trace through a function call and state the return value.


Quick Self-Test

  1. True or false: "42" is an integer. ______

  1. Fill in the blank: A variable that can only hold true or false has the ______ data type.

  1. True or false: A function with a return type of void hands back a value. ______

  1. Fill in the blank: In the declaration float pi = 3.14;, the variable name is ______.

  1. True or false: An array stores exactly one value. ______

Answers: 1. False (it is a string). 2. Boolean (bool). 3. False. 4. pi. 5. False (it stores a collection of values).


Practice Q&A

Q: What is the difference between an integer and a float?

A: An integer is a whole number with no decimal component (e.g., 25). A float includes a decimal point (e.g., 3.14). They are stored differently in memory and support slightly different operations.

Q: Write a variable declaration that stores the value true indicating a student has passed.

A: bool hasPassed = true;

Q: Given the function below, what does addNumbers(3, 7) return?

int addNumbers(int num1, int num2) {
    return num1 + num2;
}

A: It returns 10.

Q: Why should variable names be descriptive?

A: Descriptive names make code easier to read, understand, and maintain. A name like studentAge immediately tells the reader what the variable holds, while x does not.

Q: Name three common data types and give an example value for each.

A: int (e.g., 42), float (e.g., 9.81), string (e.g., "hello").


Connections to Other Topics

This material connects directly to Control Flow, because conditions in if/else statements evaluate boolean expressions, and loops use integer counters. It also feeds into Object-Oriented Programming, where classes bundle variables (properties) and functions (methods) into a single structure. Understanding data types is essential before you can make sense of type errors during debugging.


Related Terms / Search Tags

data types, variables, functions, int, float, string, boolean, bool, array, object, variable declaration, return type, parameters, arguments, return value, void, C++ basics, coding fundamentals, CS 101, intro to programming, UCF Computer Science I, declaring variables, function definition, reusable code