Menu
Coddy logo textTech

Static Functions

Part of the Object Oriented Programming section of Coddy's C journey — lesson 4 of 61.

When you split code across multiple files, every function defined in a source file is visible to other files by default. But sometimes you need helper functions that should only be used internally—functions that other files shouldn't access or even know about.

The static keyword, when applied to a function, restricts its visibility to the file where it's defined. This creates a private function that cannot be called from other source files, even if someone tries to declare it.

// math_utils.c
#include "math_utils.h"

// Private helper - only visible in this file
static int square(int x) {
    return x * x;
}

// Public function - can be called from main.c
int sum_of_squares(int a, int b) {
    return square(a) + square(b);
}

In this example, sum_of_squares is declared in the header and can be used anywhere. However, square is marked static, so it exists only within math_utils.c. If main.c tries to call square() directly, the linker will produce an error—the function simply doesn't exist outside its file.

This is C's way of achieving encapsulation at the file level. Static functions let you hide implementation details, keeping your public interface clean while using whatever helper logic you need internally.

challenge icon

Challenge

Easy

Let's build a distance calculator that uses a private helper function to compute the square of a number—a detail that should stay hidden from the rest of your program.

You'll create three files:

  • distance.h: Your header file declaring a single public function distance_squared that takes four integers (x1, y1, x2, y2) representing two points and returns an integer. Include guards using the symbol DISTANCE_H.
  • distance.c: Your source file containing the implementation. Create a static helper function called square that takes an integer and returns its square. This helper should only be visible within distance.c. Then implement distance_squared using your static helper to calculate (x2-x1)² + (y2-y1)².
  • main.c: Your main file that includes the header and uses the public function. The square helper should not be accessible here—only distance_squared is available.

The distance_squared function calculates the squared Euclidean distance between two points. By keeping the square function static, you're hiding this implementation detail from any code outside distance.c.

You will receive four integer inputs: x1, y1, x2, and y2.

Print the squared distance in this format:

Squared distance: {result}

For example, with points (0, 0) and (3, 4), the output would be:

Squared distance: 25

Cheat sheet

The static keyword restricts a function's visibility to the file where it's defined, creating a private function that cannot be called from other source files.

// Private helper - only visible in this file
static int square(int x) {
    return x * x;
}

// Public function - can be called from other files
int sum_of_squares(int a, int b) {
    return square(a) + square(b);
}

Static functions provide file-level encapsulation, allowing you to hide implementation details while keeping your public interface clean.

Try it yourself

#include <stdio.h>
#include "distance.h"

int main() {
    int x1, y1, x2, y2;
    scanf("%d", &x1);
    scanf("%d", &y1);
    scanf("%d", &x2);
    scanf("%d", &y2);
    
    // TODO: Call distance_squared with the input coordinates
    // and print the result in the format: "Squared distance: {result}"
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming