hi guys i am newbie at programming as well. I have a problem in applying reference parameters.I underastand that they are aliases for other variables and they can modify variables in the original called function where as call by value parameters are just copies and cnnot modify the original variable in the called function.

here is my program

// time.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int ConverttoSeconds(int hours,int minutes,int &Sec);
int TimePassed(int &sec,int &s);

int main(int argc, char* argv[])
{   int hours,minutes,seconds;

cout << "Enter your first set of times :\n";
cin >> hours >> minutes >> seconds;
ConverttoSeconds(hours,minutes,seconds);

cout << "\n" << seconds << " is the amount 1st set of seconds     calculated\n";
cout << "Enter your second set of times :\n";
cin >> hours >> minutes >> seconds;
ConverttoSeconds(hours,minutes,seconds);
cout << "\n" << seconds << " is the amount 2nd set of seconds calculated\n";

    TimePassed(seconds,seconds);

    char s;
    cin >> s;

    return 0;
}

 int ConverttoSeconds(int hours, int minutes ,int &Sec){

int const lastime = 12 * 60 * 60;// twelve o clock converted to  seconds
    minutes = hours * 60;
    int seconds = minutes * 60;
    Sec = lastime - seconds;
    cout << Sec << " is the amount of time passed when the clock last\n ";
    cout << "time it struck twelve 'o clock " ;

    return Sec;
}
 int TimePassed(int &sec,int &s){
     int timedif= sec - s;
     cout << "time diffirence is " << timedif;
     return timedif;

 }

I have 2 questions about this program, it is my own program
1. why does the function TimePassed return 0 when i actually want a time difference between the 2 times that are entered ?
2. in function ConverttoSecondsint hours, int minutes ,int &Sec) if i change these lines

Sec = lastime - seconds;
cout << Sec << " is the amount of time passed when the clock last\n ";
cout << "time it struck twelve 'o clock " ;

return Sec;

to

nt sec = lastime - seconds;
cout << sec << " is the amount of time passed when the clock last\n ";
cout << "time it struck twelve 'o clock " ;

return sec;

it returns the original value for seconds instead of the time difference.
Why does it do this?

Dani AI

Generated

Two separate, easy-to-miss issues are happening in the thread.

First, when you call TimePassed(seconds, seconds) both parameters become aliases for the same object. Any subtraction of a value from itself yields 0. touched on that — use two different variables to hold the two converted times (for example sec1 and sec2) so the function compares two distinct values.

Second, changing the assignment inside the conversion function to a newly declared local variable (for example int sec = ...) does not modify the caller’s variable. C++ is case‑sensitive and a local sec is a different identifier from a parameter named Sec. Either assign the computed value to the reference parameter (so the caller sees the change) or return the computed value and capture it in main. Also avoid destroying the original minutes input by overwriting it; compute total minutes/seconds from both hours and minutes.

A simple, clear approach is to make the converter return the total seconds and keep names distinct:

int toSeconds(int hours, int minutes, int seconds) {
    return hours * 3600 + minutes * 60 + seconds;
}

int main() {
    // read h1,m1,s1 and h2,m2,s2
    int total1 = toSeconds(h1,m1,s1);
    int total2 = toSeconds(h2,m2,s2);
    std::cout << "difference: " << std::abs(total1 - total2) << '\n';
}

If you prefer output parameters, call the converter into two different variables and assign to the provided reference (do not create a new local with the same meaning). Use clear names (secA, secB) and consider const for inputs that should not be changed. For authoritative details on reference semantics and parameter passing see the C++ reference material: and .

Recommended Answers

All 2 Replies

1) sec and s refer to the same object, thus they have the same value. Subtracting 5 from 5 is 0. Replace 5 by x and you have the basis of what you're doing, x - x == 0.

2) It returns the time difference, but Sec remains the same because you don't modify it. Because you don't do anything with the return value, this isn't obvious.

thanks that expalins a lot i'll try it out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.