Guided Notes

Methods

Defining methods, parameters and return values, overloading, scope, and recursion.

Defining methods

Slide 1 / 5

A method is a named block of code you can call again and again:

static int square(int x) { return x * x; }

β€’ static β€” belongs to the class (no object needed); objects come later. β€’ int β€” the return type. Use void when nothing is returned. β€’ square β€” the name, in camelCase. β€’ (int x) β€” the parameters.

Call it: int result = square(5); // 25

← Arrays