Subprograms, or subroutines, are block of code that contains a series of statements and can be invoked from other remote locations in a program (Shute, n.d.).
In C#, subprograms are known as “methods” and consists of (Point, 2018):
- access modifier
- return type
- method name
- parameter list
- method body
There are also other optional modifiers like abstract
, virtual
, async
, override
, partial
, static
, etc.
<access modifier> <return type> <method name>(parameter list) {
method body
}
Here is a simple example of a method called Max
that receives 2 parameters first
and second
and returns an int
.
public int Max(int first, int second)
{
if (first > second)
{
return first;
}
return second;
}
Access modifier
Access modifiers control whether a particular method can be used from other assemblies, from other classes or structs, or inside derived classes. There are 5 types of accessibility levels in C# (Docs, 2015):
public
private
protected
internal
protected internal
private internal
Return type
A method may return a value to the caller and the return type of the method is the data type of the value being returned. If the method does not return any value, the return type will be void
.
The return
keyword is used to terminate the execution of current method and transfer the control back to the caller, optionally returning a value. However, return
is not required for void
methods (Docs, 2015).
For example, this is a method Max
that returns an int
:
public int Max(int first, int second)
{
if (first > second)
{
return first;
}
return second;
}
Here is a void
method that uses a return
statement for early exiting of the function. The pattern is commonly known as “guard clauses” (Cunningham & Cunningham, n.d.).
public void Initialize(string name, int age)
{
if (string.IsNullOrEmpty(name)) return;
if (age < 0) return;
// ... other operations
}
Parameter list
Parameters are used for passing data to the method. A parameter list is a list of parameters along with their:
- name
- type
that are enclosed within a pair of parenthesis. The order of the parameters in the list is also important to the caller. Note that parameters are optional; that is - a method may not receive any parameters.
Parameters vs. Arguments
The method definition specifies the parameters that are required; the caller specifies the arguments that contains the concrete values for the parameters.
In the following example: a
is the argument (from the caller side); while num
is a parameter (for the method Square
).
void Main()
{
int a = 1;
// a is the argument
Square(a);
}
// num is a parameter
int Square(int num)
{
return num * num;
}
References
- Shute, G. Subprograms. Retrieved from https://www.d.umn.edu/ gshute/asm/subprograms.xhtml
- Point, T. (2018). C# - Methods. Retrieved from https://www.tutorialspoint.com/csharp/csharp_methods.htm
- Docs, M. (2015, July 20). Access Modifiers (C# Programming Guide). Retrieved from https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers
- Docs, M. (2015, July 20). return (C# Reference). Retrieved from https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/return
- Cunningham & Cunningham, I. Guard Clause. Retrieved from http://wiki.c2.com/?GuardClause