Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 29 / 68

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

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

A function that takes another function as an argument, or returns one, is a higher-order function. Being able to specify what to do from outside lets you reuse the same skeleton in many situations.

The sample code sorts into descending order by passing the criterion as a function, as in Array.Sort(nums, Function(a, b) b - a). That anonymous function (lambda) defines the ordering rule on the spot.

A common early stumble is what the comparison's return value means. Function(a, b) b - a gives descending and a - b ascending — the sign of the result decides the order.

In professional work, higher-order functions are used wherever the caller should decide the sort criterion or what happens to each element of a list.

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

๐Ÿงช This site can't compile or run VB.NET 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)