Functions
By the end of this lesson you'll be able to write your own C++ functions with parameters and return values, choose between passing by value, reference, and const reference, give parameters default values, and overload a function name — the skills that turn long scripts into clean, reusable code.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
Part of the free C++ course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
A function is a recipe card . You write the steps once — "make a coffee" — and from then on you just say "make a coffee" instead of re-listing every step. The parameters are the ingredients you hand over (the bean, the cup size); the return value is the finished coffee handed back. Passing by value is like giving the chef a photocopy of your shopping list — they can scribble on it and your original is untouched. Passing by reference is handing over the original list itself, so any note they make is on your copy. That single distinction explains most "why didn't my value change?" confusion in C++.
1. Declaring & Defining Functions
A function has a return type (what it hands back), a name , a list of parameters (its inputs), and a body . The shape is always the same: returnType name(parameters) {' ... return value; '} . Use void when the function just does something and hands nothing back. Read this worked example, run it, then you'll write your own.
Your turn. The program below is almost complete — fill in the two blanks marked ___ using the hints, then run it.
2. By Value vs by Reference vs const Reference
How you write a parameter decides whether the function can change the caller's data. By value ( int x ) gives the function a private copy — changes stay inside. By reference ( int&x ) lets the function edit the caller's actual variable. By const reference ( const std::string& ) gives read-only access with no copy , which is the efficient way to pass big objects like strings and vectors you only need to read.
Now you try. Right now addBonus takes a copy, so the caller's score never changes. Add the one character that turns the parameter into a reference:
3. Default Arguments & Overloading
Default arguments let a parameter fall back to a value when the caller leaves it out — handy for optional settings. They must be the rightmost parameters. Overloading means giving several functions the same name but different parameter lists; the compiler picks the right one from the argument types. Note: overloads must differ in their parameters, not just the return type.
C++ reads a file top to bottom, so a function must be known before you call it. A declaration (or prototype ) announces a function's signature without its body — note the semicolon. You can declare it up top and write the full definition later, even below main() .
inline is a hint that the compiler may paste a small function's body straight into the call site instead of making a real call. Modern compilers decide this for you, so you rarely add it for speed. Its real day-to-day use is letting a function be defined in a header that many files include without a "multiple definition" linker error.
No blanks this time — just a brief and an outline to keep you on track. Write the two functions yourself (one uses a default argument, the other combines defaults with multiple inputs), then check your output against the comments.
Practice quiz
What return type means a function hands nothing back?
- null
- none
- void
- empty
Answer: void. void means the function returns nothing — it just does something.
If you pass an int by value and the function changes it, what happens to the caller's variable?
- It stays unchanged (the function got a copy)
- It changes too
- It becomes 0
- The program won't compile
Answer: It stays unchanged (the function got a copy). Pass by value gives the function a copy, so the caller's variable is untouched.
How do you let a function modify the caller's variable?
- Pass by value
- Use const
- Return void
- Pass by reference, e.g. void f(int &x)
Answer: Pass by reference, e.g. void f(int &x). A non-const reference (int &x) lets the function edit the caller's actual variable.
Why pass a large object as 'const std::string&'?
- It is required for all strings
- No copy is made and it is read-only
- It makes the function return faster
- It converts the string to a number
Answer: No copy is made and it is read-only. A const reference avoids copying the big object and guarantees the function won't change it.
Where must default arguments appear in a parameter list?
- Rightmost
- Leftmost
- Anywhere
- Only in the middle
Answer: Rightmost. Defaults must be the rightmost parameters so the compiler can match arguments left to right.
What must differ between overloaded functions?
- Only the return type
- The function name
- The parameter list (types or number)
- Nothing
Answer: The parameter list (types or number). Overloads must differ by their parameter list, not just the return type.
What is a function declaration (prototype)?
- The function body
- The signature ending in a semicolon, with no body
- A call to the function
- A comment describing it
Answer: The signature ending in a semicolon, with no body. A declaration/prototype gives the name, return type, and parameter types, ending with a semicolon.
Given 'double area(double length, double width = 1.0)', what does area(5.0) return?
- 0
- 1
- 6
- 5
Answer: 5. width defaults to 1.0, so 5.0 * 1.0 = 5.
What is the main practical use of 'inline' in modern C++?
- Guaranteeing a speed boost
- Allowing a function definition in a header without multiple-definition errors
- Making a function private
- Preventing recursion
Answer: Allowing a function definition in a header without multiple-definition errors. inline mainly lets a function be defined in a header included by many files without linker errors.
What does 'add(15, 27)' return for 'int add(int a, int b){ return a + b; }'?
- 1527
- 27
- 42
- 15
Answer: 42. It returns the sum a + b, which is 42.