Course contentsShow
← C
Lesson 337 of 1,2195. FunctionsFree lesson

Positional matching of arguments

Positional matching of arguments

What you'll learn: How C matches the values you send to a function with the variables defined inside that function using their order.

The assembly line analogy

Imagine you are operating a machine that stamps labels onto boxes. The machine has two slots: one for a color and one for a word. If you put red ink in the first slot and the word "Fragile" in the second, you get a red Fragile label. If you swap them—putting the word in the first slot and the color in the second—the machine will jam or produce nonsense.

Functions in C work exactly like this. The function doesn't know the "name" of the data you are sending it; it only knows which "slot" the data arrived in. This is called positional matching.

Order defines meaning

When you define a function, you set a specific sequence of parameters. When you call that function later, C takes your first argument and plugs it into the first parameter, the second argument into the second parameter, and so on.

Take a look at this example:

#include <stdio.h>

// The function expects: 1. an int, then 2. a float
void displayStats(int level, float health) {
    printf("Level: %d, Health: %.1f\n", level, health);
}

int main() {
    int myLevel = 10;
    float myHealth = 95.5;

    // Correct order: level first, then health
    displayStats(myLevel, myHealth);

    // Incorrect order: health first, then level
    // This may cause a compiler warning or unexpected behavior!
    // displayStats(myHealth, myLevel); 

    return 0;
}

In the code above, the variable names myLevel and myHealth don't actually matter to the displayStats function. What matters is that myLevel is placed first in the parentheses. C looks at the "first slot" of the function call, sees 10, and assigns that value to the first parameter, level.

Why this matters

If you have a function void drawRectangle(int width, int height), calling drawRectangle(10, 50) creates a tall, skinny rectangle. Calling drawRectangle(50, 10) creates a wide, short one.

Even if the data types are the same (both integers), the position determines which value represents the width and which represents the height. C will never "guess" based on your variable names; it follows the map of positions you laid out in the function signature.

Key takeaway

C matches arguments to parameters based strictly on their left-to-right order, not their names.