Introduction to C# Functions
C# functions are the essential parts of the C# program that can be consisting of a number of elements, such as the function name that is used as the function’s reference, return types of the data operated in the functions, logical body of the function, parameters that can be passed as arguments for the function, and the Access Specifier for defining the accessibility of the function inside the program. The different types of functions that can be integrated into a C# program are a combination of functions with or without parameters, which can or cannot have the return values, depending on the requirement provided.
There are several components in functions that follow as-.
- To make a function call, we have a unique name called Function name.
- To specify the data type of return value, we will use the Return Type.
- The block of statements that contains the executable statements is called Body.
- We can pass the functions during the function call as a list of arguments called Parameters.
- To specify the accessibility of function in the application, we can use the Access specifier.
Different C# Function
- without parameters(arguments) and without return type
- with parameters(arguments) but without return type
- Using with parameters(arguments) and with return type
- without parameters(arguments) and with the return value
C# Function Syntax
<access-specifier><return-type>FunctionName(<parameters>)
{
// function body
// return statement
}
In the above syntax, Return statements, parameters, and Access-specifier are optional.
Functional Aspects | Syntax(Function) |
With parameters and with return values |
Declaration: int display ( int );
Function call: display ( value ); Function definition: |
With parameters and without return values |
Declaration: void display ( int );
Call: display (value); Function definition: |
Without parameters and without return values |
Declaration: void display ();
Call: display (); Definition: |
Without parameters and with return values |
Declaration: int display ( );
Call: display ( ); Definition: |
If the return value of a function is “void” then, it cannot return any values to the calling function.
1. Using Without Parameters and Without Return Type
The function with no parameter and no return type, a function that does not return any of the values here we specified as void type as a return type value. In this program, there should not be passed any values to the function call Display(), and also, there are no values that are returned from this function call to the main function. Let’s see the example with a function build without a return type and parameter,
Example
Code:
using System;
namespace FunctionSamples
{
class Program_A
{
// User defined function without return type and parameter
public void Display()
{
Console.WriteLine("Non Parameterized Function"); // No return statement
}
static void Main(string[] args) // Main Program
{
Program_A program = new Program_A (); // to create a new Object
program.Display(); // Call the Function
}
}
}
Output:
2. Using With Parameters (Arguments) and Without Return Type
In this program, a string is passed as a parameter to the function. This function’s return type is “void”, and no values can be returned from the function. The value of the string is manipulated and displayed inside the function itself.
Example
Code:
using System;
namespace FunctionSample
{
class Program_B
{
public void Display(string value) // User defined function without return type
{
Console.WriteLine("Hello " + value); // No return statement
}
static void Main(string[] args) // Main function
{
Program_B program = new Program_B(); // Creating Objec
program.Display("Welcome to C# Functions"); // Calling Function
}
}
}
Output:
3. Using With Parameters (Arguments) and with Return Type
In this program, a string is passed as a parameter to the function. The return type of this function is “string”, and the return value of the string can be returned from the function. The value of the string is manipulated and displayed inside the function itself.
Example
Code:
using System;
namespace FunctionsSample
{
class Program_C
{
// User defined function
public string Show(string message)
{
Console.WriteLine("Inside the Show Function Call");
return message;
}
// Main function
static void Main(string[] args)
{
Program_C program = new Program_C();
string message = program.Show("C# Functions");
Console.WriteLine("Hello "+message);
}
}
}
Output:
4. Using Without Parameters (Arguments) and with Return Value
In this program, there will be not passed any arguments or parameters to the function “calculate”, but to the main function, the values are returned from this calculate () function call. The variables a and b values are calculated in the function call “calculate”, and in the main function sum of these values is returned as a result.
Example
Code:
using System;
namespace FunctionsSample
{
class Program_D
{
public void calculate()
{
int a = 50, b = 80, sum;
sum = a + b;
Console.WriteLine("Calculating the given to values: " +sum);
}
static void Main(string[] args) // Main function
{
Program_D addition =new Program_D();
addition.calculate();
}
}
}
Output:
C# Passing Parameters to Methods
When we are creating a method with arguments/parameters in c#, we must pass arguments/parameters to that specified method when calling our application’s function. We have several ways to pass parameters to the method; let’s see the parameters/arguments.
Parameters | Description |
Value Parameters | Value parameters are called the “input parameters”. Instead of the original parameters, the input parameters will pass a copy of the original value; due to that, there will not be any cause or changes made to the parameter during the called method, and it will not affect on original values while the control passes to the caller function. |
Reference Parameters | Reference parameters are called the “input/output parameters”. The reference parameter will pass the reference memory of the original parameters. Thus, the changes/alteration made to the parameters in called method, while the control returns to the caller function, affects the original values. |
Output Parameters |
It is an “output parameter”; these are like the reference type parameters. The only difference is there is no need to initialize it before passing the data. |
Conclusion – C# Functions
In this article, we well-read the usage of the functions/ methods available in C# and learned the different C# functions. I hope this article would have helped you out in understanding the several functional aspects in C#.
Recommended Articles
This has been a guide to C# Functions. Here we discussed the basic concepts and different types of C# functions with their syntax for better understanding. You can also go through our other suggested articles to learn more –
6 Online Courses | 18 Hands-on Project | 90+ Hours | Verifiable Certificate of Completion
4.6
View Course
Related Courses