HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RVariablesData TypesOperatorsCharacters (character)VectorsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ Character
📚 Vector
🔀 Condition & Loop
🧩 Function
functionSingle-line function
R›REFERENCE›Function
🧩

Function

Definition and return with function.

CommandDescription
functionDefines your own command.
Single-line functionShort, single-expression functions.

function

Try it yourself →

Defines your own command.

Syntax

name <- function(params) { ... return(...) }

Description

Defined with function and assigned to a variable. return(x) returns the result (with parentheses); the last expression is also returned automatically.

Example

square <- function(x) {
  return(x * x)
}
cat(square(5), "\n", sep="")

Expected output

25

When to use?

Preventing code repetition, breaking the program into parts.

Single-line function

Try it yourself →

Short, single-expression functions.

Syntax

name <- function(x) expression

Description

Curly braces are not needed for single-expression functions; the last expression is returned automatically.

Example

addUp <- function(a, b) a + b
cat(addUp(8, 9), "\n", sep="")

Expected output

17

When to use?

Short helper functions, with sapply/lapply.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code