C++ Functions
π§ C++ Functions
A function in C++ is a block of code that performs a specific task.
Functions help make programs modular, reusable, readable, and easier to maintain.
πΉ 1. Why Use Functions?
Avoid code repetition
Improve readability
Easier debugging and maintenance
Divide a program into logical parts
πΉ 2. Function Syntax
πΉ 3. Simple Function Example
Calling the function:
πΉ 4. Function with Parameters
Call:
πΉ 5. Function with Return Value
Call:
πΉ 6. Function Declaration (Prototype)
Used when function is defined after main().
πΉ 7. Call by Value
Original value not changed.
πΉ 8. Call by Reference
Original value is changed.
πΉ 9. Function with Pointer Parameters
πΉ 10. Default Arguments
Call:
πΉ 11. Inline Functions
β Reduces function call overhead
β Large functions not suitable
πΉ 12. Function Overloading
Same name, different parameters.
πΉ 13. Recursion (Function Calling Itself)
πΉ 14. main() Function
β Common Mistakes
π Summary
Functions are reusable code blocks
Can take parameters and return values
Support overloading, recursion, default arguments
Improve program structure and clarity
