Menu
Coddy logo textTech

Helper Methods

Part of the Object Oriented Programming section of Coddy's C journey. Lesson 10 of 61.

So far, we've seen functions that modify struct data (like player_take_damage) and functions that simply display it. But there's another important category: functions that compute derived values from struct members without changing anything.

Consider a Rectangle struct that stores width and height. The area isn't stored: it's calculated on demand:

typedef struct {
    double width;
    double height;
} Rectangle;

double rectangle_area(const Rectangle *self) {
    return self->width * self->height;
}

double rectangle_perimeter(const Rectangle *self) {
    return 2 * (self->width + self->height);
}

These are helper methods. They take the struct's existing data and produce a new value. Notice they use const pointers since they only read, never write. The computed result is returned rather than stored in the struct.

Rectangle box = {5.0, 3.0};
printf("Area: %.1f\n", rectangle_area(&box));       // 15.0
printf("Perimeter: %.1f\n", rectangle_perimeter(&box)); // 16.0

This pattern keeps your structs lean: store only the essential data, and compute everything else when needed. It also ensures derived values are always consistent with the current state.

challenge icon

Challenge

Easy

Let's build a Circle module that calculates derived values from stored data. You'll practice writing helper methods: functions that compute results from struct members without modifying them.

You'll create three files:

  • circle.h: Declare a Circle struct with a single double radius member. Also declare two helper methods:
    • circle_area: takes a const pointer to Circle and returns the area
    • circle_circumference: takes a const pointer to Circle and returns the circumference
    Use include guards with the symbol CIRCLE_H.
  • circle.c: Implement both helper methods. For the calculations, use 3.14159 as the value of pi. The area formula is pi * radius * radius, and the circumference formula is 2 * pi * radius. Both functions should only read the radius: never modify it.
  • main.c: Create a Circle and use the helper methods to compute and display its properties.

You will receive one input: the radius of the circle (as a double).

In your main file, initialize a Circle with the given radius, then use your helper methods to calculate and print the area and circumference.

Print the results in this format:

Area: {area}
Circumference: {circumference}

Use %.2f for both values to display two decimal places.

For example, with a radius of 5.0, the output would be:

Area: 78.54
Circumference: 31.42

Try it yourself

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

int main() {
    double radius;
    scanf("%lf", &radius);
    
    // TODO: Create a Circle struct with the given radius
    
    // TODO: Use circle_area and circle_circumference helper methods
    // to calculate and print the results
    
    // Print format:
    // printf("Area: %.2f\n", ...);
    // printf("Circumference: %.2f\n", ...);
    
    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

Practice on your own: Online C compiler