4. What we will see…
9/27/2018
Dr.AtifShahzad
Method
• Method (slide 8)
• Method: Example
Method Declaration
• Method Declaration (slide 11)
• Method Declaration (slide 12)
Method Names:
Recommendations
Access Modifiers Body of the method
Local variables
• Local variables (slide 17)
Calling the method
• Calling the method: Example
• FromWhere Method Can Be
Called?
Declare and Use
Parameters in Methods
• Multiple Parameters in
Methods
• Multiple Parameters in
Methods (slide 24)
Parameters vs
Arguments
• Parameters in Methods (slide
26)
5. 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);
}
Console.ReadLine();
}
Dr.AtifShahzad
5
6. 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);
}
Console.ReadLine();
}
Dr.AtifShahzad
6
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
. . . .
. . . .
7. Method
a basic part of a program.
It can solve a certain problem
take parameters and
return a result.
Dr.AtifShahzad
7
8. Method
In the C# language, a method can be
declared only between the opening
{
and the closing
}
brackets of a class.
Dr.AtifShahzad
8
14. Method: Declaration
Dr.AtifShahzad
14
[static] <return_type> <method_name>([<param_list>])
When a method is declared keep the sequence of its elements
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.
Methodbody
15. Method: Declaration
Dr.AtifShahzad
15
[static] <return_type> <method_name>([<param_list>])
When a method is declared keep the sequence of its elements
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.
method signature
Methodbody
16. Method Names: Recommendations
Dr.AtifShahzad
16
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.
The method name should consist of verb, or
verb and noun.
17. Access Modifiers
Dr.AtifShahzad
17
public
• method can
be called by
any C# class,
no matter
where it is.
private
• it cannot be
called from
anywhere,
except from
the class in
which it is
declared.
static
• no need to
have an
instance of a
class in
which the
static
method is
declared
18. Body of the method
Dr.AtifShahzad
18
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
19. Local variables
Dr.AtifShahzad
19
static void Definevars()
{ int x = 3;
int y = 4;
}
Method can NOT be declared inside the body of another method.
Whenever we declare a variable inside the
body of a method, we call that variable
local variable for the method
20. Local variables
Dr.AtifShahzad
20
static void Definevars()
{ int x = 3;
int x = 4;
}
Whenever we declare a variable inside the
body of a method, we call that variable
local variable for the method
A local variable named 'x' is already defined in this scope.
23. FromWhere Method Can Be Called?
Dr.AtifShahzad
23
From the main
program method
– Main()
From some
other method
From its own
body RECURSION
24. Declare and Use
Dr.AtifShahzad
24
We are allowed to invoke (call) a method
before it is declared in code
REMEMBER that, this is NOT the case with variables.
26. Multiple Parameters in Methods
Dr.AtifShahzad
26
static void PrintMax(float number1, float number2)
{
float max = number1;
if (number2 > max)
{
max = number2;
}
Console.WriteLine("Maximal number: " + max);
}
27. Multiple Parameters in Methods
Dr.AtifShahzad
27
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)
28. Parameters vs Arguments
Dr.AtifShahzad
28
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)
are called arguments
31. Question 1
Dr.AtifShahzad
31
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.
32. Question 2
Dr.AtifShahzad
32
Develop a program with functionality of
Teller machine.
Implement Deposit,WithDraw and
CheckBalance methods and call them from
Main method