V-Functions
Introduction To Functions In C#
In C# a function is defined as a technique of wrapping code to perform a certain task and then return a value. It is quite different than its predecessor programming languages like C or C++. Here the functions do not exist alone. Functions are a part of the OOPs approach.
The function is a member of the class. It is quite the same as a method and sometimes both the terms are used interchangeably. But there are few basic differences between methods and functions.
Difference Between Method And Functions
Although both are similar in many senses and languages. The basic difference between Methods and Functions is that the method comes with a void as return type whereas function has a return type.
If you look into the differences without selecting any particular programming language then the Function can be defined as a piece of code that has a name and arguments for operations mentioned inside the parenthesis. All the argument of a function is explicit by nature.
Now, on the other hand, the method can be seen as a function that acts as an object. It is a blueprint of a class instance. The method always has implicit arguments.
In this tutorial, we will be using the terms Methods and Functions interchangeably for the sake of convenience.
Functions In C#
A function allows the programmers to enclose a piece of code and then call that part of the code from another part of the program. It is quite useful when you need to run the same code from different places.
In C#, functions have the following syntax:
<Access Specifiers> <return type> <name of the function>(< function parameters>)
{
<function code>
return;
}
As we discussed in our previous tutorials, Access specifies, Parameters and return types are optional. Let’s create functions with different options.
V.1- User Defined Function
User-defined functions are routines that can take parameters, perform calculations or other actions, and return a result. You can write user-defined functions in any Microsoft .NET Framework programming language, such as Microsoft Visual Basic .NET or Microsoft Visual C#.
Examples:1
Examples:2
0 Comments