Function Declaration and Definition in C Programming (Complete Guide)


This tutorial explains functions in C, including function declaration, definition, and calling. It helps beginners understand how to write reusable, modular code, improving readability and maintainability of C programs.

1. What Are Functions in C

Functions are blocks of code that perform a specific task.

They allow:

  1. Reusability of code
  2. Modular programming
  3. Better readability

Example: printf() is a built-in function.

2. Function Declaration (Prototype)

A function declaration tells the compiler about:

  1. Function name
  2. Return type
  3. Parameter types

Syntax:


return_type function_name(parameter_list);

Example:


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

Notes:

  1. Usually placed before main()
  2. Also called a function prototype

3. Function Definition

A function definition provides the actual body of the function.

Syntax:


return_type function_name(parameter_list) {
// code
return value; // if return_type is not void
}

Example:


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

4. Calling a Function

A function is called from main() or other functions.

Example Program:


#include <stdio.h>

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

int main() {
int num1, num2, sum;

printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

// Function call
sum = add(num1, num2);

printf("Sum = %d\n", sum);
return 0;
}

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

Sample Output:

Enter two numbers: 10 20
Sum = 30

5. Points to Remember

  1. Declaration before usage helps the compiler check arguments
  2. Definition contains the actual task logic
  3. Return type can be int, float, char, or void
  4. Functions can take zero or more parameters