Make a lecture like this from your own PDF
Drop a PDF, slides or notes here. You'll sign up, then yours starts.
0:00 / 6:02
Shared by Raj Desh
Default arguments allow a caller to omit parameters, which the compiler fills using a preset value. These must be assigned starting from the rightmost parameter to ensure the compiler can correctly map provided values to their corresponding arguments. This substitution occurs at compile time, meaning there is no performance penalty for the convenience of using a default value.
Function overloading permits multiple functions to share the same name, provided each has a unique parameter list. The compiler determines which version to execute based on the number and type of arguments passed. It is not possible to overload functions based solely on their return type, as the compiler must resolve the correct function before evaluating how the result is used.
Inline functions provide a hint to the compiler to replace a function call with the actual body of the function code. This avoids the overhead of traditional call-and-return procedures, such as jumping to a memory address or manipulating the stack. While this is useful for small, frequently called operations, the compiler ultimately decides whether to honor the request based on its own internal analysis of code efficiency.
0:00In this chapter, we look at three features in C++ that change how functions work: default arguments, function overloading, and inline functions. Each one solves a real problem. Default arguments let you leave out some arguments when calling a function — the compiler fills in a preset value so the code is still valid. Function overloading lets multiple functions share the same name as long as they take different parameters, which makes your code more readable when the same operation applies to different inputs. And inline functions give the compiler a way to skip the normal call-and-return procedure for very small functions, replacing the call with the function body directly. Together, these features give you more control over how functions are called, how they're named, and how they're compiled.
0:46A default argument is exactly what it sounds like: a value the compiler uses for a parameter when the caller doesn't provide one. The rule from the source is critical — default arguments must be the rightmost parameters in the function's parameter list. You cannot have a default for a left parameter while a right parameter has no default. Why? Because C++ fills arguments from left to right, so if you skip the left one, the compiler wouldn't know which parameter you meant. In practice, this means you can call a function with fewer arguments than it declares. Take the multiply example: int multiply(int a, int b = 1). Calling multiply(8) passes 8 for a and uses the default 1 for b, giving you 8. Calling multiply(8, 5) passes both values and returns 40. One function definition, two different behaviors.
1:38Let's walk through what actually happens when the compiler sees a call with a missing argument. The caller writes something like multiply of eight. The compiler checks the function signature and sees that multiply takes two parameters, but the second one has a default value of one. Since only one argument was provided, the compiler takes the default and fills it into the parameter slot before generating the function body's code. The function itself never knows whether the value came from the caller or from the default — it just sees b equals one and executes. This substitution happens at compile time, so there is no runtime cost for using a default argument. The result is cleaner calling code: you write multiply of eight instead of multiply of eight comma one, and everyone reading the code understands that one is just the natural fallback value.
2:30Function overloading is a different kind of flexibility. It lets you define multiple functions that share the same name, as long as their parameter lists are different. The compiler tells them apart by looking at the number and types of the arguments you pass. So you can have one add function that adds two integers, and another add that adds three — same name, different parameter lists, different behavior. But there is a hard rule here: you cannot overload a function based only on the return type. The source makes this explicit. A long add of int and int would be rejected because the compiler cannot decide which version to call just from the return type — the caller's return type is not part of the function signature used for name resolution. This is one of those rules that feels arbitrary until you realize the compiler needs to resolve which function to call before it ever checks what you're doing with the result.
3:23Default arguments and function overloading can seem like they solve the same problem — making functions more flexible to call. But they work differently and apply in different situations. Default arguments keep one function definition and let you omit trailing parameters. The compiler fills in the missing values at the call site. Overloading, on the other hand, creates separate function definitions, each with its own body, that happen to share a name. Use default arguments when the behavior is the same and you just want a convenient fallback value. Use overloading when the behavior differs based on how many or what kind of arguments you pass. The two can even work together. And here is a practical warning: if you mix both on the same function, the overloaded versions must still have distinct parameter lists, and the default argument rule — rightmost only — still applies.
4:13Calling a function normally involves overhead. The program has to push arguments onto the stack, jump to the function's address, execute the body, and then jump back. For a function that does a lot of work, this overhead is negligible. But for something like squaring a number — a single multiplication — the overhead of the call itself can be more expensive than the actual computation. The inline specifier addresses this. When you write inline before a function definition, you're suggesting to the compiler that it should insert the function's body directly at each call site instead of generating a regular call. In the source's example, inline int square of a returns a times a, the compiler could replace a call like square of four with just four times four directly in the object code. No jump, no stack manipulation — just the computation right where it's needed.
5:05Here is the most important thing to understand about inline: the compiler is not obligated to honor your request. The source says it clearly — the inline specifier merely indicates that inline is preferred, but the compiler is free to ignore it. Modern compilers have their own heuristics. They may inline a function you never marked inline if they think it's profitable, and they may skip inlining a function you did mark if they think it would bloat the code. So think of inline as a hint for very short functions where the overhead of a function call genuinely outweighs the work being done. To wrap up: we covered three C++ function features. Default arguments simplify calls by filling in missing values from the right. Function overloading lets multiple functions share a name when their parameter lists differ. And inline functions let you suggest avoiding call overhead for tiny operations. Each one makes your functions more flexible and your code cleaner.