help!! call-by-reference and call-by-value

This is a discussion on help!! call-by-reference and call-by-value within the C Programming forums, part of the General Programming Boards category; I have to write a program that converts a number of minutes to days, hours, and minutes, converts days, hours, ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    Angry help!! call-by-reference and call-by-value

    I have to write a program that converts a number of minutes to days, hours, and minutes, converts days, hours, and minutes to minutes and computes the difference in two times in days, hours, and minutes format. I was given a drawing of the program on how it is supposed to work and from that i have written prototypes for the program. I have not seen any call-by-vaule or call by reference examples in tutorials so im kind of lost. do i need to #define days, minutes, and hours?
    Code:
     
    #include <stdio.h>
    #include <math.h>
    #define 
    
    void displayMenu();
    void needMenu(); 								// ask the user whether or not he/she wants
    													// to display the menu
    int makeSelection();							 // return a GOOD selection from the menu
    													// i.e. error check the user entry
    void processTask( int task ); 			// use a switch statement
    													// to call one of the following
    void processTask_1();
    void processTask_2();
    void processTask_3(); 						// convert both days, hours, mins to minutes
    													// apply the absolute function to the difference
    													// convert the difference minutes to
    													// days, hours, mins
    int getMinutes();
    void getDhm ( int *days, int *hours, int *mins );
    void min2dhm( int minutes, int *days, int *hours, int *mins );
    int dhm2min( int days, int hours, int mins );
    
    int main (void);
    {
    													//define variables
    	double days;
    	double hours;
    	double minutes;

    All i need is help getting started on these call-by-value and call-by-reference, preferable making sure i #define is correct and im headed in the right dir. thanks.
    Thanks

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There is no call-by-reference in C. If you want to modify a passed in value, you pass a pointer to it.

    Code:
    void foo(int* x)
    {
        *x = 1;
    }
    
    int main()
    {
        int n;
        foo(&n);
        /*n is now 1*/
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by geoffr0 View Post
    do i need to #define days, minutes, and hours?
    Not unless you want to. In a nutshell, the difference between "pass by reference" and "pass by value" w/r/t to ints is this:
    Code:
    void myfunc (int x);  // pass by value
    void myfunc (int *x); // pass by reference
    Pass by reference means you get an actual memory address of a value, so you could change the value in that memory location. By value means the value is copied into a new named variable with it's own storage on the function's stack, so you can change that value in the function without affecting the original source variable.

    ps. technically anon is correct, it is all pass by value in C, since a pointer's value is a memory address. But functionally you can use this distinction.
    Last edited by MK27; 04-01-2009 at 10:20 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    yea, my teacher calls it call-by-value and call-by-reference parameters, it just changes the value of a unit with another. Im still kind of clueless on where to begin, should i start with my menu or should i put my needMenu before displayMenu? Im trying to makle it where i ask the user to press y or n to display a menu, then displays menu based on char.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Where you put functions doesn't matter.
    But it looks like you have go to do some more studying of the basics.
    You leave an empty #define statement. You also put a terminating ; after main's definition, both of which are wrong.

    Then start by drawing a flow chart or pseudo code on how you would do this in real life.
    Then you should be able to translate that into code.
    Start with basics first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by geoffr0 View Post
    yea, my teacher calls it call-by-value and call-by-reference parameters, it just changes the value of a unit with another. Im still kind of clueless on where to begin, should i start with my menu or should i put my needMenu before displayMenu? Im trying to makle it where i ask the user to press y or n to display a menu, then displays menu based on char.
    Please don't try and build a "piano" here! This is a class assignment, not a program for a king. Have it go right to the menu, and get on with it.

    Sure, define a minute, an hour, and day. It's good practice (not necessary, but good to practice these things). Keep everything based on seconds, for consistency.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    should i include
    Code:
     
    void getInputValues (double *hours, double *minutes, double *hours);

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    How about just writing the code in the main function first and splitting it later? Make a flowchart!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    can someone please provide some positive feedback like an example. rather then do this do that?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If by "positive feedback" you mean "do it for me", then no. What are you stuck on specifically? Why don't you do as suggested, and make a small example with just main and do the simplest form of the task you are stuck on? Once you know that you can do that right, incorporate it into your other program.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    void getDhm ( int *days, int *hours, int *mins );

    Would be an example of call-by-reference. In the code that calls this function, you'd use the following syntax:

    Code:
    int dd, hh, mm;
    
    getDhm( &dd, &hh, &mm);
    Note - the ampersand ("&") preceding each parameter. This allows the getDhm function to change the variables dd, hh, mm which are defined outside of the function.

    Inside the function you'd do whatever it takes to obtain the values for hours, minutes and seconds, and you'd assign them to the passed parameters thus:

    *days = some number
    *hours = some number
    *mins = some number

    Note the asterisk ("*") preceding the variables. This indicates you are assigning 'through' the pointer - affecting something outside of this function.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by geoffr0 View Post
    can someone please provide some positive feedback like an example. rather then do this do that?
    qv. vart's sig ("My effort to help you will never exceed YOUR effort to explain the problem.")

    If you are not getting the answers you want, try THINKING about how to ask your question better, because this one just screams of someone throwing up their hands in a vague way and saying "look at my program, look at my assignment specs, what should I do?" Which might translate to: "I haven't come up with anything, I hope someone else can, in fact I can't even come up with a decent summary of my issue."

    You have gotten some good feedback re: the difference between a value and a reference. C is a (comparatively) low level procedural language, approach it that way, and ask more specific questions.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by MK27 View Post
    ps. technically anon is correct, it is all pass by value in C, since a pointer's value is a memory address. But functionally you can use this distinction.
    Not really. At least I wouldn't recommend it. It's wrong to assume that someone is passing a pointer to an integer if someone says "I pass int i by reference to foo()". Mainly because it's ambiguous and has a well-defined meaning when it comes to C++. So avoid the distinction in C, among other reasons.

    For example,
    Code:
    int x = 5;
    int * y = &x;
    
    foo(&y);
    
    /* ... */
    
    void foo(int ** z)
    {
       /* ... */
    }
    What would you call that? Are you passing x by reference or y by reference? :-). Yet another reason to avoid it.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zacs7 View Post
    What would you call that? Are you passing x by reference or y by reference? :-).
    I would say both, to be honest. And I'm aware that the general concept of "references" has a more significant meaning in other languages. However, in some of those languages, "pointer" and pointing has no apparent significant (if you said a reference pointed to a variable, you could get chided! The reference reffers to the variable.) Some of those languages are implemented in C, of course.

    I'm not familiar with C++, but I do believe the "call by reference" concept perhaps derived from there is important in interpreted languages that (once again) are actually implemented in C. So what do I mean? Maybe I dunno what I mean: pass by reference, pass by value, it's all one or the other but just C somewhere, mr.zacs7, so why erase the distinction?

    After all, you know what they should say, "Do as the Romans do, When in C, C++" ???

    [edit] Oh yay... look it's Adak watch
    Last edited by MK27; 04-01-2009 at 07:36 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're talking about getting

    two different times

    calculating the difference between them

    and printing the result.

    This should be about 12-20 lines of code. Two functions and main() is more than enough.
    Code:
    //Just an idea, not ready to run code
    void getTimes(time1 *start, time1 *stop)  {
        scanf("%d , &start.hours);
       scanf("%d", &stop.hours);
       //etc. for minutes and seconds.
    }
    
    void calculatAndPrint(time1 *start, time1* stop)  {
       time *temp;
       //subtract start.hours from stop.hours
       // assign answer to temp.hours  
    
       //repeat subtractions and assignments for minutes and seconds
        //then print it.
        printf("\nHours: %d",  temp.hours);
       //include  minutes and seconds in your final print statement
    }  
    
    /*
    In main() you have two structs I call time1 here, with members of hours, minutes, and seconds.
    */
    This is not the only way, it's just one way to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  4. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 04:30 AM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21