Function Parameter in C#

In this chapter you will learn:
  • What is parameter?
  • How to define it in method?
  • How to pass parameter to method in C# programming?

The parameter is the essential part of the function. However, it is optional but it makes the C# function more dynamic than simple function. There are two type of function parameter in C#, Value type parameter and reference type parameter, that is discussed in a lateral chapter in this section.

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.

 

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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 can also return appropriate value with return keyword.

Summary

In this chapter you learned the basic of parameter in C#. You also learned a simple program of parameter. In next chapter you will learn Value type parameter in C#.

 

Share your thought