C# parameter is nothing, just a way to passing value to the function and then function calculate the value and then returns appropriate results. You can understand C# function parameter clearly in the following example.
Example:
using System;
namespace Understanding_Parameter
{
class Program
{
// function with parameter
public static int power(int num1)
{
int result;
result = num1 * num1;
return result;
}
static void Main(string[] args)
{
int pow;
// passing arguement as parameter
pow = Program.power(5);
Console.Write("\nPower = {0}", pow);
Console.ReadLine();
}
}
}
Output
namespace Understanding_Parameter
{
class Program
{
// function with parameter
public static int power(int num1)
{
int result;
result = num1 * num1;
return result;
}
static void Main(string[] args)
{
int pow;
// passing arguement as parameter
pow = Program.power(5);
Console.Write("\nPower = {0}", pow);
Console.ReadLine();
}
}
}
Output
Power = 25
While using parameterized function, must declare valid return data type with function as follow:
For Integer value:
public int power(int num1)
For String value:
public string print(string name)
You also return appropriate value with return keyword.
Comments
Post a Comment