C Programming

Functions in C

C-CAT

Functions in C

Why Functions?

  • Reusability — write once, use many times
  • Modularity — break problem into smaller pieces
  • Maintainability — easier to find and fix bugs
  • Readability — code is easier to understand

Function Anatomy

// return_type function_name(parameter_list) {
//     // function body
//     return value;
// }

// Function declaration (prototype) — tells compiler about function
// Best placed BEFORE
main or in a header file
int add(int a, int b);        // declaration

int main() {
    int result = add(3, 4);   // function call
    printf("3 + 4 = %d\n",
result);  // 7
    return 0;
}

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

Good Programming Practices for Functions

// a. Always design REUSABLE functions
// b. Make function declarations GLOBAL (before main)
// c. Avoid displaying results inside functions → RETURN results
// d. Avoid accepting inputs from user inside functions → PASS as arguments
// e. Before using pre-defined functions → READ its prototype from help

// BAD practice:
void bad_example() {
    int num;
    scanf("%d", &num);        // DON'T
accept input inside function
    int result = num * 2;
    printf("Result: %d\n", result);
// DON'T print inside function
}

// GOOD practice:
int double_it(int num) {      // Accept as argument, return result
    return num * 2;
}
// In main:
int num;
scanf("%d", &num);
int result = double_it(num);
printf("Result: %d\n", result);

Function Types

// 1. No return, no parameters (void)
void greet() {
    printf("Hello, World!\n");
}

// 2. Has return, no parameters
int get_random() {
    return 42;
}

// 3. No return, has parameters
void print_n_times(char c, int n) {
    for (int i = 0; i <
n; i++) {
        printf("%c", c);
    }
    printf("\n");
}

// 4. Has return, has parameters (most common)
float circle_area(float radius) {
    return 3.14159 * radius * radius;
}

Pass by Value

In C, all function arguments are passed by value — the function gets a COPY.

void swap_fail(int a, int b) {
    int temp = a;
    a = b;        // modifies LOCAL copy
    b = temp;     // modifies LOCAL copy
    // Original variables unchanged!
}

void swap_ok(int *a, int *b) {
    int temp = *a;
    *a = *b;       // modifies via pointer
(pass by reference)
    *b = temp;
}

int main() {
    int x = 10, y = 20;
    swap_fail(x, y);    // x=10, y=20 still!
    swap_ok(&x, &y);    // x=20, y=10 WORKS!
    return 0;
}

Factorial and Power

// Factorial function (iterative)
long long factorial(int n) {
    long long result = 1;
    for (int i = 2; i <= n; i++) {
        result *= i;
    }
    return result;
}

// Power function (iterative)
double power(double base, int exp) {
    double result = 1.0;
for (int i = 0; i < exp; i++) {
        result *= base;
    }
    return result;
}

// Four-function calculator (using global error flag per PDF)
int error_flag = 0;   //
global error flag

double calculator(double a, double b, char op) {
    switch (op) {
        case '+': return a + b;
        case '-': return a - b;
        case '*': return a * b;
        case '/':
            if (b == 0) {
                error_flag = 1;   // set error flag
                return 0;
            }
            return a / b;
        default:
            printf("Unknown operator\n");
            return 0;
    }
}

Continue learning

Related notes

Put this topic into timed practice

Open mock tests when you want full-exam pacing, or keep drilling in practice mode.