liveBook · Manning

Function

As explained in Julia as a Second Language

A function definition is a language construct that declares a function's name, arguments, and executable body.

A function gives a reusable name to a calculation or operation. It accepts zero or more inputs, called arguments, and evaluates to a value. Instead of repeating a sphere-volume calculation for every radius, define `sphere_volume(r) = 4*pi*r^3/3`, then call `sphere_volume(2)` or `sphere_volume(4)`. The radius changes between calls; the formula stays in one place. section 2.5 section 2.7

For work that needs several lines, use `function name(arguments)` followed by a body and `end`. A Julia function normally evaluates to its last expression; an explicit `return` is useful when the function must exit early. The body can contain loops and conditions as well as arithmetic. section 3.4.1 section 3.0

A calculation with a loop

`function f(n) total = 0 for x in 1:n total += 2x + 1 end total end` Calling `f(n)` repeats the calculation over `1:n`. The final `total` supplies the result without an explicit `return`. The same pattern can sum terms of a Taylor series to approximate `sin(x)`. section 3.4.1

Functions are not limited to named mathematical formulas. Julia operators are functions too: `5 + 3` and `+(5, 3)` both produce `8`. A symbol-named function with two arguments can also be called between its arguments. Built-ins such as `div`, `floor`, and `round` make common number operations available by name. section 2.5.3 section 2.5.4

A function can organize an entire interaction. `practice(n)` asks `n` multiplication questions, reads each answer, converts the input string with `parse(Int, answer)`, checks it, and prints a score. To inspect such a function, run its expressions one at a time in the REPL: a typed answer such as `"18"` must be parsed before comparison with a numeric product works as intended. section 5.4

In Julia, a function name can have multiple methods for different argument types. `function attack! end` creates a function with no methods; calling it raises a `MethodError`, while calling a name that has not been defined raises an `UndefVarError`. Use `methods(attack!)` to inspect attached methods, then add a method for an argument combination the function does not yet handle. section 7.3.3

Functions can also be assembled into a readable sequence. In `input |> splitter('_') |> mapper(uppercasefirst) |> join`, `|>` passes each result to the next function. The `->` operator defines the anonymous functions returned by `splitter` and `mapper`, avoiding a deeply nested expression. section 15.3

How Julia as a Second Language covers it

How deep it goes
Works through short and multiline definitions, final-expression results, method inspection, and chaining; defers the mathematical proof behind its sine example.
Examples
Builds sphere-volume and sine calculations, a multiplication-practice application, typed attack methods, and a chained text transformation.
What it assumes
Starts with familiar calculator functions and variables, then teaches arguments and function definitions before applying loops and methods.
What you can do after
Build it from scratch: define reusable Julia calculations and multiline tasks, then inspect methods or chain functions for larger applications.
How it connects
Connects functions to arguments, loops, operators, multiple dispatch, anonymous functions, and the pipe operator.

Also explained in

Compare how 20 books explain function →

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.