array_sum / array_product
array_sum(array $array): int|float — array_product(array $array): int|float
Numeric aggregation: array_sum adds and array_product multiplies; empty arrays return the additive/multiplicative identities 0 and 1; float accumulation follows array order.
C C23
#include <stdio.h>
int main(void) {
int xs[] = {2, 4, 6, 8};
int n = (int)(sizeof xs / sizeof xs[0]);
long long sum = 0, product = 1; // long long keeps the product from overflowing
for (int i = 0; i < n; i++) {
sum += xs[i];
product *= xs[i];
}
printf("%lld\n", sum);
printf("%lld\n", product);
printf("0 1\n"); // empty identities: 0 for sum, 1 for product
return 0;
}
20 384 0 1
C++ C++26
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> xs = {2, 4, 6, 8};
std::cout << std::accumulate(xs.begin(), xs.end(), 0LL) << "\n"; // 0LL fixes the type
std::cout << std::accumulate(xs.begin(), xs.end(), 1LL, std::multiplies<>()) << "\n";
std::cout << "0 1\n"; // empty identities
return 0;
}
20 384 0 1
C# v11.0.100
var xs = new[] { 2, 4, 6, 8 };
Console.WriteLine(xs.Sum()); // native
Console.WriteLine(xs.Aggregate(1, (a, b) => a * b)); // seed 1 = identity
Console.WriteLine($"{Array.Empty<int>().Sum()} {Array.Empty<int>().Aggregate(1, (a, b) => a * b)}");
20 384 0 1
Go v1.27.4
package main
import "fmt"
func main() {
xs := []int{2, 4, 6, 8}
sum, product := 0, 1 // the two identities
for _, x := range xs {
sum += x
product *= x
}
fmt.Println(sum)
fmt.Println(product)
fmt.Println(0, 1) // empty identities
}
20 384 0 1
Java v27.0.1
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] xs = {2, 4, 6, 8};
System.out.println(Arrays.stream(xs).sum()); // native
System.out.println(Arrays.stream(xs).reduce(1, (a, b) -> a * b));
int[] empty = {};
System.out.println(Arrays.stream(empty).sum() + " " + Arrays.stream(empty).reduce(1, (a, b) -> a * b));
}
}
20 384 0 1
JavaScript v26.13.1
const xs = [2, 4, 6, 8];
// seeds 0/1 are the additive/multiplicative identities
console.log(xs.reduce((a, b) => a + b, 0));
console.log(xs.reduce((a, b) => a * b, 1));
console.log([].reduce((a, b) => a + b, 0), [].reduce((a, b) => a * b, 1));
console.log([1.5, 2.5].reduce((a, b) => a + b, 0));
20 384 0 1 4
PHP v8.6.0
// array_sum / array_product aggregate values; the empty array
// yields the identities: 0 for sum, 1 for product
echo array_sum([2, 4, 6, 8]), "\n";
echo array_product([2, 4, 6, 8]), "\n";
echo array_sum([]), " ", array_product([]), "\n";
echo array_sum([1.5, 2.5]), "\n"; // float accumulation
20 384 0 1 4
Python v3.15.2
import math
print(sum([2, 4, 6, 8])) # native
print(math.prod([2, 4, 6, 8])) # native since 3.8
print(sum([]), math.prod([])) # identities: 0 and 1
print(sum([1.5, 2.5])) # floats print 4.0
20 384 0 1 4.0
Rust v1.99.1
fn main() {
let xs = [2, 4, 6, 8];
println!("{}", xs.iter().sum::<i32>()); // native, turbofish pins the type
println!("{}", xs.iter().product::<i32>()); // native
let empty: [i32; 0] = [];
println!("{} {}", empty.iter().sum::<i32>(), empty.iter().product::<i32>());
}
20 384 0 1
TypeScript v26.13.1
const xs: number[] = [2, 4, 6, 8];
console.log(xs.reduce((a: number, b: number) => a + b, 0));
console.log(xs.reduce((a: number, b: number) => a * b, 1));
console.log(([ ] as number[]).reduce((a: number, b: number) => a + b, 0),
([] as number[]).reduce((a: number, b: number) => a * b, 1));
20 384 0 1
Language differences
C: accumulate in long long to avoid overflow; empty-array semantics expressed with the identities (sum 0 / product 1).
C++: std::accumulate takes 0LL/1LL as the init to fix the accumulation type; a hand-written multiplication predicate gives the product.
C#: Sum() is native; the product uses Aggregate(1, (a,b) => a*b) — the seed 1 is the multiplicative identity.
Go: hand-written loop; sum, product := 0, 1 seeds both identities.
Java: IntStream.sum() is native; the product uses reduce(1, (a,b) -> a*b).
JavaScript: the reduce pair — seeds 0/1 are the additive/multiplicative identities (omitting the seed makes an empty array throw TypeError).
PHP: array_sum([]) is 0 and array_product([]) is 1 (identities); returns int|float; since 8.3 non-numeric elements emit E_WARNING; floating-point accumulation follows array order.
Python: sum() is native (floats print 4.0); math.prod() (3.8+) is native and returns 1 for an empty list.
Rust: Iterator::sum()/product() are native (the example pins the type with ::<i32>), and empty iterators yield the identities too.
TypeScript: same runtime as JS reduce; the number[] annotation type-checks the accumulator seed.