Skip to content
AgileLogics
5 min read

Functions

Prerequisites: Multidimensional arrays

Syntax

int add(int a, int b) {
    return a + b;
}

void greet(char *name) {
    printf("Hello, %s\n", name);
}

Example

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

int main(void) {
    printf("%d\n", square(4));
    return 0;
}

Expected output

16

Every C argument is passed by value — a function receives a copy of a plain int or double, so changes inside it never affect the caller's original variable. Making a function actually modify a caller's value requires passing a pointer instead (covered next, in Pointers: Introduction).

Common mistakes

  • Expecting a function to modify a plain int parameter and have that change visible after the call returns — it won't, because only the copy changed.

Practice

Write a function int isEven(int n) that returns 1 if n is even, 0 otherwise.

Show solution
int isEven(int n) {
    return n % 2 == 0;
}