⬅ Previous Next ➡

Functions in C

Functions in C
  • Function is a block of code that performs a specific task and can be reused.
  • Functions make programs modular, easy to debug, and reduce code repetition.
  • Main parts: declaration (prototype), definition, and function call.

1) Function Declaration (Prototype) & Definition

  • Declaration tells compiler about function name, return type, and parameters.
  • Definition contains the actual function code.
  • Syntax:
    • Prototype: return_type func_name(parameter_list);
    • Definition: return_type func_name(parameter_list) { ... }
#include <stdio.h>

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

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

int main() {
    int sum = add(10, 20); // function call
    printf("Sum = %d\n", sum);
    return 0;
}

2) Types of Functions (Common)

  • Library Functions: built-in (printf, scanf, strlen, etc.).
  • User-defined Functions: created by programmer (add, factorial, swap, etc.).

3) Call by Value

  • In call by value, a copy of the variable value is passed to the function.
  • Changes made inside the function do not affect the original variable.
#include <stdio.h>

void changeValue(int x) {
    x = 100; // changes only local copy
}

int main() {
    int a = 10;
    changeValue(a);
    printf("a = %d\n", a); // still 10
    return 0;
}

4) Call by Reference (Using Pointers)

  • In call by reference, the address of variable is passed using pointers.
  • Changes inside the function affect the original variable.
#include <stdio.h>

void changeValue(int *x) {
    *x = 100; // changes original variable using pointer
}

int main() {
    int a = 10;
    changeValue(&a);
    printf("a = %d\n", a); // now 100
    return 0;
}

5) Example: Swap Two Numbers (Call by Reference)

  • Swap requires reference (pointers) so original values change.
#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 5, y = 9;

    printf("Before: x=%d, y=%d\n", x, y);
    swap(&x, &y);
    printf("After : x=%d, y=%d\n", x, y);

    return 0;
}

6) Recursive Functions

  • Recursion means a function calls itself.
  • Must have a base condition to stop recursion.
  • Common examples: factorial, fibonacci, sum of digits.
6.1) Factorial using Recursion
#include <stdio.h>

int factorial(int n) {
    if (n == 0 || n == 1) {
        return 1; // base case
    }
    return n * factorial(n - 1); // recursive call
}

int main() {
    int n = 5;
    printf("Factorial of %d = %d\n", n, factorial(n));
    return 0;
}
6.2) Fibonacci using Recursion
#include <stdio.h>

int fib(int n) {
    if (n == 0) return 0;
    if (n == 1) return 1;
    return fib(n - 1) + fib(n - 2);
}

int main() {
    int i;
    int n = 6;

    for (i = 0; i < n; i++) {
        printf("%d ", fib(i));
    }
    printf("\n");
    return 0;
}

7) Quick Notes

  • Use prototypes to avoid compiler warnings.
  • Call by value is default in C (copy of value).
  • Call by reference is done using pointers (& and *).
  • Recursion needs base case; otherwise infinite recursion occurs.
⬅ Previous Next ➡