Function Swap Reference Parameters - C# Programming Exercise

In this C# exercise, you are asked to write a function named "Swap" that swaps the values of two integer numbers, which are passed by reference. This is done by using the ref keyword so that the function can modify the values of the variables directly. An example of usage would be as follows: if you have the variables x with value 5 and y with value 3, calling the function Swap(ref x, ref y) will swap the values of x and y, and the result shown would be "x=3, y=5". This exercise will help you understand how value swapping works using reference parameters in C#, which is a crucial skill for manipulating data directly without needing to return values.

 Category

Functions

 Exercise

Function Swap Reference Parameters

 Objective

Write a C# function named "Swap" to swap the values of two integer numbers, which are passed by reference.

An example of use might be:

int x=5, y=3;
Swap(ref x, ref y);
Console.WriteLine("x={0}, y={1}", x, y);
(which should write "x=3, y=5")

 Write Your C# Exercise

// Importing the System namespace to access basic system functions
using System;

class Program
{
    // Function to swap the values of two integers using reference parameters
    public static void Swap(ref int a, ref int b)
    {
        // Temporary variable to hold the value of 'a'
        int temp = a;

        // Swap the values of 'a' and 'b'
        a = b;
        b = temp;
    }

    // Main method where the Swap function is called
    public static void Main()
    {
        // Initialize two integer variables
        int x = 5, y = 3;

        // Call the Swap function with x and y passed by reference
        Swap(ref x, ref y);

        // Display the swapped values of x and y
        Console.WriteLine("x={0}, y={1}", x, y);
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Function power local variables

    In this C# exercise, you are asked to write a function named "Power" that calculates the result of raising an integer number to another positive integer number. The ...

  •  Function recursive power

    In this C# exercise, you are asked to write a function that calculates the result of raising one integer to another integer using recursion. For example, raising 5 to...

  •  Function Fibonacci

    In this C# exercise, you are asked to write a program that uses recursion to calculate a number in the Fibonacci series. The Fibonacci series is a seque...

  •  Function modify a letter in a string

    In this exercise of C#, you will learn how to create a function named ChangeChar that modifies a letter at a specific position (starting from 0) in a st...

  •  Function IsPrimeTarea

    In this exercise of C#, you will learn how to create a function named IsPrime that receives an integer number and returns true if the number is p...

  •  Function Parameters of Main, Sum

    In this exercise of C#, you will learn how to create a program named sum that receives two integer numbers from the command line and displays their sum. This t...