METHODS
Subroutines in Computer
Programming
What is Method?
●A method is a kind of building block that solves a
small problem.
●Methods allow programmers to construct large
programs from simple pieces.
●Methods are also known is functions,
procedures, subroutines.
Why to Use Methods?
● More manageable programming.
• Split large problems into small pieces
• Better organization of the program
• Improve code readability
• Improve code Understandability
● Avoiding repeating code.
• Improve code maintainability
● Code reusability.
• Using existing methods several times
DECLARING AND
CREATING
METHODS
Declaring and Creating
Methods
● Each method has a name
• It is used to call the methods
• Describes its purpose
Declaring and Creating
Methods(2)
● Methods declared static can be called by any other method (static or not).
● The keyboard void means that the method does not return any result.
Declaring and Creating
Methods(3)
● Each method has a body
• It contains the programming code
• Surrounded by { and }
Declaring and Creating
Methods(4)
● Methods are always declared inside a class
● Main( ) is also a method like all others
Calling Methods
Calling Methods
● To call a method, simply use:
1. The method’s name
2. Parentheses (don’t forget them!)
3. A semicolon(;)
PrintLogo();
● This will execute the code in the method’s body and will result in printing
the following:
Telerik corp.
www.Telerik.com
Calling Methods(2)
● A method can be called from:
• The Main( ) method
Static void Main( )
{
// …
PrintLogo();
// …
}
• Any other method
• Itself (process known as recursion)
METHODS WITH
PARAMETERS
Passing Parameter and Returning
Values
METHOD PARAMETER
● To pass information to a method, you can use parameters (also known as
arguments)
• You can pass zero or several input values
• You can pass values of different types
• Each parameter has name and type
• Parameters are assigned to particular values when the method is called
● Parameters can change the method behavior depending on the passed
values
DEFINING AND USING METHO
PARAMETERS
Static void PrintSign(int number)
{
if (number > 0)
Console.WriteLine(“Possitive”);
else if ( number > 0)
Console.WriteLine(“Negative”);
else
Console.WriteLine(“Zero”);
};
}
● Method’s behavior depends on its parameters
● Parameter can be of any type
• Int , double, string, etc.
• arrays(int[], double[],etc.)
DEFINING AND USING METHOD
●
PARAMETERS(2)
Methods can have as many parameters as needed:
• The following syntax is not valid:
Static void PrintMax(float number 1, number2)
CALLING METHODS WITH
PARAMETERS
● To call a method and pass values to its parameters:
• Use the method’s name, followed by a list of expressions for each parameter.
● Examples:
PrintSign(-5)
PrintSign(balance) ;
PrintMax(100, 200) ;
PrintMax(oldquality * 1.5, quantity * 2) ;
CALLING METHODS WITH
PARAMETERS(2)
● Expressions must be of the same type as method’s parameters (or
compatible).
• If the method requires a float expression, you can pass int instead.
● Use the same order like in method declaration.
● For methods with no parameters do not forget the parentheses.
USING METHODS
WITH PARAMETERS
METHODS PARAMETERS -
EXAMPLE
MONTHS
MONTHS - EXAMPLE
● Display the period between two months in a user-friendly way.
MONTHS – EXAMPLE(2)
PRINTING TRIANGLE
PRINTING TRIANGLE - EXAMPLE
● Creating a program for printing triangle as shown below:
PRINTING TRIANGLE - EXAMPLE
OPTIONAL
PARAMETERS
OPTIONAL PARAMETERS
● C# 4.0 supports optional parameters with default values:
● The above method can be called in several ways:
RETURNING VALUES
FROM METHODS
RETURNING VALUES FROM
METHODS
● A method can return a value to its caller
● Return value:
• Can be assign to a variable:
• Can be used in expressions:
• Can be passed to another method:
DEFINING METHODS THAT
● Instead of void,RETURN A toVALUE
specify the type of data return.
● Methods can return any type of data (int, string, array, etc.).
● Void methods do not return anything.
● The combination of method’s name, parameters and return value is called
method signature.
● Use return keyword to return a result.
THE RETURN STATEMENT
● The return statement:
• Immediately terminates method’s execution.
• Returns specified expression to the caller.
• Example:
return -1;
● To terminate void method, use just:
return;
● Return can be used several times in a method body.
TEMPERATURE
CONVERSION
TEMPERATURE CONVERSION -
EXAMPLE
● Convert temperature from Fahrenheit to Celsius:
POSITIVE NUMBERS
POSITIVE NUMBERS
● Check if all numbers in a sequence are positive:
DATA VALIDATION
DATA VALIDATION - EXAMPLE
● Validating input data:
DATA VALIDATION - EXAMPLE
METHODS – BEST PRACTICE
● Each method should perform a single, well-defined task.
● Method’s name should describe that task in a clear and non-ambiguous
way.
• Good examples: CalculatePrice, ReadName
• Bad examples: f, g1, Process
• In C# methods should start with capital letter
● Avoid methods longer than one screen.
• Split them to several shorter methods
SUMMARY
● Break large programs into simple methods that solve small sub-problems.
● Methods consist of declaration and body.
● Methods are invoked by their name.
● Methods can accept parameters.
• Parameters take actual values when calling a method.
● Methods can return a value or nothing.