Function With Parameters - 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()
{
SayHello("John");
SayGoodbye();
}

The goal of this exercise is to define the SayHello and SayGoodbye functions. The SayHello function must accept a string parameter, which will represent the name of the person to greet (in this case, "John"). The SayGoodbye function will handle displaying a farewell message. This exercise is meant to practice creating functions that accept parameters in C# and understanding how they are called within the program's execution flow.

 Category

Functions

 Exercise

Function With Parameters

 Objective

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

public static void Main()
{
SayHello("John");
SayGoodbye();
}

SayHello and SayGoodbye are functions that you must define and that will be called from inside Main. As you can see in the example. SayHello must accept an string as a parameter.

 Write Your C# Exercise

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

class Program
{
    // Function to print a greeting message with a name as a parameter
    public static void SayHello(string name)
    {
        // Output a greeting message with the user's name
        Console.WriteLine($"Hello, {name}! Welcome to the program.");
    }

    // Function to print a farewell message
    public static void SayGoodbye()
    {
        // Output a farewell message
        Console.WriteLine("Goodbye, thanks for using the program!");
    }

    // Main method to call SayHello and SayGoodbye functions
    public static void Main()
    {
        // Call the SayHello function with "John" as the parameter
        SayHello("John");

        // Call the SayGoodbye function to bid farewell to the user
        SayGoodbye();
    }
}

 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

    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;

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