I'm having trouble using void functions in .NET fiddle

351 Views Asked by At

I'm currently taking an It1050 class, and I'm new to coding. I'm having an issue with using void functions with their program. I wanted to know if anyone could help me solve the problem. There's something wrong with the website, and no matter what I, do I can't get the code to work. Even after my instructor gave me some guidance on what to do. I have a link to the code you can make changes to. Any help will be greatly appreciated!

https://dotnetfiddle.net/fcen52

using System;
                    
public class Program
{
    public static void Main()
    {
        // Function - A code block that contains a series 
        // of statements. A program causes the statements 
        // To be exucuted by calling the function 
        // and specifying anya required parameters.
        
        // void function a function that does NOT have a
        // return value.
        
        // function type - the type of value the function 
        // returns
        // function name - The name you call the function 
        
        // parameter - Information that is passed into functions 
        // A parameter acts as a variable inside a function
        
        greeting("Cole");
        greeting("John");
            
        void greeting(String name)
        { 
        
            Console.WriteLine("Hello " + name); 
        }  
        
        // name is not valid. You cannot use name here. :(
        
    }
    //TEACHER'S COMMENT:
    //Put your function outside of the main function scope
    //Put "public static" before the return type.
    
    //Example below:
    //public static void greeting(String name) { /* Your code goes here */ }
}
1

There are 1 best solutions below

2
On BEST ANSWER

Methods inside other methods, are called Local functions (C# Programming Guide) and are available since C# 7.0.

.NET fiddle is apparently not supporting C# 7.0 when you select the .NET 4.7.2 compiler. Select either the .NET 5 or the Roslyn 3.8 complier or alternatively move the function out of the Main method to the class-level and make it static (because you are calling it from Main which is static as well).