Functions in C

Subject: Data Structures and Algorithms · Language: C · Level: beginner · 4 min read

Break code into reusable functions with parameters, return values, prototypes, and scope.

A function is a named block of code that performs a specific task. Instead of writing the same logic again and again, we write it once inside a function and call that function whenever needed.

For example, if a program needs addition in many places, we can create an `add` function and reuse it.

Think of a function like a small machine. You give it required input, it performs one clear job, and it may return a result. This makes large programs easier to understand because each function has a clear responsibility.

Why Functions Are Important

Functions make programs easier to read, test, debug, and maintain. Large programs become manageable when they are divided into smaller functions.

A beginner should not write every line inside `main`. `main` should control the flow, while helper functions should do repeated or separate jobs such as calculation, validation, searching, or printing.

Parts of a Function

A function usually has four parts: return type, function name, parameters, and function body.

```c

int add(int a, int b) {

return a + b;

}

```

The return type tells what kind of value the function sends back. The function name should describe the job. Parameters receive data from the caller. The body contains the actual statements that solve the task.

Function Declaration

A function declaration tells the compiler that a function exists. It is also called a function prototype.

```c

int add(int a, int b);

```

This tells C that `add` takes two integers and returns an integer.

Why Function Prototypes Matter

A prototype allows you to call a function before its full definition appears in the file. It helps the compiler check the function name, return type, and parameters.

In larger programs, prototypes make the code easier to organize.

Function Call

A function call runs the function.

```c

int result = add(10, 5);

```

Here, `10` and `5` are passed to the function, and the returned value is stored in `result`.

Parameters and Arguments

Parameters are the variables written in the function definition. Arguments are the actual values passed during the function call.

```c

int add(int a, int b) // a and b are parameters

add(10, 5); // 10 and 5 are arguments

```

A parameter is like an empty seat inside the function. An argument is the real value that sits in that seat when the function is called.

When `add(10, 5)` runs, the value 10 is copied into parameter `a`, and the value 5 is copied into parameter `b`. Then the function uses `a` and `b` to calculate the answer.

return Statement

`return` sends a value back from the function to the place where the function was called.

```c

return a + b;

```

If a function does not return any value, its return type is `void`.

Use `return` when the function calculates something that the caller needs. Use `void` when the function only performs an action, such as printing a message.

```c

void greet(void) {

printf("Welcome to C");

}

```

Pass by Value

In C, normal function arguments are passed by value. That means the function receives a copy of the original value.

If the function changes its parameter, the original variable in `main` does not change. To update the original value, pointers are used.

```c

#include <stdio.h>

int add(int a, int b) {

return a + b;

}

int subtract(int a, int b) {

return a - b;

}

int multiply(int a, int b) {

return a * b;

}

int main(void) {

int first, second;

printf("Enter two numbers: ");

scanf("%d %d", &first, &second);

printf("Sum = %d\n", add(first, second));

printf("Difference = %d\n", subtract(first, second));

printf("Product = %d\n", multiply(first, second));

return 0;

}

```

Types of Functions

Library Functions

C already provides many useful functions through header files.

Learning to use library functions saves time and makes programs more reliable.

Function Design Tips

Scope of Variables

A variable declared inside a function is local to that function. It cannot be directly used outside that function.

```c

void show(void) {

int value = 10;

}

```

Here, `value` belongs only to `show`.

Recursion Idea

Recursion means a function calls itself. It is useful in problems like factorial, Fibonacci, tree traversal, and divide-and-conquer algorithms.

Every recursive function must have a stopping condition. Without it, the function keeps calling itself.

Functions in DSA

In DSA, functions help separate operations such as insert, delete, search, sort, push, pop, enqueue, and dequeue.

This keeps code readable and makes each operation easier to test.

Practice Question

Write a C program that takes two numbers as input and uses functions to find the greater number and the smaller number.

More C Lessons

Browse all PrepCampus study materials