# Introduction to Functions in C – Breaking Programs into Pieces

So far, all our code has lived inside `main()`.

That works for small programs.

But what happens when your program grows?

It becomes:

*   Hard to read
    
*   Hard to debug
    
*   Hard to manage
    

This is where **functions** come in.

Functions help us organize our code into smaller, reusable pieces.

## 1\. What Is a Function?

A function is:

> A block of code that performs a specific task.

Instead of writing everything inside `main()`, we can create smaller units that handle specific jobs.

Think of it like this:

Instead of writing the same instructions again and again, you write them once and reuse them.

That is the power of functions.

## 2\. Why Do We Need Functions?

Imagine this inside `main()`:

*   Print a square
    
*   Calculate a sum
    
*   Check if a number is even
    
*   Print another square
    

If everything is inside `main()`, the code becomes messy.

Functions allow us to:

*   Reuse code
    
*   Reduce repetition
    
*   Make programs easier to understand
    
*   Debug more easily
    

Professional programs always use functions.

## 3\. Basic Structure of a Function

Here is the general format:

```c
return_type function_name(parameters) {
    // code
}
```

Let’s understand each part.

### Return Type

This tells C what type of value the function sends back.

Examples:

*   `int`
    
*   `float`
    
*   `char`
    
*   `void` (returns nothing)
    

### Function Name

The name you choose for the function.

Example:  
`add`  
`printSquare`  
`isEven`

Choose meaningful names.

### Parameters

These are inputs to the function.

They allow the function to receive values.

## 4\. Example: A Simple Function

```c
int add(int a, int b) {
    return a + b;
}
```

Let’s break it down:

*   The function name is `add`.
    
*   It takes two integers: `a` and `b`.
    
*   It returns their sum.
    
*   The return type is `int`.
    

## 5\. Calling a Function

Defining a function is not enough.

We must call it.

Example:

```c
#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(5, 3);
    printf("Result: %d\n", result);
    return 0;
}
```

What happens here?

1.  `main()` runs.
    
2.  It calls `add(5, 3)`.
    
3.  The values 5 and 3 go into `a` and `b`.
    
4.  The function calculates 5 + 3.
    
5.  It returns 8.
    
6.  `result` stores 8.
    
7.  We print it.
    

That is function execution flow.

## 6\. Void Functions (Functions That Return Nothing)

Not all functions need to return a value.

If a function only performs an action (like printing), we use `void`.

Example:

```c
void greet() {
    printf("Hello!\n");
}
```

And call it like this:

```c
greet();
```

Since it returns nothing, we do not store anything.

## 7\. Functions With Parameters But No Return

Example:

```c
void printNumber(int n) {
    printf("%d\n", n);
}
```

Call it like:

```c
printNumber(10);
```

The function receives the value and prints it.

## 8\. Important Concept: Execution Flow

When a function is called:

*   The program pauses `main()`.
    
*   It jumps to the function.
    
*   It executes the function.
    
*   It returns to `main()`.
    

Then execution continues.

Functions do not run automatically.  
They only run when called.

## 9\. Common Beginner Mistakes

1.  Forgetting the return statement in non-void functions
    
2.  Using the wrong return type
    
3.  Forgetting to call the function
    
4.  Confusing parameters with arguments
    

Parameters → variables in the function definition  
Arguments → actual values passed when calling

Example:

```c
add(5, 3);
```

5 and 3 are arguments.

## 10\. Practice Exercises

1.  Write a function that multiplies two numbers.
    
2.  Write a function that prints 5 stars.
    
3.  Write a function that checks if a number is even (returns 1 if even, 0 if odd).
    
4.  Call each function from `main()`.
    

## Final Thoughts

Functions allow us to:

*   Break large programs into smaller pieces
    
*   Reuse logic
    
*   Keep code organized
    
*   Write cleaner programs
    

If nested loops taught you to think in layers, functions teach you to think in structure.

In the next lesson, we will go deeper into:

*   Function declarations (prototypes)
    
*   How the compiler reads your code
    
*   Variable scope (local vs global)
    

And that is where you start understanding how C really works behind the scenes.
