Variable Number of Parameters - D Programming Language
Description: D Programming Language
Keywords: D programming language

Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Calendar
  - Discord (community-run)
  - Forums
  - IRC
  - Wiki
  - Issues
  - Contributing
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# Variable Number of Parameters

#### Variable Number of Parameters Dersi Problem Çözümleri

For the `calculate()` function to be able to take variable number of parameters, its parameter list must include a slice of `Calculation` followed by `...`:

    double[] calculate(Calculation[] calculations...) {
        double[] results;

        foreach (calculation; calculations) {
            final switch (calculation.op) {

            case Operation.add:
                results ~= calculation.first + calculation.second;
                break;

            case Operation.subtract:
                results ~= calculation.first - calculation.second;
                break;

            case Operation.multiply:
                results ~= calculation.first * calculation.second;
                break;

            case Operation.divide:
                results ~= calculation.first / calculation.second;
                break;
            }
        }

        return results;
    }

Each calculation is evaluated inside a loop and their results are appended to a slice of type `double[]`.

Here is the entire program:

    import std.stdio;

    enum Operation { add, subtract, multiply, divide }

    struct Calculation {
        Operation op;
        double first;
        double second;
    }

    double[] calculate(Calculation[] calculations...) {
        double[] results;

        foreach (calculation; calculations) {
            final switch (calculation.op) {

            case Operation.add:
                results ~= calculation.first + calculation.second;
                break;

            case Operation.subtract:
                results ~= calculation.first - calculation.second;
                break;

            case Operation.multiply:
                results ~= calculation.first * calculation.second;
                break;

            case Operation.divide:
                results ~= calculation.first / calculation.second;
                break;
            }
        }

        return results;
    }

    void main() {
        writeln(calculate(Calculation(Operation.add, 1.1, 2.2),
                          Calculation(Operation.subtract, 3.3, 4.4),
                          Calculation(Operation.multiply, 5.5, 6.6),
                          Calculation(Operation.divide, 7.7, 8.8)));
    }

The output:

    [3.3, -1.1, 36.3, 0.875]

Copyright © 1999-2026 by the D Language Foundation | Page generated by Ddoc on Wed Aug 19 13:28:52 2026