C Programming

Unit 7: Functions

Local, global, static, register variables, library and user-defined functions, parameter passing, recursion, and arrays in functions.

Variables in C

C variables differ in scope, lifetime, and storage class.

Local variables

Local variables are declared inside a function or block. They are accessible only within that block and exist only while the block is executing.

#include <stdio.h>

void show(void)
{
    int x = 10;       /* local to show() */
    printf("%d\n", x);
}

Global variables

Global variables are declared outside all functions. They are accessible from any function in the same file and exist for the entire program execution.

#include <stdio.h>

int count = 0;        /* global variable */

void increment(void)
{
    count++;
}

int main(void)
{
    increment();
    increment();
    printf("%d\n", count);  /* 2 */
    return 0;
}

Static variables

A static local variable retains its value between function calls. It is initialized only once.

#include <stdio.h>

void counter(void)
{
    static int n = 0;
    n++;
    printf("%d\n", n);
}

int main(void)
{
    counter();   /* 1 */
    counter();   /* 2 */
    counter();   /* 3 */
    return 0;
}

Register variables

The register keyword requests that the variable be stored in a CPU register for faster access. The compiler may ignore the request.

register int i;
for (i = 0; i < 1000; i++) { /* ... */ }

Library functions and user-defined functions

  • Library functions are pre-defined functions available in standard header files such as <stdio.h>, <math.h>, and <string.h> (for example: printf, scanf, sqrt, strlen).
  • User-defined functions are written by the programmer to perform a specific task.

A user-defined function has three parts:

  1. Function declaration (prototype) — tells the compiler about the function.
  2. Function definition — contains the actual code.
  3. Function call — invokes the function.
#include <stdio.h>

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

int main(void)
{
    int result = add(5, 3);           /* call        */
    printf("Sum = %d\n", result);
    return 0;
}

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

Pass by value

In pass by value, a copy of the actual argument is passed to the function. Changes made inside the function do not affect the original variable.

#include <stdio.h>

void modify(int x)
{
    x = 100;
}

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

Pass by reference

In pass by reference, the address of the variable is passed to the function using pointers. Changes inside the function affect the original variable.

#include <stdio.h>

void modify(int *x)
{
    *x = 100;
}

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

Recursion

Recursion is a technique in which a function calls itself directly or indirectly. A recursive function must have a base case to stop the recursion.

Example — factorial of a number:

#include <stdio.h>

int factorial(int n)
{
    if (n <= 1)
        return 1;
    return n * factorial(n - 1);
}

int main(void)
{
    printf("%d\n", factorial(5));   /* 120 */
    return 0;
}

Use of array in function

An array can be passed to a function by passing its name, which represents the address of its first element. The function receives a pointer to the array.

#include <stdio.h>

int sum(int arr[], int n)
{
    int total = 0;
    for (int i = 0; i < n; i++)
        total += arr[i];
    return total;
}

int main(void)
{
    int data[5] = {1, 2, 3, 4, 5};
    printf("Sum = %d\n", sum(data, 5));   /* 15 */
    return 0;
}