Quiz.pdf
Document Details
Uploaded by OutstandingHeliotrope936
Tags
Full Transcript
Introducing Methods A method is a collection of statements that are grouped together to perform an operation. 1 KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز Introducing Methods, cont...
Introducing Methods A method is a collection of statements that are grouped together to perform an operation. 1 KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز Introducing Methods, cont. Method signature is the combination of the method name and the parameter list. The variables defined in the method header are known as formal parameters. When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. 2 KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز Introducing Methods, cont. A method may return a value. The returnValueType is the data type of the value the method returns. If the method does not return a value, the returnValueType is the keyword void. For example, the returnValueType in the main method is void. 3 KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز CAUTION A return statement is required for a nonvoid method. The following method is logically correct, but it has a compilation error, because the Java compiler thinks it possible that this method does not return any value. public static int sign(int n) { if (n > 0) return 1; else if (n == 0) return 0; else if (n < 0) return –1; } To fix this problem, delete if (n