Modular Programming
Modular Programming
Chapter 6:-Modular Programming functions, In Java, C# we call them methods while in Visual
6.1 Chapter objectives Basic we call them sub procedures/functions. Ideally, each of
By the end of this topic, learners should be able to: the module is supposed to perform a specific function hence
▪ Demonstrate understanding what modular programming avoiding code duplication, simplifying program maintenance
is and debugging as well as promoting program portability
▪ Demonstrate understanding on how to define and invoke encountered a programming approach where all the code
1
CMT 307:-.NET PROGRAMMING:-MODULAR PROGRAMMING
▪ Modules allow you to reusable the same code over and ▪ Access-modifier is used to set the access level/visibility
again in different programs with less/little code for classes, fields, methods and properties. C# supports
duplication four access specifiers as discussed below
6.4 Defining a method in C#
The general syntax form of a method definition in C#
Modifier Description
programming language is as follows:
Access-modifier return_data_type method_name( <parameter-
list> ) public The code is accessible for all classes
{
Declaration of local variables;
method-body private The code is only accessible within the same
class
return return_value;
2
CMT 307:-.NET PROGRAMMING:-MODULAR PROGRAMMING
operations without returning a value. In this case, the The other component of a method definition is the method body
return_type is the keyword void. which contains a collection of C# statements that define what
▪ Method_Name: This is a unique name of the method which the method does. Parameters if any can be used as local
should follow the rules of naming identifiers. variables within the method body. The programmer can also
▪ Parameter-list: A parameter is like a placeholder. When a declare additional local variables and constants within the
method is invoked, you pass a value to the parameter. This method body. The parameters and variables of a method are
value is referred to as actual parameter/argument. The only accessible within that method.
parameter list refers to the type, order, and number of the Example 1 of method definition
parameters of a method. Parameters are optional; that is, a Lets assume you wish to define a method which accepts marks
method may contain no parameters. of a student then computes the grade of the grade of the
The list of parameters is a declaration in the form student. The method shall return the grade of the student to the
Type_1 par_1,.....type_n par n…………….. calling block
3
CMT 307:-.NET PROGRAMMING:-MODULAR PROGRAMMING
Example 2: define a method that accepts travel time, travel <variable-name>=method-name (<argument list>);
date and destination then determine if there is a bus that ▪ Variable-name: this is optional is only
matches the three criteria. included if a method returns a value after
If a bus exists, we should return the status i.e. true or false with execution. The data type of the variable name
true indicating the bus is available and false indicating that should be the same as that of the method return
there is no bus that matches the three criteria data type
▪ Method-name:- This refers to the method we are
invoking/calling in our program. It should be
already defined within the program otherwise
the compiler will generate syntax error
▪ Argument-list: this are the actual values we
pass to the method during the point of calling
the method. The number of arguments are also
A method is supposed to be defined in a separate independent optional and this depends if the corresponding
block; i.e. that means we can’t define a method inside another method has parameters or not
event e.g. click event for a given object Once the method call is encountered in the program , execution
6.5 Method invocation/call branches to the body of the method definition . Copies of the
Method definition is completely nothing since by itself it does values of method arguments are stored in the memory locations
nothing unless instructed to execute. For a method to execute, allocated to the parameters. The statements of the method
it needs to be called/invoked otherwise the compiler will not be body are then executed up to the last return statement then
aware of its existence. The general syntax for calling a method control returns to the calling block. Any value returned by the
in C# is as follows method is stored by to the variable on the left of the assignment
4
CMT 307:-.NET PROGRAMMING:-MODULAR PROGRAMMING
operator where the method was invoked. Execution then corresponding grade depending on the marks of we pass to the
resumes immediately after the method call. method.
To call our second method, we can use the following statement
For example, to call our earlier method for grading, we can use
bool bstatus;
the following statement
string travelDate=TxtTravelDate.Text;
String grade=grading(Convert.ToInt32(Txtmarks.Text)
string travelTime=TxtTravelTime.Text;
In the above example, the marks are obtained from a text box
string destination=TxtDestination.Text;
known as Txtmarks then convert into integer before passed to
bstatus=findbus(travelDate,travelTime,destination)
the parameter in a method grading.
;
The above statement calls the grading( ) and passes a copy of 3.9 method arguments and parameters
the value stored in the textbox known as Txtmarks to the Arguments /actual parameters are values you pass to a method
parameter known as marks. parameter when you call the method. The calling code supplies
The body of the method is executed a value assigned to the the arguments in parentheses immediately following the
variable grade. The return statement passes this value back to method name.
the calling method. The memory allocated to the parameters On the other hand, a parameter/formal parameter represents a
and local variables is then destroyed to free memory. The value value that a method expects you to pass when you call it and
returned is assigned to the variable on the left-hand side, it’s defined during method declaration. The name of each
grade, in the expression used to call the method. parameter serves as a local variable within the method and can
The net effect of executing the method in our example is that be used the same way you use any other variable.
5
CMT 307:-.NET PROGRAMMING:-MODULAR PROGRAMMING
▪ Parameters and may bear different names. floor(x) rounds x down to nearest integer
▪ Method arguments can be constants and mathematical fabs(x) absolute value (unsigned)
expressions.
ceil(x) rounds x up to nearest integer
▪ Arguments can also be method that return a value
cos(x) trigonometric cosine of x (in radians)
though this concise style makes the code harder to read
and debug tan(x) trigonometric tangent of x (in radians)
3.10 Math library and system library built-in functions exp(x) exponential function
Methods come in two categories ; they can be defined by the log(x) natural logarithm of x (base e)
user (1) or built in as part of the compiler package (2). As we
log10(x) logarithm of x to base 10
have seen, user-defined method has to be defined by the user
3.11 Chapter summary
himself to perform a specific task. Built-in methods, however, 3.12 Chapter exercise
are already defined hence the user needs just to invoke them 3.13 Further reading suggestions
without writing the method logic. The programmer doesn’t need
https://docs.microsoft.com/en-us/dotnet/csharp/methods