函数可作为输入或被其他函数返回的含义及编程示例解析
Great question—this is a super common point of confusion, so let’s break it down clearly by first clarifying what your example does, then contrasting it with what functional programming means by "functions as inputs/outputs."
First: Let’s clarify your example
In the code you shared:
public int getaddition(int a,int b) { return a+b; } // Call it like this getaddition(getFirstNumber(),getSecondNumber())
You’re not passing functions to getaddition—you’re passing the return values of getFirstNumber() and getSecondNumber(). Those two calls run first, spit out integers, and those integers are what get passed to getaddition. This is standard parameter passing, not the "functions as input" behavior from functional programming.
What "Functions as Input/Output" Actually Means
In functional programming, we talk about higher-order functions: functions that either:
- Accept other functions as parameters, or
- Return a function as their output
Let’s use concrete examples to make this tangible.
Example 1: Functions as Input
Imagine we want a flexible calculator that can perform any operation (add, multiply, subtract) instead of just addition. We can write a higher-order function that takes two numbers and a function defining the operation:
// First, define a type to represent a function that takes two ints and returns an int interface IntOperation { int apply(int a, int b); } // Our higher-order function: accepts an IntOperation as input public int calculate(int a, int b, IntOperation operation) { // We execute the passed-in function here return operation.apply(a, b); } // Using it public static void main(String[] args) { // Pass an addition function (via lambda) int sum = calculate(5, 3, (x, y) -> x + y); // Pass a multiplication function int product = calculate(5, 3, (x, y) -> x * y); // Pass a custom function (e.g., square both and add) int squaredSum = calculate(5, 3, (x, y) -> (x*x) + (y*y)); }
Here, calculate doesn’t just take values—it takes a behavior (the operation to perform). That’s what "function as input" means.
Example 2: Functions as Output
We can also write functions that return other functions. For example, a function that generates custom adders with a fixed value:
// Returns a function that adds a fixed number to its input public IntOperation createAdder(int fixedAddend) { // Return a lambda function (this is the output function) return (x) -> x + fixedAddend; } // Using it public static void main(String[] args) { // Create a function that adds 5 to any number IntOperation add5 = createAdder(5); int result1 = add5.apply(10); // 15 // Create another function that adds 10 to any number IntOperation add10 = createAdder(10); int result2 = add10.apply(10); // 20 }
Here, createAdder doesn’t return a number—it returns a whole new function that we can reuse later. That’s "function as output."
Why This Matters
This pattern lets you write more flexible, reusable code. Instead of writing separate getaddition, getMultiplication, getSubtraction functions, you can have one calculate function that adapts to any operation. It’s also the backbone of tools like Java’s Stream API (where map, filter, and reduce all take functions as inputs) or Python’s map/filter built-ins.
内容的提问来源于stack exchange,提问作者Akash Bisariya

