5.1.7 · D1C Programming

Foundations — Functions — declaration vs definition, prototypes, call by value

2,039 words9 min readBack to topic

Before you can read the parent note comfortably, you must own every little symbol it throws around. This page collects them in build order: each one only uses ideas already explained above it. If you meet a symbol on the parent page and feel a flicker of "wait, what is that?" — it is defined here.


0. The absolute atoms: what a "program" even is

Why the topic needs this: the parent note keeps saying "the compiler reads top-to-bottom, once." That single fact is the reason prototypes exist. So we anchor it first.

Figure — Functions — declaration vs definition, prototypes, call by value

The red scanner in the figure only moves downward. When it reaches a line that uses a name it has never seen, it cannot look ahead to find it later — it must already know that name. Hold that image; it explains the whole "declare before you call" rule.


1. Types — the "shape" of a value

Why needed: every function has a return type (the shape of what comes out) and its parameters have types (the shape of what goes in). Without types the compiler wouldn't know how many bytes to copy.


2. Variable — a named box

Why needed: the parent's whole swap/square/next story is about which box gets changed — the caller's box, or a fresh copy box inside the function.


3. Function — the reusable machine

Figure — Functions — declaration vs definition, prototypes, call by value

The figure names each part on the machine. Learn these four words — return type, name, parameters, body — because the parent note's "declaration vs definition" distinction is entirely about which of these parts are present.


4. Parameter vs Argument — the two ends of a call

These two words sound the same and get mixed up forever. Nail them now.

Why needed: "call by value" means the argument's value is copied into the parameter. You cannot understand that sentence until these two words are separate in your head.


5. The three verbs: ;, {}, and the difference they make

Now the pivotal parent idea falls out with zero new machinery:

Figure — Functions — declaration vs definition, prototypes, call by value

So "declaration vs definition" is not two mysterious concepts — it is literally the same signature followed by either a semicolon or a body. That is why the parent can say "a definition is automatically also a declaration": the definition contains the whole signature, so it also fulfils the promise a bare declaration would make.


6. Return — the output chute

Why needed: the return type from §1 is exactly the shape of the thing that comes down this chute. void functions have no chute — they return nothing.


7. Copying — the heart of "call by value"

Now every atom is in place to explain the topic's soul.

Figure — Functions — declaration vs definition, prototypes, call by value

The yellow arrow in the figure is a copy, not a wire. There is no live connection back to v. That is precisely why the parent's swap fails: swap shuffles its copies x and y, but the copy-arrows were one-way, so a and b never move.


8. Address & pointer — the escape hatch (preview only)

You will need these on the parent page's swap fix, so here is just enough to recognise them.

Full treatment is in Pointers in C. The parent note's key twist — passing a pointer is still call by value — makes sense now: you copy the address slip (a value!), but the slip still points home.


The prerequisite map

Source code and single-pass compiler

Types int and void

Variable a named box

Function the named machine

Signature name return type params

Parameter vs Argument

Semicolon vs Braces

Declaration vs Definition

Return the output chute

Call by value copying

Stack frame scratchpad

Address and pointer escape hatch

Functions topic 5.1.7

Each box only uses ideas from the boxes feeding into it — which is exactly the order this page introduced them.


Equipment checklist

Test yourself: cover the right side and answer before revealing.

The compiler reads a C file in which direction, how many times?
Top to bottom, once (single-pass) — so a name must be known before it is used.
What is a type, in one phrase?
A label saying what kind of value and how big it is (e.g. int = whole number, void = nothing).
A variable is…?
A named box in memory holding one value of one type.
The four parts of a function are…?
Return type, name, parameters, body.
What is a function's signature?
Its name + return type + parameter types — everything except the body.
Difference between a parameter and an argument?
Parameter = the placeholder slot inside the definition; argument = the actual value handed in at the call.
A signature ending in ; (no body) is a…?
Declaration (prototype) — just the promise.
A signature followed by { ... } is a…?
Definition — the built machine (also counts as a declaration).
What does return x; do?
Hands x back out to the caller and stops the function.
In call by value, what actually reaches the parameter?
A fresh copy of the argument's value; the caller's box is untouched.
What is a stack frame?
Scratch memory for one call holding its parameters and locals, discarded on return.
Read &a and *p aloud.
"address of a" and "the box at address p".

Connections

  • Yeh note Hinglish mein →
  • Stack and Function Call Mechanism — where the copied parameters actually live.
  • Pointers in C — the & / * escape hatch, in full.
  • Scope and Lifetime of Variables — why the copy-box vanishes on return.
  • Header Files and Separate Compilation — why declarations live apart from definitions.
  • Arrays as Function Arguments · Recursion — next stops once these atoms are solid.