IT101 – Computer Programming 1 (Fundamentals of Programming 1)
Module 5 (Week 10-12, October 7-24, 2019)
First Semester, AY 2019-2020
Function Definition
User-defined function
Parameter Passing
To solve a certain task, especially if it is a complex one, we apply the method that ancient
Romans did “Divide and Conquer”. According to this principle, the problem we solve must be
divided into small sub-problems. Taken separately they are well defined and easy to be resolved
compared to the original problem. At the end by finding solutions for all the small problems we
solve the complex one.
Using the same analogy, whenever we write a program we aim to solve particular task. To
do it in an efficient and “easy-to-make” way we use the same mentioned above principle “divide
and conquer”. We separate the given task into smaller tasks, then develop solutions for them
and put them together into one program called subroutines. In some other programming
languages, subroutines can be named as routines, procedures or functions but in general these
are called method.
I. Function Definition and Concepts:
A method is a basic part of a program. It can solve a certain problem, eventually take
parameters and return a result.
Method consists of the program’s logic. This is where the real job is done.
Method gives opportunity by using a simple block to build bigger programs.
Why to use Methods?
o Better structured program and more readable code – whenever a program has
been created, it is always a good practice to use methods, in a way to make your
code better structured and easy to read.
Only 20% of the effort is spent on creating and testing the program. The
rest is for maintenance and addition new features to the initial version made
by same or different developers. Readability is very important
o Avoid duplicated code – minimize the number of lines of code.
o Code reuse – if a piece of code is used more than once, it is a good idea to separate
it in a method for code reuse. This avoid repeated codes with same functionality.
Repeating impedes the maintenance of the program and may leads to
errors.
If a defect is found into a piece of 50 lines code, that is copied to 10 different
places over the program, to fix the defect, the repeated code must be fixed
for the 10 places. However, due to lack of concentration or some other
reasons, the developer fixes only some of the pieces of code, but not all of
them.
II. User-defined Function
Below is the syntax to declare a method in java:
[modifier] <return_type> <method_name> ([parameter_list]){
…
[method_body]
…
}
where those inside [] are optional and those inside <> are required.
Page 1 of 4
This figure shows the parts of a method
Parts
o Modifiers – these are keywords that gives additional information of the method.
Common modifier is access modifiers. Access modifier define access type in the
method. In java, there are four type of access specifiers.
public
protected
private
default
o Return type – the data type of the value returned by the method or void if does
not return a value.
o Method name – the identifier of the method. This is used to call (invoke) the
method. In naming methods, it is recommended to use verb or verb plus noun.
o Parameter list – comma separated list of the input parameters are defined,
preceded with their data type, within the enclosed parenthesis. If there is no
parameters, you must use empty parenthesis. Any data type can be used as
parameters and in multiple parameters, different data types can also be used.
o Method body – enclosed between braces. The code you need to be executed to
perform your intended operations
Method Signature/Specification – consists of the method name and parameter list
(number of parameters, type of the parameters and order of parameters). Return type
and exceptions are not considered as part of signature.
Local Variables – these are variables declared inside the body of the method.
o Variable Scope – the area of visibility of the variable.
Invoking a method – a.k.a calling a method is the process of execution of the method’s
code.
o Completes all the statements in the method.
o Reaches a return statement
o Throws an exception
Page 2 of 4
Formal Parameters – these are the parameters in the parameters list.
Argument – the values assigned to parameter when method is called/invoked.
public int max(int x, int y){…}
*x and y are called parameters
max(10,20);
*10 and 20 are called formal arguments
Return type – Method can be classified as function or routine. Function returns a value
based on the data type used. Routine or procedure does not return a value and uses void
keyword. Void means that the method do not give value.
o Function requires return keyword and the return value should be under the
datatype used in creating the method.
o Routine does not require return keyword
public static String printHi(){return "HI";}
*method the returns a String value which is “HI”.
public static void printHi(){
System.out.println("HI");
}
*method that does not return value but prints “HI”.
III. Parameter Passing
Method can be created without or with parameters.
Routine without parameter:
public static void printHi(){
System.out.println("HI");
}
Routine with parameter:
public static void printHi(String str){
System.out.println(str);
}
Page 3 of 4
Routine with multiple parameters:
public static void printHi(String n, int a){
System.out.println("I am"+n+","+a+"y/o.");
}
Function without parameter:
public static String printHi(){
return "Hi";
}
Fuction with parameter:
public static String printHi(String n){
return n;
}
Function with multiple parameter:
public static String printHi(String n, int a){
return "I am"+n+", "+a" y/o.";
}
Two types of parameter passing:
o Call by value – if a modification of the argument has no effect to the actual
argument.
Primitive data type
o Call by reference – if a modification of the argument can change the value of the
actual argument
Non primitive data type
Practice Time!!!
Further research is encourage about the concepts of method in java to strengthen your
understanding.
Creating sample methods in actual programs is highly suggested
Review and research on the previous topics.
Apply previous programming problems discussed to methods.
Try to understand and define and real essence of using method in programming.
Research on how to incorporate method in java.
Research on how to call method in java.
Create simple method in java based the above discussions.
//end of module 5
Page 4 of 4