Users Online

· Guests Online: 44

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Program to Demonstrate Pass by Value

C# Program to Demonstrate Pass by Value

 

This is a C# Program to demonstrate pass by value parameter.

Problem Description

This C# Program Demonstrates Pass by Value Parameter.

Problem Solution

Here Passing a value-type variable to a method by value means passing a copy of the variable to the method. Any changes to the parameter that take place inside the method have no affect on the original data stored in the argument variable.

Program/Source Code

Here is source code of the C# Program to Demonstrate Pass by Value Parameter . The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Demonstrate Pass by Value Parameter 
 */
using System;
class program
{
    static void Cube(int x)
    {
        x = x * x * x;
        Console.WriteLine("Value Within the Cube method : {0}", x);
    }
    static void Main()
    {
        int num = 5;
        Console.WriteLine("Value Before the Method is called : {0}", num);
        Cube(num);
        Console.WriteLine("Value After the Method is called : {0}", num);
        Console.ReadKey();
    }
}
Program Explanation

In this C# program, we are reading the two integer variables ‘num1’ and ‘num2’ respectively. In pass by value method, the value of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function.

 

In pass by value, the changes made to formal arguments in the called function have no effect on the values of actual arguments in the calling function. The swap() function is used to interchange the num1 and num2 variable values by using temporary variable temp.

Runtime Test Cases
 
Value Before the Method is called : 5
Value Within the Cube method : 125
Value After the Method is called : 5

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.90 seconds
10,843,768 unique visits