Function Double - C# Programming Exercise

In this C# exercise, you are asked to write a function named "Double" to calculate and return an integer number doubled. For example, when calling the Double(7) function, it should return 14. This exercise is great for practicing the use of functions that perform simple calculations and return results in C#. It's important to note that the expected input data type is an integer, and the output will also be an integer. Additionally, you'll learn how to use parameters within a function and how to return values from it. Through this exercise, you'll improve your skills in structuring functions in C# that perform specific tasks.

 Category

Functions

 Exercise

Function Double

 Objective

Write a C# function named "Double" to calculate and return an integer number doubled. For example. Double(7) should return 14.

 Write Your C# Exercise

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

class Program
{
    // Function to calculate and return the doubled value of an integer
    public static int Double(int number)
    {
        // Multiply the input number by 2 to get the doubled value
        return number * 2;
    }

    // Main method where the Double function is called
    public static void Main()
    {
        // Test the Double function with an example number
        int result = Double(7);

        // Display the doubled value of 7
        Console.WriteLine("Double(7) returns: {0}", result);
    }
}

 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 Double reference parameter

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

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