Documente Academic
Documente Profesional
Documente Cultură
IE-322
Computer Applications
LECTURE #07 in Industrial Engg.-I
Computer Applications in
Industrial Engg.-I
IE322
Dr. Atif Shahzad
8/12/2018
…Recap
What we will see…
Method Parameters in Methods
• Method (slide 8) • Multiple Parameters in
• Method: Example Declare and Use Methods
• Multiple Parameters in
Methods (slide 24)
Local variables
Method Names: • Local variables (slide 17)
Recommendations
Dr. Atif Shahzad
8/12/2018
A Quick Review Question
static void Main(string[] args)
{
int I, X;
for (I = 1; I <= (9 % 2 + I); I++)
{
X = (I * 3 + I * 2) / I;
Console.WriteLine(X);
}
Dr. Atif Shahzad
Console.ReadLine();
} 5
static void Main(string[] args)
I (9 % 2 + I) Condition X
1 2 TRUE 5
2 3 TRUE 5
3 4 TRUE 5
4 5 TRUE 5
5 6 TRUE 5
6 7 TRUE 5
7 8 TRUE 5
8 9 TRUE 5
. . . .
Dr. Atif Shahzad
. . . .
6
Method
a basic part of a program.
It can solve a certain problem
take parameters and
return a result.
Dr. Atif Shahzad
7
Method
In the C# language, a method can be
declared only between the opening
{
and the closing
}
brackets of a class.
Dr. Atif Shahzad
8
Method: Example
Dr. Atif Shahzad
9
Method: Declaration
10
Method: Declaration
Method body
Dr. Atif Shahzad
11
Method: Declaration
Method body
Dr. Atif Shahzad
12
Method: Declaration
Method body
Dr. Atif Shahzad
13
Method: Declaration
Method body
When a method is declared keep the sequence of its elements
Dr. Atif Shahzad
description: first is the type of the value that the method returns,
then is the method’s name, and at the end is a list of parameters
placed in round brackets. 14
Method: Declaration method signature
Method body
When a method is declared keep the sequence of its elements
Dr. Atif Shahzad
description: first is the type of the value that the method returns,
then is the method’s name, and at the end is a list of parameters
placed in round brackets. 15
Method Names: Recommendations
Method name must describe the method’s
purpose.
The name of a method should start
with capital letter.
The PascalCase rule should be applied, i.e.
each new word, that concatenates so to form
the method name, must start with capital
letter.
Dr. Atif Shahzad
17
Body of the method
static <return_type> <method_name>(<parameters_list>)
{
// … code goes here – in the method's body …
}
18
Local variables
Whenever we declare a variable inside the
body of a method, we call that variable
local variable for the method
19
Local variables
Whenever we declare a variable inside the
body of a method, we call that variable
local variable for the method
20
Calling the method
<method_name>();
Dr. Atif Shahzad
21
Calling the method: Example
PrintLogo();
Dr. Atif Shahzad
22
From Where Method Can Be Called?
From the main
program method
– Main()
From some
other method
23
Declare and Use
We are allowed to invoke (call) a method
before it is declared in code
REMEMBER that, this is NOT the case with variables.
Dr. Atif Shahzad
24
Parameters in Methods
{
Console.WriteLine("Zero");
}
} 25
Multiple Parameters in Methods
26
Multiple Parameters in Methods
When a method with multiple parameters is declared, we
must note that even if the parameters are of the same type,
usage of short way of variable declaration is NOT allowed.
27
Parameters vs Arguments
PrintMax(100.0f, -23.5f);
arguments
are called
28
Parameters in Methods
Dr. Atif Shahzad
29
Return output
30
Question 1
Develop a program with four methods
(Add,Subtract,Multiply,Divide) with two
inputs and one output.
Call them from main method and display
the results.
Dr. Atif Shahzad
31
Question 2
Develop a program with functionality of
Teller machine.
Implement Deposit,WithDraw and
CheckBalance methods and call them from
Main method
Dr. Atif Shahzad
32
NEVER hesitate to
contact should you
have any question
Dr. Atif Shahzad
Dr. Atif Shahzad
8/12/2018 34