0% found this document useful (0 votes)
27 views4 pages

Tugas Introduction To Programming Brayden Assignment 11 PDF

The document discusses the benefits of using methods in programming, including code reuse and improved readability. It explains how to define and invoke methods, the importance of return statements in value-returning methods, and the definitions of parameters, arguments, and method signatures. Additionally, it addresses common errors related to method declarations and variable redeclarations in Java.

Uploaded by

braydentan35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

Tugas Introduction To Programming Brayden Assignment 11 PDF

The document discusses the benefits of using methods in programming, including code reuse and improved readability. It explains how to define and invoke methods, the importance of return statements in value-returning methods, and the definitions of parameters, arguments, and method signatures. Additionally, it addresses common errors related to method declarations and variable redeclarations in Java.

Uploaded by

braydentan35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Tugas Introduction to Programming Assignment 11

Nama : Brayden Hernett Tan

NIM : 2702247973

1. What are the benefits of using a method? How do you define a method? How do you invoke
a method?
a. Benefit
- To reduce redundant code
- To enable code reuse
- To improve the quality of program
- Modularize code
- To let the code readable
- To narrows the scope of debugging
b. How to define a method

To call a method you just need to type the name of method followed by () which you
can fill parameters if it required to.

c. How to invoke a method

2. What would be wrong with not writing a return statement in a value-returning method? Can
you have a return statement in a void method? Does the return statement in void method
cause syntax errors?
- You will get an error if you don’t write a return statement in value returning method
In a void method (a method that does not return any value), you can still use a return
statement without a value. This is often used to exit the method prematurely or
indicate that the method has completed its execution.

Or you can use “return true;” , “return a;” if it just “return; “only it will error
3. Define the terms parameters, argument, and method signature
- Parameter = Parameters are variables used in a method or function to receive values
that are passed to it when the method is called.
- Arguments = Arguments are the actual values that are passed to a method or
function when it is called
- Arguments = Arguments are the actual values that are passed to a method or
- function when it is called
4.

Eror =
- Add return statement after public static
- Set variable m in the parameter of method1
- Put integer in method1
- The method2 can’t accept the argument of 4 to put in parameter list, because
method2 only have 1 parameter which is n. so add int m into it
5. In this program there are 2 methos name “ method” that has a same type of parameter, in
java the metode must be divide by each type and by order of the parameter. In this case,
both method “method” has the same parameter ‘int’ this is not allowed in java. So the
solution is we change the name of one of the method or change the type or the amount of
parameter for making the method becomes unique. Example :

6. The code is error because there is a variable ‘n’ declaration occurs twice. First is in ‘n’ is
declared as a parameter of ‘nPrintin’ method and the second ‘n’ is redeclared inside that
method block. This makes the program error.

So, the solution is not to declare the same variable twice and just replace the value, and if
we still wanted to use the ‘n’ variable without changing the the parameter, just remove the
int n declaration from the parameter and just write ‘int n =1;’ inside the method
7.

You might also like