Functions in C (original) (raw)

Last Updated : 24 Jan, 2026

A function is a named block of code that performs a specific task. A function can take inputs (called parameters), execute a block of statements, and optionally return a result.

#include <stdio.h>

// function definition int square(int x) { return x * x; }

int main() {

// Calling the function
int result = square(5);

printf("Square of 5 is: %d", result);

return 0;

}

`

Why-Use-Functions__

How Functions Work in C?

Function Syntax

keyword

Function Declaration vs Definition

It's important to understand the difference between declaring a function and defining it. Both play different roles in how the compiler understands and uses your function.

**Function Declaration

A declaration tells the compiler about the function's name, return type, and parameters before it is actually used. It does not contain the function's body. This is often placed at the top of the program or in a header file.

C `

// function declaration int add(int a, int b);

`

**Function Definition

A definition provides the actual implementation of the function. It includes the full code or logic that runs when the function is called.

C `

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

`

**Why is declaration needed?

If a function is defined after the main function or another function that uses it, then a declaration is needed before it is called. This helps the compiler recognize the function and check for correct usage.

In short, the declaration introduces the function to the compiler, and the definition explains what it actually does.

Calling a Function

Once a function is defined, you can use it by simply calling its name followed by parentheses. This tells the program to execute the code inside that function.

C `

#include <stdio.h>

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

int main() {

// Function call
int result = add(5, 3);
printf("The sum is: %d", result);
return 0;

}

`

In this example, the function add is called with the values 5 and 3. The function runs its logic (adding the numbers) and returns the result, which is then stored in the variable result.

You can call a function as many times as needed from main or other functions. This helps avoid writing the same code multiple times and keeps your program clean and organized.

Types of Function in C

In C programming, functions can be grouped into two main categories: **library functions and **user-defined functions. Based on how they handle input and output, user-defined functions can be further classified into different types.

**1. Library Functions: These are built-in functions provided by C, such as printf(), scanf(), sqrt(), and many others. You can use them by including the appropriate header file, like #include <stdio.h> or #include <math.h>.

**2. User-Defined Functions: These are functions that you create yourself to perform specific tasks in your program. Depending on whether they take input or return a value, they can be of four types:

Each type serves different purposes depending on what the program needs. Using the right type helps make your code more organized and efficient.

Memory Management of Functions

When a function is called, memory for its variables and other data is allocated in a separate block in a stack called a **stack frame. The stack in which it is created is called **function call stack. When the function completes its execution, its stack frame is deleted from the stack, freeing up the memory.

Refer to this article to know more Function Call Stack in C

Disadvantages of Functions

Read more about Difference between function declaration and function definition.