You are on page 1of 20

Objectives

In this session, you will learn to:
Introduction to procedures
Types of procedures

Using Methods
A method is a set of one or more program statements,
which can be executed by referring to the method name.
To use methods, you need to:
Define methods
Call methods

Defining Methods Defining a method means declaring the elements of its structure. Consider the syntax of defining a method: <Access specifier> <Return Type> <Method Name>(Parameter List) { Method Body } .

) The elements of the method declaration include the method name. and the method body.Defining Methods (Contd. The following are the elements of a method: Access specifier Return type Method name Parameter list Method body Let us understand each of the element of the method declaration. the parameters list. the return type. .

Consider the syntax of defining a method: This determines the extent to which a variable or method can be accessed <Access specifier> <Return Type> from another class.Defining Methods (Contd.) Defining a method means declaring the elements of its structure. <Method Name>(Parameter List) { Method Body } .

) Defining a method means declaring the elements of its structure.Defining Methods (Contd. If the method is not returning any value. { Method Body } . <Access specifier> <Return Type> use void as the <Method Name>(Parameter List) return type. Consider the syntax of defining a method: A method can return a value of any type.

) Defining a method means declaring the This is a unique identifier and is elements of its structure. .Defining Methods (Contd. Consider the syntax of defining a method: case-sensitive. The method name <Access specifier> <Return Type> cannot be the <Method Name>(Parameter List) same as the { variable name or Method Body any other } non-method item declared in the class.

. <Access specifier> <Return Type> The method name cannot be the <Method Name>(Parameter List) same as the { variable name or Method Body any other } non-method item declared in the class.) Defining a method means declaring the This is a unique identifier and is elements of its structure.Defining Methods (Contd. Consider the syntax of defining a method: case-sensitive.

) Defining a method means declaring the elements of its structure.Defining Methods (Contd. Consider the syntax of defining a method: <Access specifier> <Return Type> <Method Name>(Parameter List) { Method Body } This contains the set of instructions needed to complete the required activity. .

. You can call a method by using the name of the method. you can execute it by calling it. as shown in the following example: MethodName(). The method name is followed by parentheses even if the method call has no parameters.Calling Methods After defining the method.

Calling Methods (Contd. result = num1 + num2.) The following is an example of calling methods: using System. return result. class Calculator { public int AddNumber(int num1. int num2) { int result. } static void Main(string[] args) .

AddNumber(10. Console. } } .) { Calculator cal = new Calculator(). // The following statement is calling the // AddNumber method and passing 10 and // 20 as the parameter list.ReadLine(). int value=cal.WriteLine("The result is {0}".Calling Methods (Contd. 20). Console. value).

Using Methods with Parameters Methods can also be declared with parameters. The methods with parameters are called by passing the value using the following mechanism: Value Reference Output . Consider the example of declared a method with parameters: void DisplayResult (int result) { //….. they should be called with parameters. } When the methods are declared with parameters.

int num2) { //… } void Accept() { int val1=10.Using Methods with Parameters (Contd.val2). } . CalculateSum(val1. int val2=2.) Value: The parameters passed by value creates a separate copy in the memory. The following example shows the parameters passed by value: void CalculateSum( int num1.

} .) Reference: The parameters passed by reference does not creates a separate copy of the variable in the memory. int val2=2. A reference parameter stores the memory address of the data member passed. CalculateSum( ref val1. The following example shows the parameters passed by reference: void CalculateSum( ref int num1.ref int num2) { //… } void Accept() { int val1=10.ref val2).Using Methods with Parameters (Contd.

out recieveVal).Using Methods with Parameters (Contd. } void Accept() { int val1=10. out int result) { result=num1+num2.ref val2. The following example shows the parameters passed by reference: void CalculateSum( ref int num1.ref int num2. CalculateSum( ref val1. int val2=2. } .) Output: The output parameters are used to pass the value out of the method. int recieveVal.

.Demo: Swapping Two Numbers by Using Methods with Parameters Problem Statement: Write a program to swap two numbers by using reference type parameters in a method.

. The private access specifier allows a class to hide its member variables and member functions from other class functions and objects. you learned that: Abstraction is the process of reducing information content in order to retain only the relevant information for a particular purpose. The public access specifier allows a class to expose its member variables and member functions to other functions and objects. Encapsulation is the process of hiding all the details of an object that do not contribute to its essential characteristics.Summary In this session. An access specifier is used to determine whether any other class or function can access the member variables and functions of a particular class.

Parameters allow information to be passed into and out of a method. you can include a list of parameters in parentheses. The access modifiers that can be used with methods are public. . just like the private access specifier while implementing inheritance.) The protected access specifier allows a class to hide its member variables and member functions from other class objects and functions. protected internal. protected. and private. internal. Defining a method means declaring the elements of its structure.Summary (Contd. When you define a method. A method is a set of one or more program statements that can be executed by referring to the method name.

The return statement is used to return the control to the caller. A reference parameter is a reference to a memory location of a data member. except that they transfer data out of the method rather than into it.) Parameters can be passed by using any one of the following parameters types: Value Reference Output Pass by value is the default mechanism for passing parameters in C#.Summary (Contd. Output parameters are like reference parameters. .