Function arguments

2 snippets across 2 stacks — Bash & Linux, JavaScript

Also written as function as argument

SHBash & Linux

Functions

SH · Bash Scripting
syntax
function_name() {
  commands
  return exit_code
}
example
log_msg() {
  local level="$1"
  local message="$2"
  echo "[$(date '+%Y-%m-%d %H:%M:%S')] [${level}] ${message}"
}

log_msg "INFO" "Deployment started"
log_msg "ERROR" "Config file missing"
output
[2026-04-04 14:22:01] [INFO] Deployment started
[2026-04-04 14:22:01] [ERROR] Config file missing

Note Use local to scope variables to the function (without local, variables are global). Arguments are accessed via $1, $2, etc. $@ is all arguments. return sets the exit code (0-255); to return strings, echo them and capture with $().

JSJavaScript

Higher-Order Functions

JS · Functions
syntax
function fn(callback) { callback(); }
function fn() { return function() {}; }
example
function withLogging(fn) {
  return function(...args) {
    console.log(`Calling ${fn.name} with`, args);
    const result = fn(...args);
    console.log(`Result:`, result);
    return result;
  };
}

const add = (a, b) => a + b;
const loggedAdd = withLogging(add);
loggedAdd(3, 4);
output
"Calling add with [3, 4]"
"Result: 7"

Note Functions that accept or return other functions. The backbone of functional patterns like decorators, middleware, and composition.