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

Type checking in function calls

Type checking in function calls

What you'll learn: Why C requires your inputs to match your function’s requirements and how the compiler acts as a safety guard.

The "Square Peg" rule

In previous lessons, you learned how to define a function with parameters. When you actually call that function, you pass it "arguments"—the actual values it will work with. C is a statically typed language, which means it is very picky about the "shape" of the data you pass in.

Think of a function like a specialized vending machine. If the machine is designed to accept only gold coins (integers), and you try to shove a rectangular credit card (a string) or a piece of paper (a character) into the slot, the machine won't just fail to work—it will refuse to even start. In C, the compiler is the person checking your coins before they even reach the slot.

How the compiler protects you

When you compile your code, C looks at the function signature and compares it to your function call. It checks for two things:

  1. The number of arguments: Did you provide exactly as many values as the function expects?
  2. The type of arguments: Do the data types match the parameters?

If you try to pass a decimal number (double) into a function expecting a whole number (int), the compiler will often try to "demote" the value for you, but it might warn you that you're losing data. If you try to pass something completely unrelated, like a piece of text where a number should be, the compiler will stop you entirely with an error.

Seeing it in action

Consider this simple program that calculates the area of a square:

#include <stdio.h>

// This function strictly expects an integer
void printArea(int side) {
    int area = side * side;
    printf("The area is: %d\n", area);
}

int main() {
    int myValue = 5;
    
    // Correct: Passing an int to an int parameter
    printArea(myValue);

    // Risky: Passing a double (5.9). C may truncate this to 5,
    // leading to an inaccurate result (25 instead of 34.81).
    printArea(5.9); 

    // Error: Passing text ("Hello") would cause the compiler to fail.
    // printArea("Hello"); 

    return 0;
}

Why this matters

Type checking might feel annoying when you’re in a hurry, but it is one of C’s best features for catching bugs. By forcing the arguments to match the parameters, C ensures that the math inside your functions is performed on the correct kind of data, preventing "garbage in, garbage out" results.

Key takeaway

C checks your function arguments against your parameters during compilation to ensure the data types match and the program stays predictable.