A **Function** is a self-contained block of statements that carries out a specific task. Every C program has at least one function, which is main(). Functions help in achieving **code reusability** and **modularity**, making the program easier to read, test, and maintain.
A function consists of two main parts: the **Declaration** (or Prototype) and the **Definition**. To use a function, you must also **Call** it.
int, float). If it returns nothing, we use void.{} that performs the task.In C, you usually declare a function above main() and define it later, or define it entirely above main() so the compiler knows it exists.
Parameters act as placeholders for data you send into the function. When you call the function and pass real values, those values are called **Arguments**.
Most functions perform a calculation and need to send the result back to the caller. We use the return keyword for this. The return value must match the **Return Type** declared at the start of the function.
Variables declared inside a function are called **Local Variables**. They only exist within that function and cannot be accessed from main() or other functions. Conversely, **Global Variables** are declared outside all functions and are accessible everywhere.
| Method | Description |
|---|---|
| Pass by Value | A copy of the data is sent. Changing it inside the function does NOT change the original variable. |
| Pass by Reference | The memory address (pointer) is sent. Changing it inside the function DOES change the original variable. |
C allows a function to call itself. This is known as **Recursion**. It is a powerful tool for solving problems that can be broken down into smaller, similar sub-problems (like calculating factorials or Fibonacci numbers).