Skip to content
Circuit

Function Example in C

Functions

Separate a reusable calculation from main using a typed function.

Complete program

#include <stdio.h>

int square(int value) {
    return value * value;
}

int main(void) {
    printf("Square of 7: %d\n", square(7));
    printf("Square of -3: %d\n", square(-3));
    return 0;
}

Try in Circuit →

Opens a new program without replacing your saved code. Press Run when you are ready.

How it works

The function definition appears before its calls, so the compiler knows its return and parameter types. Each call gives value a fresh local copy of the argument.

The function returns a result instead of printing it, allowing main or another function to decide what to do next. The example uses small integers; squaring a sufficiently large signed integer overflows and is undefined behavior.

Expected output

Square of 7: 49
Square of -3: 9

Concepts to practice

  • Parameters
  • Return values
  • Scope
  • Reuse

Predict a different input or change one value, then compare the result with your prediction. Keep changes within the documented bounds and read warnings before trusting an answer.