Sunteți pe pagina 1din 34

DR ATIF SHAHZAD

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)

Method Declaration Calling the method Parameters vs


• Method Declaration (slide 11) • Calling the method: Example Arguments
• Method Declaration (slide 12) • From Where Method Can Be • Parameters in Methods (slide
Called? 26)

Local variables
Method Names: • Local variables (slide 17)
Recommendations
Dr. Atif Shahzad

Access Modifiers Body of the method

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)

A Quick Review Question


{
int I, X;
for (I = 1; I <= (9 % 2 + I); I++)
{
X = (I * 3 + I * 2) / I;
Console.WriteLine(X);
}
Console.ReadLine();
}

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

[static] <return_type> <method_name>([<param_list>])


Dr. Atif Shahzad

10
Method: Declaration

[static] <return_type> <method_name>([<param_list>])

Method body
Dr. Atif Shahzad

11
Method: Declaration

[static] <return_type> <method_name>([<param_list>])

Method body
Dr. Atif Shahzad

12
Method: Declaration

[static] <return_type> <method_name>([<param_list>])

Method body
Dr. Atif Shahzad

13
Method: Declaration

[static] <return_type> <method_name>([<param_list>])

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

[static] <return_type> <method_name>([<param_list>])

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

The method name should consist of verb, or


verb and noun. 16
Access Modifiers
public private static

• method can • it cannot be • no need to


be called by called from have an
any C# class, anywhere, instance of a
no matter except from class in
where it is. the class in which the
which it is static
declared. method is
declared
Dr. Atif Shahzad

17
Body of the method
static <return_type> <method_name>(<parameters_list>)
{
// … code goes here – in the method's body …
}

static void PrintLogo()


{ // Method's body starts here
Console.WriteLine(“Industrial Deptt");
Console.WriteLine(“KAU, Jeddah");
} // … And finishes here
Dr. Atif Shahzad

18
Local variables
Whenever we declare a variable inside the
body of a method, we call that variable
local variable for the method

static void Definevars()


{ int x = 3;
int y = 4;
}
Dr. Atif Shahzad

Method can NOT be declared inside the body of another method.

19
Local variables
Whenever we declare a variable inside the
body of a method, we call that variable
local variable for the method

static void Definevars()


{ int x = 3;
int x = 4;
}
A local variable named 'x' is already defined in this scope.
Dr. Atif Shahzad

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

From its own


body RECURSION
Dr. Atif Shahzad

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

static void PrintSign(int number)


{
if (number > 0)
{
Console.WriteLine("Positive");
}
else if (number < 0)
{
Console.WriteLine("Negative");
}
else
Dr. Atif Shahzad

{
Console.WriteLine("Zero");
}
} 25
Multiple Parameters in Methods

static void PrintMax(float number1, float number2)


{
float max = number1;

if (number2 > max)


{
max = number2;
}
Console.WriteLine("Maximal number: " + max);
Dr. Atif Shahzad

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.

float var1, var2;

static void PrintMax(float var1, var2)

static void PrintMax(float var1, float var2)


Dr. Atif Shahzad

27
Parameters vs Arguments

elements in the parameters list (var1 and var2) are


called parameters

static void PrintMax(float var1, float var2)

PrintMax(100.0f, -23.5f);

values by the method invocation (-23.5 and 100)


Dr. Atif Shahzad

arguments
are called
28
Parameters in Methods
Dr. Atif Shahzad

29
Return output

Static float PrintMax(float number1, float number2)


{
float max = number1;

if (number2 > max)


{
max = number2;
}
return max;
Dr. Atif Shahzad

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

S-ar putea să vă placă și