Defining and Calling Functions in C++
A C++ function defines a named operation through its return type, name, parameter list, and body.
Summary
A C++ function defines a named operation through its return type, name, parameter list, and body. Defining the function specifies its behavior; calling it executes that behavior with supplied arguments. For example, int add(int a, int b) { return a + b; } defines a function, while add(3, 4) calls it.
Parameters are named inputs in a function definition; arguments are expressions supplied at a call site. Parameter and return types establish an interface that the compiler checks.
| Parameter mechanism | Relationship to caller's variable |
|---|---|
| Pass by value: int x | Receives a copy; modifying the parameter does not directly modify the caller's variable. |
| Pass by reference: int& x | Aliases the caller's argument and permits modifying it. |
A declaration such as int add(int, int); makes a function known before a call when its definition appears later. Separating declarations and definitions supports organization across headers and source files.
A return statement ends the current function call and can provide a result. A void function produces no return value and may finish with return; or by reaching its closing brace. Parameters and variables declared inside a function have local scope and are not directly accessible outside it. Functions support independent testing and reuse.
Common Misconceptions: Defining a function does not execute it. Parameters and arguments are not interchangeable terms. Changing a value parameter does not directly change the caller's variable.
🧠 Key Concepts
- Function definition
- Function call
- Function declaration
- Parameters and arguments
- Pass by value
- Pass by reference
- Return statement
- Void functions
- Local scope
🧠 Quick Check
See what you remember from the summary.
What causes the operation defined by a C++ function to execute with supplied arguments?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Defining and Calling Functions in C++
📘 Overview A C++ function defines a named operation that runs when called with matching arguments. Its definition specifies a return type, name, parameter list, and body.
🧠 Key Idea Defining a function specifies its behavior; calling it executes that behavior using supplied arguments. Parameters receive the inputs, and a return statement can provide a result to the caller.
⚔️ Core Details: - Function definition: int add(int a, int b) { return a + b; } defines a function named add with two int parameters and an int result. - Function call: int total = add(3, 4); initializes total with the returned value 7. - A declaration such as int add(int a, int b); makes the function known before a call when its definition appears later. - Pass by value: a parameter such as int x receives a copy of the argument's value. - Pass by reference: a parameter such as int& x aliases the caller's argument and permits modification of that argument. - A return statement ends the current function call; a void function returns no value and may end with return; or by reaching its closing brace.
🎯 Why It Matters: - Functions separate a program into named operations that can be tested and reused independently. - Parameter types and return types establish an interface that the compiler checks at call sites. - Choosing value or reference parameters controls whether a function can directly change the caller's variables. - Separate declarations and definitions support organizing larger programs across headers and source files.
🧠 Quick Recall: - Parameter - Named input declared in a function's parameter list, such as a in int add(int a, int b). - Argument - Expression supplied at a call site, such as 3 in add(3, 4). - Function declaration - Introduces a function's name and type without requiring its body; example: int add(int, int);. - void - Return type indicating that a function produces no return value. - Local scope - Parameters and variables declared inside a function are not directly accessible outside that function.
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Science notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.