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

C Functions

0%

Quick Reference

Function definition pattern:

// Prototype (declaration)
int add(int a, int b);

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

void functions:

void greet(const char *name) {
    printf("Hello, %s\n", name);
    // no return value needed
}

Common Pitfalls

  • Missing prototype – Calling a function before declaring it causes implicit declaration warnings (or errors in C99+).
  • Forgetting pass-by-value – Changes to parameters don’t affect the caller. Use pointers to modify the caller’s data.
  • Missing return – Non-void functions must return a value on all paths. Use -Wall to catch this.
  • Returning local addressesreturn &local_var; is a dangling pointer. The variable is destroyed when the function returns.

Unlocks

Complete this skill to see what it unlocks.