A function is a self-contained block of code that performs a specific task. Functions are used to break large programs into smaller, manageable pieces.
Think of a function as a mini-program inside your program.
Each function does one specific job, like printing a message, calculating sum, or multiplying numbers.
Benefits of Functions
Reusability – Write once, use multiple times.
Modularity – Divide program into logical sections.
Ease of Maintenance – Change code in one place only.
Readability – Programs are easier to read.
Avoid Code Duplication – Reduces repeated code.
Debugging Made Easy – Errors can be traced in smaller sections.
Function Declaration
Function declaration tells the compiler what the function looks like (name, parameters, return type).
It is optional for small programs, but necessary for large programs to organize code.
Syntax:
Example:
Here,
int→ return typeadd→ function name(int, int)→ parameters
Function Definition
Function definition contains the actual code to perform the task.
Can be defined before main() or after main() (if declared first).
Syntax:
Example:
Function Call
To execute a function, we use a function call.
Function call can be in main() or in another function.
Example:
add(5, 10)→ passes arguments5and10to the function.Function returns
15which is stored inresult.
Function Parameters
Functions can take input values (parameters) and may return output.
Types of Parameters
No parameter, no return value
With parameter, no return value
No parameter, returns value
With parameter, returns value
Call by Value vs Call by Reference
a) Call by Value
Function receives a copy of the argument.
Changes inside the function do not affect original variable.
b) Call by Reference
Function receives original variable using
&.Changes inside the function affect the original variable.
Recursive Functions
A recursive function calls itself to solve a problem.
Useful for factorial, Fibonacci, and many mathematical problems.
Example: Factorial
Recursion breaks a big problem into smaller problems.
Advantages of Functions
Reduces Code Complexity – Large programs become simpler.
Easy Testing – Test individual functions.
Efficient Programming – Less code, more clarity.
Collaborative Programming – Teams can work on separate functions.
Supports Recursion – Solves complex problems elegantly.
Function Overloading
Functions with same name but different parameters.
Allows using one name for multiple tasks.
Inline Functions
Inline functions execute faster as code is replaced at compile time.
Syntax:
inlinekeyword.
Useful for small, frequently used functions.
Default Arguments in Functions
Functions can have default parameter values.
Practical Applications of Functions
Mathematical Calculations – Sum, product, factorial, powers.
Input/Output Tasks – Separate functions for input validation, printing menus.
Repetitive Tasks – Reusable blocks instead of writing same code multiple times.
Complex Programs – Break large projects into smaller, manageable functions.
Summary of Functions
Functions perform specific tasks and make programs organized.
Declaration tells the compiler about the function.
Definition contains the actual code.
Call by value passes a copy, call by reference passes original variable.
Functions can return values or be void.
Supports recursion, overloading, inline, and default arguments.
Benefits: reusability, modularity, readability, maintenance, and efficiency.