CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Java · 6 min · free

User-defined Methods in Java

6 min5 KwickClipsFull text belowFree
Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Why methods exist, how to define and call one, signature rules, actual and formal parameters, pass by value, and pure versus impure methods.

Follows the syllabus of: ICSE Class 9 Computer Applications, ICSE Class 10 Computer Applications, ISC Class 12 Computer Science (868)

On screen in this lesson

Why methods?

A method is a named block of code that does one task
Reuse: write once, call it many times
Modularity: split a big program into small parts
Easier to test, read and correct

Parts of a method

PartIn our exampleMeaning
Modifierstatichow it is used
Return typeinttype sent back
Nameareawhat we call it
Parameter listint length, ...inputs it needs
Body{ ... }the steps

Signature and return type

Signature = method name + parameter list
Signature of area: area(int, int)
Return type is NOT part of the signature
void means the method returns nothing
return sends a value back and ends the method

Passing arguments

Arguments are the values written in the call
Number of arguments must match the parameters
Types must match or widen, like int to double
Order matters: first argument goes to first parameter

Actual and formal parameters

NameWhere it appearsExample
Actual parameterin the callbill(3, 15)
Formal parameterin the headerqty, price

Value vs reference

Primitives (int, double, char): a copy of the value
Arrays and objects: a copy of the reference
Changes to the contents are seen by the caller
Textbooks call the second case call by reference

Quick answers

Is the return type part of a method signature?

No. The signature is only the name and the parameter types.

Why did marks stay 50 after add(marks)?

An int is passed as a copy, so changing the parameter does not touch marks.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. Suppose you must calculate a canteen bill fifty times in one program. Will you type the same lines fifty times? No. Today we will learn methods, which let you write the steps once and use them again and again.

Let us start with a new term. A method is a named block of code that does one job. Reuse means you write the method once and call it whenever you need it. Modularity means breaking a big program into small parts, like chapters in a book. Small parts are easier to test, read and correct, because each one has a single job.

Here is a method that finds the area of a rectangle. The word static lets us call it without making an object, and we will study it in a later lesson. Int before the name is the return type, the kind of value the method sends back. Area is the method name. Inside the round brackets are the parameters, length and width. The return statement sends the answer back to the caller.

Let us name every part of the first line, called the method header. The modifier, here static, tells Java how the method can be used. The return type, here int, is the type of value sent back. The name is area, and we use it to call the method. The parameter list says what inputs the method needs. The body, inside curly brackets, holds the steps.

Exams often ask about the method signature. The signature is the method name together with the types of its parameters. So the signature of our method is area, int, int. The return type is not part of the signature, so remember this point. If a method sends nothing back, its return type is void. The return statement sends a value back, and the method stops right there.

A method does nothing until it is called. Here, bill takes qty, the quantity, and price. Every Java program starts running from main. Inside main, we call bill with three and fifteen, for three samosas at fifteen rupees each. The method returns forty five, and print line shows it on the screen.

The values we send in a call are called arguments. Three rules control them. First, the number of arguments must match the number of parameters. Second, each type must match, or be safely widened, like an int going into a double. Third, the order matters, because the first argument goes to the first parameter, and so on.

Two more exam terms. Actual parameters are the real values written in the method call, like three and fifteen. Formal parameters are the variables in the method header, like qty and price, which receive those values.

When we pass a primitive value, like an int, Java sends a copy. This is called pass by value, or call by value. Here marks is fifty, and add receives a copy in num. The method changes num to sixty, but marks is untouched. So main prints fifty.

Pause and predict. This time marks is an array holding fifty, and the method adds ten to its first box. What prints? It prints sixty. An array is an object, and the variable marks only holds its address, called a reference. The method gets a copy of that address, so it reaches the same array.

Let us sum up the two cases. For primitives like int, double and char, the method gets a copy of the value, so the caller's variable never changes. For arrays and objects, the method gets a copy of the reference. So changes made to the contents are seen by the caller. Many textbooks call this second case call by reference, and strictly, Java always passes a copy.

Methods are also grouped as pure or impure. The state of an object means the values stored in it. A pure method never changes any state, while an impure method does. A pure method always gives the same output for the same input. So a pure method is also called an accessor, and an impure one a mutator. For example, halving a number is pure, but adding money to a balance is impure.

Here bal is the bank balance, one hundred rupees. Half is pure, because it only returns half of its input. Dep, short for deposit, is impure, because it changes bal. Half of one hundred returns fifty, and deposit adds fifty to the balance. So the program prints one hundred fifty.

Let us revise what we learned today. Methods give us reuse and modularity. A signature is the name plus the parameter types, and the return type is not part of it. Actual parameters are in the call, and formal parameters are in the header. Primitives are passed as a copy, but an array's contents can be changed by the method. A pure method changes nothing, while an impure method changes state.

Courses that teach this

CourseUnit
ICSE Class 9 Computer ApplicationsWorking with Methods
ICSE Class 10 Computer ApplicationsUser-Defined Methods
ISC Class 12 Computer Science (868)Functions (Methods)
Programming All levels JavaClasses, Objects and Methods

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →