student@ubuntu:~$
c 2/5 25 XP

Functions

0%

Quick Reference

Function structure:

return_type name(parameter_list)
{
    // body
    return value;    // omit for void functions
}

Prototype pattern:

double average(int arr[], int size);    // Prototype at top

int main(void) {
    double avg = average(grades, count);  // Call
    return 0;
}

double average(int arr[], int size) {    // Definition below
    int sum = 0;
    for (int i = 0; i < size; i++) sum += arr[i];
    return (double)sum / size;
}

Pass-by-value rules:

Type What’s passed Can function modify original?
int x Copy of value No
double x Copy of value No
int arr[] Pointer to first element Yes (array elements)
int *p Copy of address Yes (pointed-to value)

Common Pitfalls

  • Broken swap — Pass-by-value means swapping copies doesn’t work. Fix with pointers (Week 6).
  • void f() vs void f(void) — Empty () means unspecified args, not “no args.” Use (void).
  • Returning local pointers — Local variables die on return. Use calloc or pass a buffer.
  • Missing prototypes — Calling an undeclared function is an error in C99+.
  • Forgetting array size — Arrays decay to pointers in functions. Always pass size separately.
  • No overloading — Each function needs a unique name. Use prefixes: print_int, print_student.

Unlocks

Complete this skill to see what it unlocks.