Scholar Assistant: C7 for Beginners
This will walk you through Chapter 7 step-by-step as if you’re just starting fresh. Everything will be broken down clearly, with examples, and explained why things matter — including what to avoid.
This is going to feel like we’re building knowledge layer by layer, so even if you didn’t fully get it before, by the end, you will.
Chapter 7: How to Code Functions — Beginner-Friendly Master Guide
Why Functions Are a Big Deal
Think of a function like a mini-program inside your program.
It does one specific job, and you can call it anytime you want that job done.
Why We Use Functions:
Organize code into clean blocks
Avoid repeating the same code (DRY = Don’t Repeat Yourself)
Make testing easier
Improve readability
Collaborate better (others can work on parts)
What a Function Looks Like
Function Syntax:
return_type function_name(parameter_list) {
// body (what it does)
return something; // if not void
}
Let’s break that down:
Part | Meaning |
|---|---|
| What kind of result the function gives back ( |
| A name you choose like |
| Info you send in (optional) |
| The code the function runs |
1. Function That Does Something but Returns Nothing
void show_greeting() {
cout << "Hello, welcome!" << endl;
}
void= returns nothingYou call it like this:
show_greeting();
Use void when the job is just to do something — not to calculate and return a value.
2. Function That Takes In Information
void greet_person(string name) {
cout << "Hi, " << name << "!" << endl;
}
The parameter
string nameallows you to pass a value into the function.Call it like this:
greet_person("Alex");
This is useful when the function needs custom input each time.
3. Function That Calculates and Returns a Value
double calculate_mpg(double miles, double gallons) {
return miles / gallons;
}
Call it like this:
double mpg = calculate_mpg(300.0, 10.0);
Now mpg holds the result of 300 ÷ 10 = 30.0.
Use return values when you need the function to give something back to be used elsewhere.
Calling vs Defining a Function
Defining = writing what the function does.
Calling = using the function in your program.
Function Declaration (Prototype)
If you want to use a function before it’s defined, you have to declare it.
Example:
void show_title(); // declaration (at top)
int main() {
show_title(); // you can call it here
}
void show_title() { // actual definition (later)
cout << "My Program" << endl;
}
Declarations allow flexibility in your program’s structure.
Local vs Global Variables
Local Variables
Defined inside a function, used only there.
void say_hello() {
string name = "Sara"; // local
cout << "Hi " << name << endl;
}
Global Variables
Defined outside all functions, can be used anywhere.
string name = "World"; // global
void greet() {
cout << "Hello " << name;
}
Why NOT to use global variables often:
Anyone can change them anytime.
Leads to bugs that are hard to trace.
Makes code harder to maintain.
Exception: Global const values (constants) are okay.
Planning With a Function Hierarchy Chart
This is like a map of your program.
Example:
main
├── display_menu
└── convert_temp
├── to_celsius
└── to_fahrenheit
This helps you visualize which function calls which, like a family tree for your code.
Use hierarchy charts to organize large programs into clean parts.
Using Default Parameters
You can give a function a default value for a parameter:
double calc_tax(double amount, double rate = 0.05) {
return amount * rate;
}
Call it like:
calc_tax(100); // uses 5% rate
calc_tax(100, 0.08); // uses 8% rate
Put default parameters at the END of the list!
Don’t set default in both the declaration and definition — it causes errors.
Overloading Functions
You can have multiple functions with the same name, but different parameters.
int square(int x) {
return x * x;
}
double square(double x) {
return x * x;
}
square(3); // calls the int version
square(3.5); // calls the double version
C++ picks the right one based on what you pass in.
Can’t overload just by return type — parameters must be different.
Reference Variables (&)
By default, parameters are copies.
If you want to change the original, use a reference.
Without Reference:
void add_tax(double price) {
price += price * 0.1;
}
int main() {
double total = 100;
add_tax(total);
// total is still 100!
}
With Reference:
void add_tax(double &price) {
price += price * 0.1;
}
int main() {
double total = 100;
add_tax(total);
// total is now 110!
}
Use reference when you want to modify the original variable.
Constant Reference (const &)
Used when:
You want to pass big data (like
stringorvector) efficientlyYou don’t want the function to change it
Example:
void print_name(const string &name) {
cout << name;
}
Faster than copying, safer than allowing changes.
Header Files and Namespaces
Header Files (.h)
Used to share functions across multiple .cpp files.
In header file (temperature.h):
double to_celsius(double);
In source file (temperature.cpp):
double to_celsius(double f) {
return (f - 32) * 5 / 9;
}
In main.cpp:
#include "temperature.h"
Keep declarations and definitions separate for better organization.
Namespaces
Avoid name conflicts using namespaces.
namespace math_utils {
double square(double x) {
return x * x;
}
}
Use it like:
double area = math_utils::square(5);
Recap Summary
Concept | Why It Matters |
|---|---|
Functions | Break code into reusable chunks |
Parameters | Give info to functions |
Return Values | Get answers from functions |
Declarations | Let you call functions before defining them |
Local Variables | Safe, used only in one place |
Global Variables | Risky — avoid unless constant |
Default Params | Let you skip some arguments |
Overloading | One function name, different jobs |
Reference ( | Let function change original |
| Efficient, safe way to pass big data |
Header Files | Share functions across files |
Namespaces | Avoid name conflicts in big projects |