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?

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.