Skip to content
Learn Netverks

Lesson

Step 11/36 31% through track

functions-r

Functions

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Functions: the syntax, patterns, and safety habits you need before advancing in R.

Teams still ship Functions in R codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Functions in contexts like: Research pipelines, Shiny dashboards, and statistical reporting.

Write R in the editor and click Run on server—the dev runner executes with Rscript; use print() or cat() and base R in playground snippets (tidyverse locally; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Functions are first-class in R—define with function(args) { body }. Named arguments and defaults reduce boilerplate; return the last evaluated expression unless return() is used.

Defining functions

add <- function(a, b) {
  a + b
}
greet <- function(name = "World") {
  paste("Hello,", name)
}

Default arguments

scale_center <- function(x, center = TRUE) {
  if (center) x - mean(x) else x
}

Avoid growing environments accidentally—document side effects in production code.

Important interview questions and answers

  1. Q: Implicit return?
    A: The last expression in a function body is returned unless return() is called earlier.
  2. Q: function vs <- function?
    A: Both create functions; the assignment form is idiomatic: fn <- function() {}.

Self-check

  1. How do you set a default argument?
  2. What returns from a function if return() is omitted?

Pitfall: R functions return the last expression—use early return() when clarity helps.

Interview prep

Default arguments?

Defined in the signature: function(x, center = TRUE).

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Default args?
  • ... ellipsis?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump