EN ES
Programming Concept

Functions

Defining and using functions; call by value/reference, overloading, variable args, defaults.

Chapter 5 (pp. 133–150) of Computer Science I.

Topics

SectionPageContent
Defining & Using Functions134Signatures, calling, organizing.
How Functions Work137Call by value vs call by reference.
Other Issues142Functions as entities, overloading, variable-argument functions, optional/default parameters.

Key Ideas

  • Call by value vs call by reference — the central abstraction; made concrete with pointers in C and object references in Java.
  • Functions as first-class entities foreshadows function pointers (C) and lambdas (Java).
  • Generalized Rounding example recurs across language parts.

Examples

Return a value

function max(a, b):
  if a > b:
    return a
  return b

Call by reference

function swap(ref x, ref y):
  temp ← x
  x ← y
  y ← temp

Mutates the caller’s variables; call by value would leave them unchanged.

In Java

static int max(int a, int b) {
    return (a > b) ? a : b;
}

Java passes primitives by value, so it cannot swap two ints through parameters — wrap them in an array or object to emulate call by reference.

Citations

[1] Computer Science I, Ch. 5, pp. 133–150.