Function Double Reference Parameter - C# Programming Exercise

In this C# exercise, you are asked to write a function named "Double" that calculates the double of an integer number and modifies the data passed as an argument. This function must be a "void" function and should use reference parameters (using the ref keyword). For example, if the value of x is 5 and you call the function with Double(ref x), the value of x will be changed to 10. This exercise is a great opportunity to practice using reference parameters in C#, which allow you to modify the variables passed to the function directly. Additionally, you'll learn how to work with functions that don't return values but still perform modifications to parameters, an important skill when writing more complex programs.

 Category

Functions

 Exercise

Function Double Reference Parameter

 Objective

Write a C# function named "Double" to calculate the double of an integer number, and modify the data passed as an argument. It must be a "void" function and you must use "refererence parameters". For example.

x = 5;
Double(ref x);
Console.Write(x);

would display 10

 Write Your C# Exercise

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

class Program
{
    // Function to calculate and modify the number passed as a reference parameter
    public static void Double(ref int number)
    {
        // Multiply the input number by 2 to double it
        number = number * 2;
    }

    // Main method where the Double function is called
    public static void Main()
    {
        // Initialize the integer variable x
        int x = 5;

        // Call the Double function with x passed by reference
        Double(ref x);

        // Display the modified value of x (doubled)
        Console.Write(x);
    }
}

 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 swap reference parameters

    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 b...

  •  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...