Aral Code

Arrays & Functions Lesson 11 of 16

C Functions

Write your own C functions with parameters and return values, declare prototypes, pass arrays to functions, understand scope and static variables, and use recursion.

A function is a named block of code that does one job. main, printf and strlen are functions. Writing your own functions keeps programs organized, avoids repeated code and makes bugs easier to find.

Defining and calling

#include <stdio.h>

void print_line(int length) {
    for (int i = 0; i < length; i++) printf("-");
    printf("\n");
}

double average(double a, double b, double c) {
    return (a + b + c) / 3;
}

int main(void) {
    print_line(24);
    printf("Average: %.2f\n", average(88, 91, 85));
    print_line(24);
    return 0;
}

Output

------------------------
Average: 88.00
------------------------
  • The first word is the return type: void means nothing is returned.
  • Parameters are listed with their types.
  • return sends a value back and ends the function.

Function prototypes

C reads files from top to bottom. If main comes first, the compiler must already know the functions it calls. A prototype (declaration) at the top tells it:

#include <stdio.h>

int is_passing(int grade);            /* prototype */
const char *descriptor(int grade);    /* prototype */

int main(void) {
    int g = 86;
    printf("%d: %s (%s)\n", g, descriptor(g), is_passing(g) ? "passed" : "failed");
    return 0;
}

int is_passing(int grade) {
    return grade >= 75;
}

const char *descriptor(int grade) {
    if (grade >= 90) return "Outstanding";
    if (grade >= 85) return "Very Satisfactory";
    if (grade >= 80) return "Satisfactory";
    if (grade >= 75) return "Fairly Satisfactory";
    return "Did Not Meet Expectations";
}

Output

86: Very Satisfactory (passed)

Prototypes are what header files (.h) contain: stdio.h is mostly a long list of prototypes for printf, scanf and friends.

Passing arrays

When you pass an array, the function receives its address, so it can read and even change the original. It doesn't know the length, so pass that separately:

#include <stdio.h>

int highest(const int arr[], int n) {
    int max = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] > max) max = arr[i];
    }
    return max;
}

void add_bonus(int arr[], int n, int bonus) {
    for (int i = 0; i < n; i++) arr[i] += bonus;
}

int main(void) {
    int scores[] = {78, 85, 92, 69};
    printf("Highest: %d\n", highest(scores, 4));
    add_bonus(scores, 4, 3);
    printf("After bonus: %d %d %d %d\n", scores[0], scores[1], scores[2], scores[3]);
    return 0;
}

Output

Highest: 92
After bonus: 81 88 95 72

const in highest promises the function won't change the array; the compiler enforces it.

Pass by value

Normal variables are passed as copies. Changing the parameter doesn't change the caller's variable:

#include <stdio.h>

void try_double(int x) {
    x = x * 2;
}

int main(void) {
    int n = 10;
    try_double(n);
    printf("%d\n", n);
    return 0;
}

Output

10

To let a function change a variable, pass its address with a pointer. That's the next lesson.

Scope and static variables

#include <stdio.h>

int next_ticket(void) {
    static int counter = 0;   /* keeps its value between calls */
    counter++;
    return counter;
}

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

Output

Ticket 1
Ticket 2
Ticket 3

Normal local variables disappear when the function ends. A static local variable is created once and remembers its value across calls.

Recursion

#include <stdio.h>

long long factorial(int n) {
    if (n <= 1) return 1;            /* base case */
    return n * factorial(n - 1);     /* recursive step */
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

int main(void) {
    printf("10! = %lld\n", factorial(10));
    printf("gcd(48, 36) = %d\n", gcd(48, 36));
    return 0;
}

Output

10! = 3628800
gcd(48, 36) = 12

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does a function prototype do?
2. Can a function change an array passed to it?
3. What is special about a static local variable?
4. What must every recursive function have?

By Sir Reyel · Last updated 2026-12-04