dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: I don't understand why my program is outputting the way it is

  1. #1
    Join Date
    Sep 2019
    Posts
    7

    I don't understand why my program is outputting the way it is

    I've written a program to demonstrate using the keyword ref to use pass-by-reference. I'm expecting this to be output:

    The value of x is 5
    The value of product is 25
    The value of x is 25

    But instead my program is showing this:

    The value of x is 5
    The value of product is 25
    The value of x is 5

    It does not appear that the final value of x in the Main method is being changed like it should be. Here is my code:
    Code:
    using System;
    
    class PassByReference
    {
        static void Main()
        {
            int x = 5;
            int product;
    
            Console.WriteLine("Value of x is: " + x);
            product = Multiply(ref x);
            Console.WriteLine("Value of product is: " + product);
            Console.WriteLine("Value of x is: " + x);
            Console.WriteLine();
        }
    
        static int Multiply(ref int a)
        {
            return a * a;
        }
    }

  2. #2
    Join Date
    Sep 2019
    Posts
    7
    Okay I solved it. I realized that the return statement in my method was in fact not changing the original value of the variable.

Similar Threads

  1. Outputting to excel
    By Newbie in forum C++
    Replies: 1
    Last Post: 08-09-2002, 11:55 AM
  2. Help to understand the program
    By pavel in forum C++
    Replies: 3
    Last Post: 03-28-2002, 03:31 PM
  3. cant understand this simple program
    By sysdeamon in forum C++
    Replies: 1
    Last Post: 02-21-2001, 08:45 AM
  4. Outputting results
    By Stephen in forum VB Classic
    Replies: 1
    Last Post: 01-19-2001, 07:03 PM
  5. XSL Outputting
    By Assaf in forum XML
    Replies: 0
    Last Post: 09-26-2000, 05:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center