A function groups statements into a single operation. Functions are the basic unit of organization in a Zig program.
| Section | Title |
|---|---|
| 1 | Defining Functions |
| 2 | Parameters and Return Values |
| 3 | Passing Values |
| 4 | Public Declarations |
| 5 | Importing Files |
| 6 | Organizing a Small Program |
| 7 | Exercises |
Defining FunctionsA function groups statements into a single operation. Functions are the basic unit of organization in a Zig program.
Parameters and Return ValuesFunctions communicate through parameters and return values.
Passing ValuesWhen a function is called, values are passed from the caller to the function parameters.
Public DeclarationsA Zig program may be split across many files. Declarations can be made visible outside a file with pub.
Importing FilesLarge programs are divided into smaller files. Zig uses @import to include declarations from another file.
Organizing a Small ProgramA program grows gradually.
Exercises1. Write a function max3 that returns the largest of three integers.