liveBook · Manning

Include

As explained in Julia as a Second Language

include is Julia's include function loads and evaluates the contents of a source code file in the current session.

Julia's include function loads a source file and evaluates its contents in the current session. It lets you keep reusable function definitions in a file, load them without restarting the REPL, and reload the file after changing an implementation. section 2.5.1

Load reusable volume functions

Place sphere_volume, cylinder_volume, and cone_value in volumes.jl, then run include("volumes.jl"). Julia evaluates the definitions and returns the final result; subsequent calls such as cylinder_volume(1, 2) use the loaded functions. section 2.5.1

Listing 2.2 volumes.jl source code file
# Volume calculations
sphere_volume(r)      = 4π*r^3/3
cylinder_volume(r, h) = π*r^2*h
cone_value(r, h)      = π*r^2*h/3

Inspect the compact volumes.jl listing used to demonstrate file-based function definitions. section 2.5.1

include is an ordinary Julia function call, not a textual paste mechanism like C or C++ #include. It returns the value produced by the included file, can run inside a function, and evaluates definitions in the surrounding module. Package source files commonly use include inside a module so separate files become part of that module. section 16.2.2 section 16.4

Split a package across files

A ToyGeometry module can include volume.jl and trig.jl. This keeps a growing package manageable while placing both files' function definitions inside ToyGeometry. section 16.2.2

How Julia as a Second Language covers it

How deep it goes
Explains file evaluation, return values, module context, and reloading, while stopping short of advanced metaprogramming details.
Examples
Uses volumes.jl, ToyGeometry's volume.jl and trig.jl, and arithmetic.jl to demonstrate loading, modularization, return values, and nested calls.
What it assumes
Assumes basic Julia functions, source files, and REPL use while clarifying module evaluation from concrete examples.
What you can do after
Operate Julia's include function to load, reload, organize, and evaluate definitions from source files.
How it connects
Connects include with source files, the REPL, functions, modules, packages, and code organization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.