Function Returning A Value - C# Programming Exercise

In this C# exercise, you are asked to write a program where the Main method must be like this:


public static void Main()
{
int x = 3;
int y = 5;
Console.WriteLine(Sum(x, y));
}

The goal of this exercise is to define the Sum function, which must accept two int parameters (in this case, the values 3 and 5) and return an int value, which will be the sum of those two numbers. This exercise is meant to practice creating functions that accept parameters and return values in C#, helping you understand how functions are used within the program's execution flow.

 Category

Functions

 Exercise

Function Returning A Value

 Objective

Write a C# program whose Main must be like this:

public static void Main()
{
int x= 3;
int y = 5;
Console.WriteLine( Sum(x,y) );
}

"Sum" is a function that you must define and that will be called from inside Main. As you can see in the example, it must accept two integers as parameters, and it must return an integer number (the sum of those two numbers).

 Write Your C# Exercise

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

class Program
{
    // Function to calculate the sum of two integers and return the result
    public static int Sum(int a, int b)
    {
        // Return the sum of a and b
        return a + b;
    }

    // Main method to call the Sum function and display the result
    public static void Main()
    {
        // Declare two integer variables
        int x = 3;  // First number
        int y = 5;  // Second number

        // Call the Sum function with x and y as arguments, and display the result
        Console.WriteLine(Sum(x, y));  // This will print 8 (3 + 5)
    }
}

 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 returning a value V2

    In this C# exercise, you are asked to write a program where the Main method must be like this:public static void Main(){ Console.WriteLi...

  •  Function write centered

    In this C# exercise, you are asked to write a function that displays the given text centered on the screen (assuming a screen width of 80 characters). The code should...

  •  Function write underlined

    In this C# exercise, you are asked to write a function that displays the given text centered on the screen (assuming a screen width of 80 characters) and then underli...

  •  Function sum of array

    In this C# exercise, you are asked to write a program to calculate the sum of the elements in an array. The Main function should look like the following:<...

  •  Function double

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