Function Overloading - 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.

# Function Overloading

#### Function Overloading Dersi Problem Çözümleri

The following two overloads take advantage of the existing `info()` overloads:

    void info(Meal meal) {
        info(meal.time);
        write('-');
        info(addDuration(meal.time, TimeOfDay(1, 30)));

        write(" Meal, Address: ", meal.address);
    }

    void info(DailyPlan plan) {
        info(plan.amMeeting);
        writeln();
        info(plan.lunch);
        writeln();
        info(plan.pmMeeting);
    }

Here is the entire program that uses all of these types:

    import std.stdio;

    struct TimeOfDay {
        int hour;
        int minute;
    }

    void info(TimeOfDay time) {
        writef("%02s:%02s", time.hour, time.minute);
    }

    TimeOfDay addDuration(TimeOfDay start,
                          TimeOfDay duration) {
        TimeOfDay result;

        result.minute = start.minute + duration.minute;
        result.hour = start.hour + duration.hour;
        result.hour += result.minute / 60;

        result.minute %= 60;
        result.hour %= 24;

        return result;
    }

    struct Meeting {
        string    topic;
        size_t    attendanceCount;
        TimeOfDay start;
        TimeOfDay end;
    }

    void info(Meeting meeting) {
        info(meeting.start);
        write('-');
        info(meeting.end);

        writef(" \"%s\" meeting with %s attendees",
               meeting.topic,
               meeting.attendanceCount);
    }

    struct Meal {
        TimeOfDay time;
        string    address;
    }

    void info(Meal meal) {
        info(meal.time);
        write('-');
        info(addDuration(meal.time, TimeOfDay(1, 30)));

        write(" Meal, Address: ", meal.address);
    }

    struct DailyPlan {
        Meeting amMeeting;
        Meal    lunch;
        Meeting pmMeeting;
    }

    void info(DailyPlan plan) {
        info(plan.amMeeting);
        writeln();
        info(plan.lunch);
        writeln();
        info(plan.pmMeeting);
    }

    void main() {
        immutable bikeRideMeeting = Meeting("Bike Ride", 4,
                                            TimeOfDay(10, 30),
                                            TimeOfDay(11, 45));

        immutable lunch = Meal(TimeOfDay(12, 30), "İstanbul");

        immutable budgetMeeting = Meeting("Budget", 8,
                                          TimeOfDay(15, 30),
                                          TimeOfDay(17, 30));

        immutable todaysPlan = DailyPlan(bikeRideMeeting,
                                         lunch,
                                         budgetMeeting);

        info(todaysPlan);
        writeln();
    }

That `main()` function can also be written with only object literals:

    void main() {
        info(DailyPlan(Meeting("Bike Ride", 4,
                               TimeOfDay(10, 30),
                               TimeOfDay(11, 45)),

                       Meal(TimeOfDay(12, 30), "İstanbul"),

                       Meeting("Budget", 8,
                               TimeOfDay(15, 30),
                               TimeOfDay(17, 30))));

        writeln();
    }

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