Knowledgebase

Functions and How Arguments Are Passed Print

  • programminglanguages, programming, guide, howto, solution, zillionkinghost, hosting, support
  • 0

Reusable units.

WHAT A FUNCTION IS

A named unit taking inputs and producing a result.

WHAT DIFFERS BETWEEN LANGUAGES

Whether arguments are passed by value or by reference Whether default values are supported Whether arguments can be named Whether a variable number is permitted

WHY PASSING MATTERS

Passing by reference means the function can change the caller's value.

WHAT THAT CAUSES WHEN MISUNDERSTOOD

Functions modifying data the caller did not expect to change.

WHAT MANY LANGUAGES ACTUALLY DO

Pass references by value, which behaves differently from both simple descriptions.

WHAT THAT MEANS PRACTICALLY

Reassigning the parameter does not affect the caller; modifying what it points to does.

WHAT MAKES A GOOD FUNCTION

One responsibility A name saying what it does Few parameters No dependence on hidden state Predictable output for given input

WHAT TO AVOID

Many parameters, which indicates too many responsibilities Boolean parameters changing behaviour Functions modifying things outside themselves without saying so

WHAT HIGHER-ORDER FUNCTIONS ARE

Functions taking or returning functions.

WHAT THEY ENABLE

Expressing transformation and filtering declaratively.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot