Sie sind auf Seite 1von 2

Management Information Systems Dept.

Advanced Programming – MIS 301

Semester: Fall 2017


Lecturer: Dr. Amira Sayed
TA: Eng. Mohamed Mahmoud

Tutorial1; Functions & Methods:


Definition:
A set of code which is referred to by name and can be called (invoked) at any point in a program
simply by utilizing the method's name. Think of a methodas a subprogram that acts on data and
often returns a value. Each method has its own name (code reusing).
Syntax

public static void methodName(int a, int b) {


// body
}

 Public static – modifier (Optional)

 The word public- means that the method itself can be called from
anywhere which includes other classes, even from different packages
(files) as long as you import the class
 The second keyword, static -means that the method belongs to the
class and not any instance of the class ( object ).
 However, if the keyword static was not there, then the method can be
invoked only through an object. int − return type

 Void-return type.

 The word void -means that the method doesn't return anything (it
does not return anything when you run the method).

1
Management Information Systems Dept.
Advanced Programming – MIS 301

 If you do want a method to return something, then simply replace the


word void with a data type (primitive or reference type) of the object
(or primitive type) that you wish to return.

 methodName − name of the method

 a, b − formal parameters

 int a, int b − list of parameters

Example
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;

return min;
}

Method Calling
For using a method, it should be called. There are two ways in which a method
is called i.e., method returns a value or returning nothing (no return value).

Das könnte Ihnen auch gefallen