🌍 C++ Programming · subject

C++ Programming Functions Syllabus

The Functions syllabus for C++ Programming in full: 2 chapters and 7 topics, every one listed below, plus 50 Functions flashcards with answers.

2Chapters
7Topics
50Flashcards

Functions syllabus: all 2 chapters

Expand any chapter to see its topics and sub-topics.

  1. Function Basics

    3 topics
    • Function Declaration and Definition
    • Function Parameters and Arguments
    • Return Types
  2. Advanced Function Concepts

    4 topics
    • Inline Functions
    • Function Overloading
    • Default Arguments
    • Recursion

C++ Programming Functions flashcards — 50 questions with answers

  1. What is the difference between a function declaration and a function definition in C++?

    A declaration (function prototype) specifies the function's name, return type, and parameter types but has no body, e.g. int add(int, int); A definition additionally provides the body containing the executable statements, e.g. int add(int a, int b) { return a + b; }

  2. What three parts make up a function's signature in C++?

    The function name and the number, types, and order of its parameters. The return type is NOT part of the signature (which is why you cannot overload on return type alone).

  3. What is a function prototype and why is it used?

    A prototype is a declaration that tells the compiler a function's name, return type, and parameter types before its use. It lets you call a function before its full definition appears and enables type checking of arguments.

  4. In C++, what does a function declared with an empty parameter list, e.g. void f();, mean compared to C?

    In C++, void f(); means the function takes no parameters (equivalent to void f(void);). In C, an empty parameter list means an unspecified number of arguments.

  5. What is the difference between a parameter and an argument?

    A parameter (formal parameter) is the variable named in the function definition. An argument (actual parameter) is the concrete value or expression passed to the function when it is called.

  6. Describe pass-by-value in C++ and its effect on the caller's variable.

    The function receives a copy of the argument's value. Modifications to the parameter inside the function do NOT affect the original variable in the caller.

  7. Describe pass-by-reference in C++ and give its syntax.

    The parameter is declared as a reference, e.g. void inc(int& x). It becomes an alias for the caller's variable, so changes inside the function DO affect the original argument. No copy is made.

  8. What is pass-by-pointer and how does it differ from pass-by-reference?

    Pass-by-pointer passes the address of a variable, e.g. void inc(int* x), and the function dereferences it (*x). Unlike a reference, a pointer can be null and can be reassigned to point elsewhere; a reference must bind on initialization and cannot be rebound.

  9. Why use a const reference parameter, e.g. void print(const std::string& s)?

    It avoids the cost of copying a large object (efficiency of pass-by-reference) while guaranteeing the function cannot modify the caller's argument (safety of pass-by-value).

  10. When an array is passed to a function in C++, what is actually passed?

    The array decays to a pointer to its first element. The function receives the address, not a copy, and the array's size information is lost (which is why size is usually passed separately).

  11. What is the purpose of a function's return type, and what does void indicate?

    The return type specifies the type of value the function sends back to the caller via a return statement. A return type of void indicates the function returns no value.

  12. What happens in a non-void function if control reaches the end without a return statement?

    It results in undefined behavior. The function is required to return a value of its declared type on all paths (except main, which implicitly returns 0).

  13. Can a C++ function return a reference? Give one caution.

    Yes, e.g. int& at(int i). Caution: never return a reference (or pointer) to a local automatic variable, because that variable is destroyed when the function returns, leaving a dangling reference.

  14. What does the auto return type combined with trailing return syntax look like, e.g. for adding two values?

    auto add(int a, int b) -> int { return a + b; } Here auto with -> specifies the return type after the parameter list; in C++14 auto add(int a, int b) can deduce the return type automatically.

  15. What is an inline function in C++ and what does the inline keyword request?

    An inline function is one where the inline keyword requests the compiler to replace the function call with the function body at the call site, potentially eliminating call overhead. It is a request/hint, not a command.

  16. State two benefits and two drawbacks of inline functions.

    Benefits: eliminates function-call overhead (stack setup, jump) and can enable further optimization. Drawbacks: can increase code size (code bloat) if the function is large or called many times, and may reduce instruction-cache performance.

  17. Besides speed, what important role does the inline keyword play with respect to the One Definition Rule?

    An inline function may be defined identically in multiple translation units (e.g. in a header included by many files) without violating the One Definition Rule; the linker merges the definitions into one.

  18. Are member functions defined inside a class body inline by default?

    Yes. A member function whose body is written inside the class definition is implicitly treated as inline.

  19. Give two reasons a compiler may refuse to actually inline a function marked inline.

    Common reasons: the function is too large or complex, it is recursive, it contains loops, it takes its own address, or contains a static local variable. The compiler is free to ignore the inline request.

  20. What is function overloading in C++?

    Defining multiple functions with the same name in the same scope but with different parameter lists (different number, types, or order of parameters). The compiler selects the correct one based on the arguments.

Show the other 30 Functions flashcards
  1. On what parameter differences can functions be overloaded?

    On the number of parameters, the types of parameters, and the order of parameter types. They cannot be overloaded solely on the basis of differing return types or by making a parameter const-by-value.

  2. Why can't two functions be overloaded when they differ only in return type?

    Because the return type is not part of the function signature. The compiler resolves overloads using the arguments at the call site, which do not convey the expected return type, so it could not disambiguate the calls.

  3. What is overload resolution?

    The compile-time process by which the compiler examines the arguments of a call and selects the best-matching overloaded function, considering exact matches, promotions, standard conversions, and user-defined conversions.

  4. What is an ambiguous call in overload resolution, and what is the result?

    When two or more overloads are equally good matches for the given arguments (no single best match), the call is ambiguous. The compiler reports a compile-time error.

  5. Can const-ness of a reference/pointer parameter be used to overload functions? Give an example.

    Yes. void f(int&) and void f(const int&) are distinct overloads, as are void f(int*) and void f(const int*). A const lvalue argument selects the const version. (Note: top-level const on by-value parameters, like int vs const int, does NOT distinguish overloads.)

  6. What is a default argument in C++?

    A value specified in the function declaration that is automatically used for a parameter when the caller omits the corresponding argument, e.g. void greet(std::string name = "World");

  7. What is the rule about the position of default arguments in a parameter list?

    Default arguments must be the trailing (rightmost) parameters. Once a parameter has a default, all parameters to its right must also have defaults.

  8. Where should default arguments be specified when a function is both declared and defined separately?

    They should be given in the declaration (typically in the header), not repeated in the definition. Specifying the same default in both places is an error; the caller only sees the declaration.

  9. How can default arguments sometimes replace function overloading?

    A single function with default arguments can serve callers who pass different numbers of arguments, e.g. int box(int l, int w = 1, int h = 1); handles box(l), box(l,w), and box(l,w,h) without writing three overloads.

  10. What is a potential conflict between default arguments and function overloading?

    They can create ambiguity: if an overload can be called with fewer arguments due to defaults, and another overload matches that same call, the compiler cannot decide, producing an ambiguous-call error.

  11. Define recursion in the context of functions.

    Recursion is a technique where a function calls itself, directly or indirectly, to solve a problem by breaking it into smaller subproblems of the same form.

  12. What two essential components must every correct recursive function have?

    A base case (terminating condition that stops the recursion) and a recursive case (the function calling itself on a smaller/simpler input that progresses toward the base case).

  13. What happens if a recursive function has no reachable base case?

    It recurses infinitely, consuming stack frames until the call stack is exhausted, causing a stack overflow (and program crash).

  14. Write a recursive definition of factorial and its base case.

    $$n! = \begin{cases} 1 & n = 0 \\ n \times (n-1)! & n > 0 \end{cases}$$ Base case: $0! = 1$; recursive case multiplies $n$ by the factorial of $n-1$.

  15. What is the time complexity of the naive recursive Fibonacci that computes $F(n)=F(n-1)+F(n-2)$?

    Exponential time, $O(\varphi^{n})$ where $\varphi = \frac{1+\sqrt{5}}{2}$ (commonly stated as $O(2^{n})$), because it recomputes the same subproblems repeatedly. Space is $O(n)$ for the call stack.

  16. What is the difference between direct and indirect recursion?

    Direct recursion: a function calls itself directly (f calls f). Indirect (mutual) recursion: a function calls another function that eventually calls the first back (f calls g, and g calls f).

  17. What is tail recursion?

    A recursive call that is the very last operation performed in the function, with nothing left to do after it returns. Compilers can optimize it into a loop (tail-call optimization), using constant stack space.

  18. Compare recursion and iteration in terms of memory usage.

    Iteration uses a fixed amount of stack space (typically one frame). Recursion allocates a new stack frame for each call, so it uses O(depth) stack memory and risks stack overflow for deep recursion.

  19. What is the recurrence relation form used to analyze many recursive algorithms via the Master Theorem?

    $$T(n) = a\,T\!\left(\frac{n}{b}\right) + f(n)$$ where $a$ is the number of subproblems, $\frac{n}{b}$ is the subproblem size, and $f(n)$ is the non-recursive work per call.

  20. What is a recursive function's 'depth of recursion' and why does it matter?

    The maximum number of nested active calls before a base case returns. It matters because each level consumes a stack frame; excessive depth causes stack overflow, and it determines the space complexity O(depth).

  21. What is the scope and lifetime of a local variable declared inside a function?

    Scope: local to the function (block scope) — visible only within it. Lifetime: automatic storage duration — created when the function is entered and destroyed when it returns (unless declared static).

  22. What does declaring a local variable static inside a function do?

    It gives the variable static storage duration: it is initialized once and retains its value between successive calls to the function, while still being visible only within that function's scope.

  23. What is a function pointer and how do you declare one that points to int f(int)?

    A function pointer stores the address of a function. Declaration: int (*fp)(int); It can be assigned fp = f; and called as fp(5) or (*fp)(5).

  24. What is a function template and how does it relate to overloading?

    A function template defines a family of functions parameterized by type, e.g. template<typename T> T max(T a, T b). The compiler instantiates a concrete function per type used, providing type-safe generic code instead of writing separate overloads for each type.

  25. What is the special role of the main function's return value in C++?

    main returns an int that is the program's exit status to the operating system: 0 (or EXIT_SUCCESS) signals success, non-zero signals failure. If no return statement is present, main implicitly returns 0.

  26. What is a constexpr function and what does it enable?

    A constexpr function is one that can be evaluated at compile time when given constant-expression arguments, e.g. constexpr int square(int x){ return x*x; }. It enables its result to be used in contexts requiring compile-time constants, such as array sizes.

  27. What is recursion's typical trade-off versus iteration for problems like tree traversal?

    Recursion offers cleaner, more readable code that closely mirrors the recursive structure of the problem (e.g. trees), at the cost of function-call overhead and stack memory. Iteration is generally faster and uses less memory but can be more complex to write for inherently recursive structures.

  28. Can inline and recursion be combined effectively?

    Generally no. Compilers typically will not fully inline a recursive function because the depth of recursion is unknown at compile time, so the inline request is usually ignored for the recursive calls.

  29. What is the difference between an lvalue reference parameter (T&) and an rvalue reference parameter (T&&)?

    T& binds to modifiable lvalues (named objects). T&& binds to rvalues (temporaries), enabling move semantics and perfect forwarding. Overloading f(T&) and f(T&&) lets a function behave differently for lvalue vs rvalue arguments.

  30. How does the compiler pass control and data during a function call at runtime (the call mechanism)?

    A stack frame (activation record) is pushed containing the return address, the arguments, and space for local variables. Control jumps to the function; on return, the return value is delivered, the frame is popped, and execution resumes at the return address in the caller.

C++ Programming Functions FAQ

What is in the C++ Programming Functions syllabus?

C++ Programming Functions is split into 2 chapters: Function Basics and Advanced Function Concepts. Together they contain 7 topics.

How many chapters are in C++ Programming Functions?

2 chapters, containing 7 topics. The largest chapter is Advanced Function Concepts, with 4 topics.

Are there flashcards for C++ Programming Functions?

Yes — a 50-card Functions deck, printed in full in the flashcards section of this page and available in the Examius app with spaced repetition. The free plan covers one exam of your choice, including its flashcards; Examius Pro, a one-time purchase, unlocks every other exam and deck.