Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
66 views

Tutorial1 : Functions & Methods

This document provides an overview of functions and methods in programming. It defines a method as reusable block of code that can be called by name, optionally takes parameters, and may return a value. The syntax for defining a method is explained, including usage of public, static, and void keywords. An example method is provided to find the minimum of two integer parameters and return the result. Methods are called to either use the return value or execute the code without returning a value.

Uploaded by

Mrmr Gawad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Tutorial1 : Functions & Methods

This document provides an overview of functions and methods in programming. It defines a method as reusable block of code that can be called by name, optionally takes parameters, and may return a value. The syntax for defining a method is explained, including usage of public, static, and void keywords. An example method is provided to find the minimum of two integer parameters and return the result. Methods are called to either use the return value or execute the code without returning a value.

Uploaded by

Mrmr Gawad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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).

You might also like