Abstract


  • A set of Instruction that can be triggered to accomplish a particular task

Function Signature

  • Refers to the declaration of a function
  • Including its name, return Datatype, and parameter datatype

First-class Citizen Function


Behave and treated like a variable

  1. Assigned to variables
  2. Passed as arguments to other functions
  3. A function can be the return value of another function
  4. Can store functions in data structures like Array, List, or Hash Map etc

This enables powerful programming techniques like Higher-order Function and Closure etc.

Important

There isn’t first class citizen function in Java, Java creates an illusion of it using Java Lambda & Functional Interface.

Higher-order Function


  • A Function that takes in another one or more functions as inputs or return a function as its output

Important

Higher-order function builds on top of First-class Citizen Function, so we can pass function into another function or return function from another function as if function is a variable.

Higher-order function powers Closure.

Common higher-order functions

map(): take in a function, and uses it to implement changes on a stream of values.

filter(): take in a function, and uses it to remove some values from a stream of values.

reduce(): takes in a function, and uses it to convert a stream of values into a single value.

Rust Function


  • For Function Signature, you must declare the Datatype of each parameter

  • So Compiler almost never needs you to use the function elsewhere in the code to figure out what type you mean. The compiler is also able to give more helpful error messages if it knows what types the function expects.


  • Return the last Expression implicitly

References