0% found this document useful (0 votes)
6 views

Modular Programming

Uploaded by

e.mutindamalusi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Modular Programming

Uploaded by

e.mutindamalusi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CMT 307:-.

NET 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

▪ Explain the benefits associated with modular


programming In the previous sections, we have

▪ Demonstrate understanding on how to define and invoke encountered a programming approach where all the code

methods in C# (statements) has been written inside a single block e.g. a


certain event handler block. In the next sections we will learn
▪ Understand the difference between an argument and
how to program in a modular way by writing additional user
parameters.
defined functions.
6.2 Introduction 6.3 Benefits associated with modular programming
Modular programming is an art of software engineering ▪ Less code duplication since each module will do a
whereby programmers/software engineers break large and specific task.
complex programs into manageable portions known as ▪ It’s also easy to maintain the program since the
modules. Each of the module will perform a dedicated/specific programmer is working with small program
task and it’s upon the programmer to decide when to break the segments/module.
program into these small manageable modules.
▪ Simplifies debugging since each program logic can be
verified separately
Different programming languages refer to these modules using
different terms. In C and C++, we call this small module

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;

} protected The code is accessible within the same class,


or in a class that is inherited from that class.
A method definition in C# consists of two components; header
and method body.
internal The code is only accessible within its own
The method header has the following syntax and never has a
assembly, but not from another assembly.
semicolon at the end.
return_data_type method_name( <parameter list> ) ▪ Return_data_Type: this is the type of the value the method
returns after execution. it’s supposed to be a valid C# data
type. There are some methods that perform the desired

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

They represent external values needed by the method. The


list of parameters can be empty. If present, each parameter
should be preceded by the appropriate data type.
▪ Return return_value: this line is optional and its included
only if the method returns a value and its placed at the end
of method. Return_value denotes the value the method
shall return after execution. It should be of the same type
with method return_data_type

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.

the variable grade has been assigned the value of the

5
CMT 307:-.NET PROGRAMMING:-MODULAR PROGRAMMING

Point to note: to worry about the implementation details of these methods.


▪ Each argument corresponds to the parameter in the Some of them perform mathematical method while others
same position in the list. perform daily routine operations as shown below .

▪ The number of arguments must be the same as the


Function Description
number of parameters.
sqrt(x) square root
▪ Parameters and arguments must be of the same data
type pow(x,y) x raised to power y

▪ 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

You might also like