Table of contents >> Functions > Methods and functions declaration
High importance article

Methods and functions declaration is actually a three step process: declaration, implementation and call of our method or function.

  • Declaration is the process of writing the method type, name and eventual parameters, so the program can successfully identify it.
  • Implementation of a method is writing the actual code that will be executed when the method is executed.
  • Call is the process of invoking the already declared and implemented method from some part of the program, where a problem that the method should solve, must be solved.

Declaration of a method cannot be done wherever we want. Although we haven’t yet learned about classes, we are going to mention them here because methods can only be declared inside the block of a class. As you have already seen in some of our previous examples, our Main method was declared inside the curly brackets of a class (the body of that class). So, for now, imagine a class like a structure defined by a name, having a body delimited by opening and closing curly brackets. In this context, a method declaration would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
using System;
 
namespace HelloWorld
{
    class Program
    { // opening brace of the class
        // declaration of our method inside the class body
        static void GreetTheWorld()
        { // opening brace of our method
            
        } // close brace of our method
    } // closing brace of the class
}

In the above example, we have declared a class named Program, and inside its block, a method called GreetTheWorld. Now, this method is only declared, but it doesn’t do anything, since its body contains no code. This is done in the implementation of the method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
 
namespace HelloWorld
{
    class Program
    { // opening brace of the class
        // declaration of our method inside the class body
        static void GreetTheWorld()
        { // opening brace of our method
            Console.WriteLine("Hello World!"); // implement some code to be executed
            Console.ReadLine();
        } // close brace of our method
    } // closing brace of the class
}

By placing the two instructions inside the body of our method, we have “implemented” it, meaning, we have given our method something to do when we will call it. Speaking of, even if we declare and implement our method, it still doesn’t do anything by itself. This is where a method call comes useful. In order for our method to run the code inside its block, we need to first call our method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
 
namespace HelloWorld
{
    class Program
    { // opening brace of the class
        
        static void Main(string[] args)
        {
            GreetTheWorld(); // call our method
            Console.Read();
        }
        
        // declaration of our method inside the class body
        static void GreetTheWorld()
        { // opening brace of our method
            Console.WriteLine("Hello World!");
        } // close brace of our method
    } // closing brace of the class
}

In the above example, we have declared and implemented our method. Aside of that, you can notice a second method which we used in a lot of our previous lessons, called Main() (this is the first method that executes when a console program is started). Inside the block of our Main() method, we called our second method, GreetTheWorld().

We will describe these aspects in separate future lessons, for now you just have to know that a method can be declared, implemented and called.

Function declarations are somehow identical to methods declarations, with the only difference that they must return a value, like we explained in the previous lesson. Lets consider the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
 
namespace HelloWorld
{
    class Program
    { // opening brace of the class
        
        static void Main(string[] args)
        {
            string myGreeting = GreetTheWorld(); // call our function and get its return value
            Console.WriteLine(myGreeting); // print our greeting on the console
            Console.Read();
        }
        
        // declaration of our function inside the class body
        static string GreetTheWorld()
        { // opening brace of our function
            string greeting = "Hello World!"; // implement our function
            return greeting; // mandatory return a value (of type string)
        } // close brace of our function
    } // closing brace of the class
}

In C# 6 and later, we can use expression bodied members to provide a concise syntax for methods and functions that consist of a single expression. An expression-bodied function consists of a single expression that returns a value (or performs an action in the case of a method), without needing the braces for its block of code and the return statement. Instead, it uses an arrow (=>) followed by the expression that represents the method's or function's implementation.

Here's how we can convert our GreetTheWorld() method from earlier to an expression-bodied method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
 
namespace HelloWorld
{
    class Program
    { // opening brace of the class
        
        static void Main(string[] args)
        {
            GreetTheWorld(); // call our method
            Console.Read();
        }
        
        // expression-bodied method declaration and implementation
        static void GreetTheWorld() => Console.WriteLine("Hello World!");
    } // closing brace of the class
}

As you can see, the method declaration and implementation are now in one line. We used the arrow (=>) to point to the method's implementation, which is just the Console.WriteLine() statement in this case.

Similarly, we can use expression-bodied syntax for our GreetTheWorld() function shown earlier:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
 
namespace HelloWorld
{
    class Program
    { // opening brace of the class
        
        static void Main(string[] args)
        {
            string myGreeting = GreetTheWorld(); // call our function and get its return value
            Console.WriteLine(myGreeting); // print our greeting on the console
            Console.Read();
        }
        
        // expression-bodied function declaration and implementation
        static string GreetTheWorld() => "Hello World!";
    } // closing brace of the class
}

Here, we removed the curly braces and the return statement from our function and used the arrow (=>) to point to the function's implementation, which is just the string "Hello World!" in this case.

If more than a single statement is needed in an expression bodied method, you can surround them in a block of code, just like a normal method. You can still use the arrow operator, but for functions, you need to use the return keyword.

Keep in mind that expression-bodied members are best suited for simple methods and functions with short and straightforward implementations. For more complex implementations, the traditional syntax shown at the beginning of this lesson, with curly braces and separate declaration and implementation is more appropriate. It is also important to note that programmers put a lot of accent on consistency. Don't declare half your methods one way, the other half another way.

The concepts explained in this lesson are also shown visually as part of the following video: