racket Functions Function Definitions

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Functions in Racket can be created with the lambda form. The form takes a list of arguments and a body.

(lambda (x y) (* x y))

In the example above, the function takes in two arguments and returns the result of multiplying them.

> ((lambda (x y) (* x y)) 4 4)
16
> ((lambda (x y) (* x y)) 3 2)
6

It's tedious to re-write the function and its body every time we want to multiply two numbers, so let's give it a name. To give it a name, use the define form. This will bind functions to a name.

(define multiply (lambda (x y) (* x y)))

Now we can refer to our function by calling multiply

> (multiply 5 2)
10

Since it is very common to bind procedures to names, Racket provides a shorthand to define functions using the define form.

(define (multiply x y) (* x y))

For more information and examples, see Functions: lambda in the Racket Guide.



Got any racket Question?