An article discussing user-defined functions in C# Language with examples.
There are four types of user-defined functions in C#:
Functions with No Argument and No Return Type.
Functions with Argument and No Return Type.
Functions with No Argument and with Return Type.
Functions with Argument and with Return Type.
Definition: A function that does not receive any data from the calling function and does not return a value.
Details:
No data transfer between calling and called functions.
Cannot be used in an expression; only as an independent statement.
Function: Sum
(returns void).
Output:
"Sum of 10 and 20 is 30"
using System;
namespace FunctionDemo {
class Program {
static void Main(string[] args) {
Sum();
Console.ReadKey();
}
static void Sum() {
int x = 10;
int y = 20;
int sum = x + y;
Console.WriteLine($"Sum of {x} and {y} is {sum}");
}
}
}
Definition: A function that receives no data from the calling function but returns a value.
Data Transfer: Data is transferred from called function back to the calling function.
Function: Sum
returns an integer value.
Output:
"Sum is 30"
using System;
namespace FunctionDemo {
class Program {
static void Main(string[] args) {
int Result = Sum();
Console.WriteLine($"Sum is {Result}");
Console.ReadKey();
}
static int Sum() {
int x = 10;
int y = 20;
return x + y;
}
}
}
Definition: A function that receives data from the calling function via arguments but does not return a value.
Data Transfer: Data is transferred from the calling function to the called function, but not vice versa.
Function: Sum
receives two integer arguments but returns no value.
Output:
"Sum is 30"
using System;
namespace FunctionDemo {
class Program {
static void Main(string[] args) {
int x = 10, y = 20;
Sum(x, y);
Console.ReadKey();
}
static void Sum(int x, int y) {
int sum = x + y;
Console.WriteLine($"Sum is {sum}");
}
}
}
Definition: A function that receives input via arguments and returns a value, functioning like a “black box.”
Data Communication: Two-way data transfer.
Function: Sum
takes two arguments and returns the sum.
Output:
"Sum is 30"
using System;
namespace FunctionDemo {
class Program {
static void Main(string[] args) {
int x = 10, y = 20;
int Result = Sum(x, y);
Console.WriteLine($"Sum is {Result}");
Console.ReadKey();
}
static int Sum(int x, int y) {
return x + y;
}
}
}
Definition: Writing multiple functions with the same name but different argument or parameter lists.
Example:
add(int x, int y)
and add(int x, int y, int z)
are valid overloads.
Benefit: Simplifies naming and keeps related functionality grouped together.
Function that adds integers:
using System;
namespace FunctionDemo {
class Program {
static void Main(string[] args) {
int a = 10, b = 2;
int c = add(a, b);
Console.WriteLine($"Sum of {a} and {b} is {c}");
int d = add(a, b, c);
Console.ReadKey();
}
static int add(int x, int y) {
return x + y;
}
static int add(int x, int y, int z) {
return x + y + z;
}
}
}
Output:
"Sum of 10 and 2 is 12"
Further check shows multiple overloads work properly based on parameter count.
User-defined functions are a key feature in C#, enabling various forms of functionality through arguments and return types. Understanding these concepts is vital for effective programming in C#. Functions can improve code readability, maintainability, and reusability.