Lesson 7.3

C# Functions or Methods with Programming Example


Objective

What is parameter?
How to use it in a method?
Programming Example

In the previous few chapters you have learned how to use classes and methods in a program. In this chapter you will learn how to use parameter in programming language.

What is parameter in c#?

A parameter is a way to pass information to a method. If a method is defined with argument then it is necessary to pass parameter while calling that method. Based on parameter a method processes a task and return value.

How to define parameter?

You can define parameter as follow:

public int AddNumber(int x, int y)
{
Return x+y;
}

Or 

public void DisplayName(string str)
{
Console.writeline(str);
}



Programming Example:

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

namespace parameter_csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int result;
            //Passing parameter to a method
            result = AddTwoNumber(12, 17);
            Console.WriteLine("Addition of 12 and 17 is " + result);
            Console.ReadKey();
        }
        
        //Creating parameterized method
        public static int AddTwoNumber(int num1, int num2)
        {
            return num1 + num2;
        }
    }
}


Output

Addition of 12 and 17 is 29
__

The above example contains an integer type method AddTwoNumber(int num1, int num2) which requires 2 integer value as a parameter then it returns some of both value using return keyword.

How many types of parameter in c#?

There are two types of parameter available in C# - Value Type Parameter and Reference Type Parameter

Value type parameter

A value type parameter directly passes the value to a method. A method creates special spaces in memory for handling that value. If meanwhile the original value changed, it doesn't reflect in the parameterized method.

Value Type Parameter

Reference Type Parameter

Reference Type Parameter passes memory address of value as a parameter instead of direct value. So, if the original value changed it reflects into parameterized method too. A reference type parameter can be defined by ref keyword.

Reference Type Parameter

Programming Example

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

namespace val_ref_type
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 5;

            ValAddNumbers(num1);
            Console.WriteLine("Value Type Output " + num1);

            RefAddNumbers(ref num1);
            Console.WriteLine("Reference Type Output " + num1);

            Console.ReadKey();

        }

        //Value type parameter
        public static void ValAddNumbers(int num1)
        {
            num1 = num1 + 7;
        }
        //Reference Type Parameter
        public static void RefAddNumbers(ref int num1)
        {
            num1 = num1 + 7;
        }
    }
}


		   

Output

Value Type Output 5
Reference Type Output 12
__

Explanation

In the preceding example we have an integer variable int num1 which holds value 5. There are two methods ValAddNumbers(int num1) and RefAddNumbers(ref int num1) which is value type and reference type respectively. Both methods do same process as it add 7 to existing value but when you run the program you will get different output.

  1. The ValAddNumbers(int num1) method does not reflect output to previous address. So the final result is unchanged and the value of num1 doesn't changed in main method.
  2. The RefAddNumbers(ref int num1) method directly reflects the output to variable address. So, it doesn’t matter where you have used ref type parameter, the calculated value directly added to variable.

Output Parameter

Several times you may want more than one return value from the method. The return keyword only returns a single value at one time. To overcome this issue you can use output keyword for getting multiple outputs from a single method.

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

namespace output_parameter
{
    class Program
    {
        static void Main(string[] args)
        {
            int add, sub, multiply;
            Program.calculator(12, 15, out add, out sub, out multiply);

            Console.WriteLine("12 + 15 = " + add);
            Console.WriteLine("12 - 15 = " + sub);
            Console.WriteLine("12 * 15 = " + multiply);

            Console.ReadKey();

        }

        public static void calculator(int num1, int num2, out int add, out int sub, out int multiply)
        {
            add = num1 + num2;
            sub = num1 - num2;
            multiply = num1 * num2;
        }
    }
}

		   

Output

12 + 15 = 27
12 - 15 = -3
12 * 15 = 180
__

What is command line argument?

Did you notice that main method always comes with String[] args parameter. What is this? This is a command line argument. You can also pass parameter to a main method. Just see this programming example

Step 1: Open Notepad and Paste following command in it and save it with cmdline.cs. You must select All Files (*.*) in file type while saving notepad.

cmdlines
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace command
{
    class Program
    {
        static void Main(string[] args)
        {
           
            Console.WriteLine(args[0]);
            Console.WriteLine(args[1]);
            Console.ReadKey();
        }
    }
}

		   
		   

Step 2: Now Open Visual Studio Command Prompt. Go to Start > All Program > Microsoft Visual Studio > Developer Command Prompt

Step 3: Go to location where your file is saved. I have saved my file in D drive so I have just typed D: and hit Enter. Now compile your program using following command.

csc cmdline.cs

Step 4: Run the program using following command. I am also passing two parameters while executing my program as follows:

cmdline Jasmine Nicole

Output

C:\Program Files (x86)\Microsoft Visual Studio 11.0>D:

D:\>csc cmdline.cs
Microsoft (R) Visual C# Compiler version 4.6.0079.0 for C# 5 Copyright (C) Microsoft Corporation. All rights reserved.
This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

D:\>cmdline Jasmine Nicole
Jasmine
Nicole
__

Summary

In this chapter you have learned all about the passing parameter to a method. You have also learned about value type parameter, reference type parameter, command line argument and output parameter. Next chapter explains Array manipulation in c#.

BackNext