You are on page 1of 24

METHODS

How to code methods


• Methods are blocks of code that are designed to do one specific job.

• To start, you code an access modifier that indicates whether the


method can be called from other classes.

• In most cases, you'll use the private access modifier so the method can
only be called from within the class where it's coded.

• If you need to call the method from another class, however, you can
use the public access modifier.
Continue…
• After the access modifier, you code the return type for the method,
which identifies the type of data that the method returns.

• If the method doesn't return any data, you code the void keyword as
the return type. Then, you don't code a return statement.

• After the return type, you code the name of the method. In most cases,
you '11 give the 1nethod a name that indicates the action it performs.
Continue…
• After the method name, you code a set of parentheses.

• Within the parentheses, you declare the parameters that are required by
the method.
• This is known as the parameter list.

• Later, when you call the method, you pass arguments that correspond
to these parameters.
Continue…
Continue…
Continue…
Continue…
• To allow other classes to access a method, use the public access
modifier.
• To prevent other classes fro1n accessing a method, use the private
modifier.
• To code a method that doesn't return data, use the void keyword for the
return type.
• To code a method that returns data, code a return type in the method
declaration and code a return statement in the body of the method.
Continue…
• The return statement ends the execution of the current 1nethod and
returns the specified value to the calling method.

• Within the parentheses of a method, you can code an optional


parameter list that contains one or more parameters, with the
parameters separated by commas.

• When you code a parameter, you must code a data type and you must
provide a name for the parameter.
How to call methods
• To start, you can type the optional this keyword to specify that the
method that's being called is in the current class, followed by the dot
operator and the method name.

• Otherwise, you just code the method name and the current class is
assumed.

• If the method requires arguments, you code the argument list within
parentheses right after the method name.
Continue…
• Otherwise, you code an empty set of parentheses.

• The term parameter refers to the variables of a method declaration, and
the term arguments refers to the values that are passed to a method.

• When you call a method, you typically code the arguments in the same
sequence that's used by the para1neter list in the method.

• This is called passing arguments by position.


Continue…
• When you pass arguments, the data types of those arguments must be
compatible with the data types of the corresponding parameters in the
method.

• In fact, it's a good coding practice to pass arguments that have the
same data types as the parameters in the method.
Continue…
Continue…
Continue…
• When you call a method, you code an argument list with the
arguments that are passed to the method. You can pass arguments by
position or by name.

• When you pass arguments by position, the arguments must be in the


same order as the parameters in the parameter list defined by the
method.

• The arguments you pass to a method must have data types that are
compatible with their corresponding parameters.
How to use optional parameters
• When you code a method, you 1nay occasionally want to provide
default values for one or more parameters in case the user doesn't
provide values for them.

• To do that, you can use a feature of C# called optional parameters.

• To declare an optional parameter, you assign a default value to the


parameter.
Continue…
• Then, if a value isn't passed to this para1neter, the default value is
used.
• When you use optional parameters, you should realize that they must
be coded after all required parameters.
How to use named arguments
• To pass arguments by name, you code the name of the parameter
followed by a colon and the name of the argument.

• This is illustrated in the third and fourth statements in the example on


the next slide that call the Future Value method.

• Both of these statements pass arguments to the first and third


parameters, but omit the argument for the second parameter.
Continue…
• You can also pass some arguments by position and some by name as
illustrated in the last statement.

• Here, the first argument passes a value to the first parameter by


position, and the second argu1nent passes a value to the third
parameter by name.
Continue…
Continue…
Continue…
• The parameter list for a method can include one or more optional
parameters.
• Then, you don't have to supply a value for those parameters when you
call the method.
• An optional parameter must be assigned a constant value as its default.
• Then, if a value isn't supplied for that para1neter, the default value is
used.
• If you define a para1neter as optional, every parameter after that
parameter in the parameter list must be defined as optional.
Continue…
• If you pass a value to an optional parameter by position, you must pass
a value to every other parameter before that parameter.

• When you use optional parameters, you may also want to pass
arguments by name.

• To do that, you code the parameter name followed by a colon followed


by the argument name.
Continue…
• When you pass arguments by name, you can code the arguments in
any sequence, and you can omit any of the optional parameters.

• You can also combine the two techniques for passing arguments and
pass some by position and others by name.

You might also like