Anatomy of a Function Prototype
Anatomy of a Function Prototype
Return Type
What kind of data the function sends back (
int, float, void).Function Name
The name you use to call it (e.g.,
findMax).Parameter List
Types of inputs it expects (e.g.
int, int). Variable names are optional here.Semicolon (;)
A prototype always ends with a semicolon
; because it is just a declaration.Explanation
- A prototype declares a function's return type, name and parameter types.
- The compiler checks a call against that declaration before linking the definition.
Parameter Passing Mechanisms in C++
Parameter Passing Mechanisms in C++
1. Call by Value
A duplicate copy of the value is passed. Changes made inside the function do NOT affect the original variable in
main().2. Call by Address (Pointers)
The memory address (
&a) is passed to a pointer (int *x). Changes made using *x modify the original variable.3. Call by Reference (C++ Alias)
A reference nickname (
int &x) is passed. Directly modifies the original variable with clean, simple syntax without pointers!Explanation
- A value parameter gets a copy while a reference parameter aliases the caller's object.
- A pointer parameter receives an address and can be used to modify its target.
Execution Overhead of a Standard Function Call
Execution Overhead of a Standard Function Call
01
Save Context
CPU saves active registers & return address to stack
➔
02
Push Arguments
Passes parameters across memory boundaries
➔
03
Jump & Execute
Jumps to function body in code segment
➔
04
Return & Resume
Returns value and jumps all the way back to main()
Explanation
- A call arranges arguments and saves the information needed to return.
- The exact cost depends on the calling convention and compiler optimisations.
Normal Function Call vs. Inline Function Expansion
Normal Function Call vs. Inline Function Expansion
Normal Function (JUMP)
int res = cube(5);CPU pauses, pushes data onto the stack, jumps to
cube() address, runs it, and jumps back. Takes extra CPU time!Inline Function (COPY-PASTE)
int res = cube(5);Compiler replaces it directly at compile time with:
int res = 5 * 5 * 5; (Zero jumping overhead! Super fast!)Explanation
- An ordinary call transfers control to the function and then returns.
- Inline expansion may put the function's operations at the call site when the compiler chooses.
Default Arguments Rule Validation
Default Arguments Rule Validation
VALID Declarations
•
•
•
void func(int a, int b, int c = 50); (Rightmost only)•
void func(int a, int b = 20, int c = 50); (Right two)•
void func(int a = 10, int b = 20, int c = 50); (All parameters)
INVALID Declarations (Compiler Error)
•
•
Reason: The compiler gets confused about which value belongs to which variable!
void func(int a = 10, int b, int c = 50); (Middle missing!)•
void func(int a = 10, int b, int c); (Rightmost missing!)Reason: The compiler gets confused about which value belongs to which variable!
Explanation
- A call can omit an argument only when a suitable default is available.
- After a defaulted parameter, later ordinary parameters must also have defaults available.
Valid Overloading Criteria
Valid Overloading Criteria
1. Number of Arguments
int add(int a, int b);int add(int a, int b, int c); (Takes 2 vs 3 parameters)2. Data Types of Arguments
float add(float a, float b);int add(int a, int b); (Takes floats vs integers)3. Order / Sequence of Types
void show(int a, float b);void show(float a, int b); (Different sequence)Explanation
- Overloads share a name but differ in their parameter lists.
- Changing only the return type leaves the compiler unable to distinguish a call.