Coding/Learn/c/c functions and prototypes
C Foundational Course
01.Introduction, Compilation Pipeline & Syntax
02.Variables, Primitives & Explicit Type Casting
03.Operators, Precedence & Booleans (<stdbool.h>)
04.Control Flow: If-Else, Ternary & Switch
05.Loops (For, While, Do-While) & Jump Statements
06.Contiguous Arrays & Multi-Dimensional Matrices
07.Null-Terminated Strings & <string.h>
08.User Input, scanf & fgets Buffer Safety
09.Pointers, Memory Addresses & Dereferencing
10.Functions, Prototypes & Pass-by-Pointer
11.11. Recursion & High-Speed Function Pointers
12.File Streams: Create, Write & Read (fopen/fclose)
13.Structures, Unions & typedef Aliasing
14.Memory Word Alignment & Struct Padding
15.Enums & Bitwise Manipulation (Flags & Masks)
16.Dynamic Memory: malloc, calloc, realloc & free
17.Defensive Error Handling, errno & Assertions
18.Preprocessor Macros & Storage Classes
19.Fixed-Width Types (stdint.h), Math & Time (<time.h>)
20.Capstone: High-Performance In-Memory Key-Value Engine
Course Certificate0%

Complete all lessons to earn

LearnFunctions, Prototypes & Pass-by-Pointer
10 / 20 0%
Lesson 10 of 20
6 mins read

Functions, Prototypes & Pass-by-Pointer

PreviousNext Lesson
In Plain English

C evaluates arguments strictly pass-by-value. To mutate a caller variable, pass its memory address (&val) to a pointer parameter (int *val). Forward function prototypes inform the compiler of parameter types and return signatures before main().

The Mental Model

"Pass-by-value is photocopying your passport and handing the copy to an officer; pass-by-pointer is giving the officer your actual passport to stamp."

Deep Dive: How It Works

Function Prototypes: Informs the compiler of signatures before implementation definitions.

Pass-by-Value: Placing arguments onto the stack creates local copies.

Pass-by-Pointer: Passing addresses enables direct mutation of caller scope variables.

Syntax Blueprint

void doubleValue(int *val) {
    *val *= 2;
}

doubleValue(&score);

Define function accepting pointer parameter and pass address with & at callsite.

PointCore Rules to Remember

Point star
Forward Declarations: Prototypes prevent implicit declaration warnings and enforce type safety.
Point star
Pass-by-Pointer: Enables multiple return values from a single function call.
Point star
Stack Scope: Never return pointers to local stack variables from functions.

Common Beginner Traps & How to Fix Them

Mistake:Returning the address of a local stack variable: int* getVal() { int x = 10; return &x; }.

Why it happens: x is popped off the stack when the function returns; the pointer becomes immediately dangling.

How to fix: Allocate dynamic heap memory (malloc) or have the caller provide a destination buffer.

Pro Tip: Use const on pointer parameters (const char *str) when the function only reads and does not mutate the buffer.

Live Interactive Example

Hit Run Code to see it live
In-Place Mutation via Pointer
C11 / GCC 9.2
1
2
3
4
5
6
7
8
9
10
11
12
Output Console
Click "Run Code" to view the rendered output.
How it works: doubleValue dereferences the pointer and doubles the original score in place.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Call doubleValue a Second Time

  • PointCall doubleValue(&score) a second time.
  • PointPrint "Quadrupled Score: %d\n" with score.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates