CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Methods

  1. #1
    Join Date
    Oct 2011
    Posts
    3

    Arrow Methods

    I am learning methods in school and with this program I enter the starting distance type "feet"
    it asks me for a double value for it and I entered 12 and I wanted to convert it to yards. It should then let me convert it to imperial or metric units but the out put says 0.0 feet = 0.0 yards.
    I debugged the program and my double ft had the value 12 until the method went to its return statement. Does anybody know how to fix this? My code is not that great but it is in the link below. Help is appreciated.
    Attached Files Attached Files

  2. #2
    Join Date
    Aug 2011
    Location
    West Yorkshire, U.K.
    Posts
    54

    Re: Methods

    You have encountered the classic "call by reference / call by value" problem. Your code is written with the belief that a variable passed to a function will be altered by the function but in reality it is not. It is best illustrated by a simple code example:
    Code:
    public class Testing
    {
        public static void main (String[] args) {
            int input = 23;
            increment(input, 12);
            System.out.println("Main: (1) input now: " + input);
    
            // This time use the return value
            input = increment(input, 12);
            System.out.println("Main: (2) input now: " + input);
        }
    
        public static int increment(int input, int incrementValue) {
            System.out.println("Input : " + input);
            input += incrementValue;
            System.out.println("Input now: " + input);
            return input;
        }
    }
    output:
    Input : 23
    Input now: 35
    Main: (1) input now: 23
    Input : 23
    Input now: 35
    Main: (2) input now: 35

    Decent explanation here: http://www.yoda.arachsys.com/java/passing.html

  3. #3
    Join Date
    Oct 2011
    Posts
    3

    Re: Methods

    Thanks. I'll try the link once I get out of school today because my school blocked the website.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured