General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals
  2. Functions

Lambda expressions

A lambda expression in computer programming is essentially an anonymous function or a function without a name. It is a concise way to represent a function in many programming languages, such as Python, Java (from version 8 onwards), C#, and JavaScript. Lambda expressions are particularly useful in functional programming and scenarios where a short, one-time function is needed, often passed as an argument to higher-order functions (functions that take functions as parameters or return functions).

Lambda expressions typically have the following characteristics:

  • Conciseness: They are generally more concise than defining a named function.

  • Inline Definition: They are defined at the point where they are needed, often inline with other code.

  • Arguments: They can take any number of arguments, just like regular functions.

  • Expression Body: They usually consist of a single expression, the result of which is returned by the lambda function. Some languages support block bodies for more complex logic.

  • Limited Scope: They have access to the surrounding lexical scope, allowing them to use variables defined outside of the lambda expression.

Lambda Expression Syntax Examples

Python

In Python, a lambda expression is defined using the lambda keyword, followed by a list of parameters, a colon, and the expression to evaluate and return.

# Lambda expression to add two numbers
add = lambda x, y: x + y
print(add(5, 3))  # Output: 8

C#

In C#, lambda expressions use the => operator, separating the parameters from the expression body.

// Lambda expression to add two numbers
Func<int, int, int> add = (x, y) => x + y;
Console.WriteLine(add(5, 3));  // Output: 8

JavaScript

JavaScript lambda expressions, or arrow functions, use the => syntax similar to C#.

// Lambda expression (arrow function) to add two numbers
const add = (x, y) => x + y;
console.log(add(5, 3));  // Output: 8

Use Cases

Lambda expressions are often used in situations involving:

  • Passing a function as an argument to a higher-order function (e.g., map, filter, reduce in many languages).

  • Short event handlers or callbacks.

  • Small functions that are used only once and do not need a name.

Lambda expressions make code more readable and concise, especially for short functions that are used on the fly. They are a key part of functional programming paradigms in many languages.

PreviousParameters and argumentsNextHigher Order Functions

Last updated 1 year ago

Was this helpful?