TutorialsCFunctions
Share:

Functions

intermediate

Part of Program Flow in C

Theory

Functions in C are reusable blocks of code that perform specific tasks. They help organize code, avoid repetition, and make programs easier to debug and maintain.

A function consists of a declaration (prototype) and a definition. The declaration tells the compiler about the function's name, return type, and parameters. The definition contains the actual code.

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

Return types: Functions can return any data type using the return statement. A function that returns nothing uses void.

Parameters:

  • Formal parameters — the variables in the function definition
  • Actual arguments — the values passed when calling the function

C uses pass-by-value — functions receive copies of the arguments, not the originals. To modify a variable in a calling function, pass its address (using pointers).

Function prototypes (declarations) are typically placed before main() or in header files. They allow the compiler to verify correct usage even if the function definition appears later.

Scope rules:

  • Local variables — declared inside a function, accessible only within it
  • Global variables — declared outside all functions, accessible everywhere
  • Static variables — retain their value between function calls
void counter() {
    static int count = 0;  // initialized once
    count++;
    printf("%d ", count);
}

Recursion occurs when a function calls itself. Every recursive function needs a base case to stop the recursion.

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

Inline functions (C99) suggest the compiler to expand the function code at the call site, avoiding function call overhead for small functions:

inline int square(int x) { return x * x; }

The math library (<math.h>) provides functions like sqrt(), pow(), sin(), cos(), abs(). Compile with -lm flag.

Practical Examples

Example 1: Function Types and Prototypes
c
Example 2: Recursion and Static Variables
c

Exercises

Calculator with Functions

easy

Write separate functions for add, subtract, multiply, and divide (returns float). Each function takes two int parameters. The main() function should call all four and print the results.

Starter Code:

#include <stdio.h>\n\n// Function prototypes\n\nint main() {\n  int a = 20, b = 5;\n  // Call and print results\n  return 0;\n}

Expected Output:

20 + 5 = 25\n20 - 5 = 15\n20 * 5 = 100\n20 / 5 = 4.00

Power Function (Recursive)

medium

Write a recursive function 'power' that calculates base^exp (exponent is a non-negative integer). Use the formula: base^exp = base * base^(exp-1) with base^0 = 1.

Starter Code:

#include <stdio.h>\n\nint power(int base, int exp) {\n  // Your recursive code here\n}\n\nint main() {\n  // Test with 2^10\n  return 0;\n}

Expected Output:

2^10 = 1024\n3^4 = 81\n5^0 = 1

Static Counter

medium

Write a function 'uniqueID' that uses a static variable to generate unique IDs starting from 1000. Each call returns the next ID. Call it 5 times in main() and print each ID.

Starter Code:

#include <stdio.h>\n\nint uniqueID() {\n  static int id = 1000;\n  // Your code here\n}\n\nint main() {\n  // Call and print 5 IDs\n  return 0;\n}

Expected Output:

ID: 1000\nID: 1001\nID: 1002\nID: 1003\nID: 1004

Mini Quiz

Mini Quiz

Mini Project

Mini Project: Math Utility Library

Create a collection of math utility functions including factorial (recursive), GCD, LCM, prime check, and digit sum. Demonstrate modular programming.

Requirements:

    Bonus Challenge

    Add a function that finds prime factors of a number using a loop that calls the prime check function.