60 likes | 261 Views
Methods. It’s how stuff gets done. What. Methods are blocks of code that can be called from anywhere in the program They take in a set of variables or nothing at all They can return one variable or nothing at all. Where. The methods are placed inside of the class decleration. Why.
E N D
Methods It’s how stuff gets done
What • Methods are blocks of code that can be called from anywhere in the program • They take in a set of variables or nothing at all • They can return one variable or nothing at all
Where • The methods are placed inside of the class decleration
Why • Re-Use: Methods provide a convenient way to re-use code. If there is a specific process that occurs repeatedly then it can be replaced at each location with a single method call • Management: Methods also provide a centralized location to define a process so it can be easily changed everywhere by changing what is inside the method. • Encapsulation: When a method has been programed by someone else, you can also use it without worrying about how it works.
How • The structure for a method is as follows: public/private returnType name(input variables){ code } • public/private – whether the method is public or private • returnType – what type of variable or Object the method returns • name – the name of the variable • input variables – the variables or Objects that the method takes in • code – the code that is executed
Other stuff • When primitive variables are passed into a method they are passed by copy while objects are passed by reference. • So if there is a primitive variable x passed into the method then the value will not be changed outside of the method.