Sie sind auf Seite 1von 16

Objectives

In this session, you will learn to:


Introduction to procedures
Types of procedures
Calling procedures
Argument passing mechanism

Introduction to procedures
A procedure is a set of one or more program statements ,
which can be executed by referring to the method name.
Procedures are useful for performing repetitive tasks.
They allow you to break an application into discrete logical
units, which make the application more readable.
Dividing a complex application into methods makes code
more flexible and easy to maintain and to debug.

Introduction to procedures
To use procedures, you need to define them.
(contd..)

Defining a procedure/method means declaring the elements


of its structure.
The syntax of defining a method is :
<access specifier> <return type> <method name>(parameter list)
{
//Method body
}

Arithmetic Assignment Operators

Introduction to procedures
The elements of the method declaration include the method
(contd..)
name, the parameters list, the return type, and the method
body.
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.

Arithmetic Assignment Operators

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

extent to which a
variable or method
can be accessed
from another class.

Arithmetic Assignment Operators

Introduction to procedures
Defining a method means declaring
(contd..)
A method can return
the elements of its structure.
Consider the syntax of defining a
method:
<Access specifier> <Return Type>
<Method Name>(Parameter List)
{
Method Body
}

a value of any type.


If the method is not
returning any value,
use void as the
return type.

Arithmetic Assignment Operators

Introduction to procedures
Defining a method means declaring
(contd..)
This is a unique
the elements of its structure.
Consider the syntax of defining a
method:
<Access specifier> <Return Type>
<Method Name>(Parameter List)
{
Method Body
}

identifier and is
case-sensitive.
The method name
cannot be the
same as the
variable name or
any other
non-method item
declared in the
class.

Arithmetic Assignment Operators

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

This is used to pass


and receive the data
from a method.

Arithmetic Assignment Operators

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

set of instructions
needed to complete
the required activity.

Unary Operators

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

Comparison Operators

Introduction to procedures
Consider the following code snippet of method definition :
(contd..)
public int AddNumber(int num1, int num2)
{
int result;
result = num1 + num2;
return result;
}

Comparison Operators (Contd.)

Argument passing mechanism


Methods can also be declared with parameters. Consider
the example of declared a method with parameters:
void DisplayResult (int result)
{
//..
}

When the methods are declared with parameters, they


should be called with parameters. The methods with
parameters are called by passing the value using the
following mechanism:
Value
Reference
Output

Logical Operators

Argument passing mechanism


Value: The parameters passed by value creates a separate
(contd..)
copy in the memory. The following example shows the
parameters passed by value:
void CalculateSum( int num1, int num2)
{
//
}
void Accept()
{
int val1=10;
int val2=2;
CalculateSum(val1,val2);
}

Logical Operators (Contd.)

Argument passing mechanism


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

Using Conditional Constructs

Argument passing mechanism


(contd..)
Output: The output parameters are used to pass the value
out of the method. The following example shows the
parameters passed by reference:

void CalculateSum( ref int num1,ref int num2, out


int result)
{
result=num1+num2;
}
void Accept()
{
int val1=10;
int val2=2;
int recieveVal;
CalculateSum( ref val1,ref val2,out
recieveVal);
}

Summary (Contd.)

Summary

Introduction to procedures
Declaring the procedures
Invoking procedures
Argument passing mechanism

Das könnte Ihnen auch gefallen