Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 29 / 68

Higher-order functions (passing a function as an argument)

This lesson covers passing a function as an argument in Julia, with the aim of treating the work itself as a component.

Being able to pass one function into another is what makes it a higher-order function. Picture specifying only how the work should be done and leaving the doing to someone else. Passing a keyword argument to sort() works on much the same idea.

The sample code sorts into descending order by passing the criterion as a keyword argument, as in sort(nums, rev=true). The map you met earlier is in fact another higher-order function.

A common early stumble is the difference between positional and keyword arguments. A keyword argument passed by name, such as rev=true, is handled differently from an ordinary positional one.

In professional work, it extends to designs where the logic is swapped out depending on the situation — an important part of what makes Julia's data handling work.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)