Ad space (banner)
๐Ÿ“ŠR Lessons
Lesson 6 / 68

Writing your own function

This lesson covers the basics of writing a function in R, with the aim of producing code you can reuse.

You define a function with function(args) { body }. Gathering work you do repeatedly under a name means you can run it just by calling it.

In the sample code, a greet function takes a name argument and returns an assembled greeting. R functions automatically return the value of the last expression evaluated, so an explicit return is not needed.

A common early stumble is that mechanism of returning a value without return. If you are used to other languages, a function with no explicit return feels odd — but this is R's proper design.

In professional work, gathering shared logic into functions reduces duplication and means a change only has to be made in one place.

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

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