Thread: Confused on structures - functions example [Noob Question]

  1. #1
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33

    Confused on structures - functions example [Noob Question]

    Hi,

    I have been reading through my c++ book and came across an example I do not fully understand, with very little explanation given. I am hoping someone from these forums can better explain what is going on here.

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct travel_time
    {
        int hours;
        int mins;
    };
     
    const int Mins_per_hour = 60;
    
    travel_time sum(travel_time t1, travel_time t2);
    void show_time(travel_time t);
    
    int main()
    {
    
        travel_time day1 = { 5, 45 }; // 5hrs 45 min
        travel_time day2 = { 4, 55 }; // 4hrs 55 min
    
        travel_time trip = sum(day1, day2);
        cout << "Two-day total: ";
        show_time(trip);
    
        travel_time day3 = { 4, 32 };
        cout << "Three-day total: ";
        show_time(sum(trip, day3));
    
         
    
        cin.get();
        cin.get();
        return 0;
    }
    
    travel_time sum(travel_time t1, travel_time t2)
    {
        travel_time total;
    
        total.mins = (t1.mins + t2.mins) % Mins_per_hour;
        total.hours = t1.hours + t2.hours + (t1.mins + t2.mins) / Mins_per_hour;
     
        
        return total;
    }
    
    void show_time(travel_time t)
    {
        cout << t.hours << " hours, "
            << t.mins << " minutes\n";
    }
    On line 13 there is a function header named sum, yet on line 19-20 they are accessing that function with different variable names. (I dont get why I just assumed they where objects being created, so I can let that pass).

    However on line 22 (day1 and day2) data is passed to the sum function while being stored in the variable trip, so it can be accessed from main. How exactly this happening?

    day 1 has two paramaters, day 2 also has two paramets
    so how does it fit inside the sum function? The only way I see is by adding them together....

    so day1 = 50
    day 2 = 59

    so the following code:
    travel_time trip = sum(day1, day2);
    would look like:
    travel_time trip = sum(50, 59);

    But then going to line 41 makes no sense
    total.mins = (t1.mins + t2.mins) % Mins_per_hour;
    total.mins = 50 + 59 % 60 which is 49 which the compiler does not output.
    "Don't quit. Suffer now and live the rest of your life as a champion"
    - Muhammad Ali


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by immy
    On line 13 there is a function header named sum, yet on line 19-20 they are accessing that function with different variable names. (I dont get why I just assumed they where objects being created, so I can let that pass).
    On lines 19 and 20 two variables are declared. This has nothing to do with the function named sum. What these variable declarations and function declaration have in common is that the variables are of type travel_time and the function's return type is travel_time, but that does not mean that "they are accessing that function with different variable names".

    Quote Originally Posted by immy
    However on line 22 (day1 and day2) data is passed to the sum function while being stored in the variable trip, so it can be accessed from main. How exactly this happening?
    The function returns a value.

    Quote Originally Posted by immy
    day 1 has two paramaters, day 2 also has two paramets
    so how does it fit inside the sum function?
    We say that the two travel_time objects have members, not parameters. They "fit inside the sum function" because the function has two parameters, each of type travel_time, i.e., copies of the function arguments (i.e., day1 and day2) are made, and for this function call, they correspond to t1 and t2 in the sum function.

    Quote Originally Posted by immy
    The only way I see is by adding them together....

    so day1 = 50
    day 2 = 59

    so the following code:
    travel_time trip = sum(day1, day2);
    would look like:
    travel_time trip = sum(50, 59);
    No, that is a bad misconception. day1 is not equal to 50. Rather, day1 is a travel_time object for which the hours member is equal to 5 and the mins member is equal to 45. It is like you have a box containing two coins: you can pass around the box with the coins inside without having to melt down the coins to form a single big lump of metal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Namespace & <iostream> help required for a confused noob
    By W3ari3dS0ul in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2012, 05:37 AM
  2. Noob question about functions
    By spark* in forum C++ Programming
    Replies: 10
    Last Post: 03-19-2012, 03:55 AM
  3. Confused about writing functions
    By chickenlittle in forum C++ Programming
    Replies: 5
    Last Post: 11-27-2010, 11:52 PM
  4. Noob getting confused by booleans
    By Nathan the noob in forum C++ Programming
    Replies: 7
    Last Post: 06-27-2008, 03:50 PM
  5. Lesson 4: Functions noob question
    By NiVaG in forum C++ Programming
    Replies: 8
    Last Post: 09-24-2004, 05:12 PM