Writing Methods in Programming

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What is the primary action performed when a method is called?

  • The program executes the method's code block from bottom to top.
  • The method's code block is ignored and the program continues.
  • The program executes the method's code block from top to bottom. (correct)
  • The program skips over the method's code block.

Why are methods considered convenient in programming?

  • They reduce the amount of memory the program uses.
  • They automatically optimize code execution speed.
  • They allow code to be reused without rewriting it. (correct)
  • They make code run only once, preventing infinite loops.

In the context of a method, what does the term 'signature' refer to?

  • The code block within the method.
  • The method's name and parameter list. (correct)
  • The access modifiers of the method.
  • The return type of the method.

What is the primary purpose of the return keyword in a method?

<p>To indicate that the method has completed its task and to return a value. (B)</p> Signup and view all the answers

What happens if a method's return statement is located inside an if statement and the condition is false?

<p>If the method has a non-void return type, there must be another return statement to ensure a value is always returned. (D)</p> Signup and view all the answers

What is the key characteristic of a method with a void return type?

<p>It does not return any value. (D)</p> Signup and view all the answers

What is typically included in a method's header?

<p>The method's access modifiers, return type, name, and parameters. (A)</p> Signup and view all the answers

In what order does the code within a method's block execute?

<p>According to the order specified by the programmer, typically from top to bottom. (B)</p> Signup and view all the answers

Where should methods be written in relation to other methods and classes?

<p>Outside other methods but inside a class. (A)</p> Signup and view all the answers

What is a key benefit of using methods for debugging?

<p>You can isolate and identify where the source code may be located in a program. (C)</p> Signup and view all the answers

What does the term 'parameter' refer to in the context of methods?

<p>A variable that stores a value passed into the method. (D)</p> Signup and view all the answers

In the public static boolean isPositive(int num) method, what does boolean signify?

<p>The return type of the method. (D)</p> Signup and view all the answers

What is meant by 'unreachable code' in a method?

<p>Code that cannot be executed because it comes after a return statement. (D)</p> Signup and view all the answers

How do you call a method in your program?

<p>By using the method's name followed by parentheses. (A)</p> Signup and view all the answers

In the context of methods, what is the significance of 'camelCase'?

<p>It is a convention for naming methods where the first word is lowercase and subsequent words start with uppercase letters. (A)</p> Signup and view all the answers

Which part of the following method declaration is the method header? public static int calculateSum(int a, int b)

<p><code>public static int calculateSum(int a, int b)</code> (B)</p> Signup and view all the answers

How does using methods contribute to less code repetition?

<p>Methods let you reuse code blocks with different parameters, avoiding the need to rewrite the code. (D)</p> Signup and view all the answers

Which components are considered part of a method's signature?

<p>Method name and parameter list. (C)</p> Signup and view all the answers

Consider a method declared as public static void displayMessage(String message). What can you infer about this method?

<p>It does not return any value. (A)</p> Signup and view all the answers

What happens when a method returns a value?

<p>The returned value is available at the location where the method was called. (C)</p> Signup and view all the answers

If a method has a non-void return type, what must the method body always ensure?

<p>That a value of the specified type is always returned. (C)</p> Signup and view all the answers

Why must isPositive() from the resource's content, return something?

<p>The method was specified as a type of boolean. (A)</p> Signup and view all the answers

Why might we want to store a method into a variable, instead of calling it by itself?

<p>We need to store the retuned value. (C)</p> Signup and view all the answers

What happens if a method declares a return after an if statement?

<p>The code may sometimes becomes unreachable. (D)</p> Signup and view all the answers

How do we identify if something is as a method?

<p>If it comes after parenthesis. (A)</p> Signup and view all the answers

What does declaring the method say about it?

<p>It can be returned and reused. (A)</p> Signup and view all the answers

What is a named block of code?

<p>A method. (D)</p> Signup and view all the answers

If given a scenario of the same output, it is better to use a method instead of just running the code regularly. WhY?

<p>Improves readability. (B)</p> Signup and view all the answers

Why is debugging considered to be easier when writing methods?

<p>Methods can be isolated when debugging. (B)</p> Signup and view all the answers

Why is there are benefit towards less code repitition?

<p>Means it saves time from rewritting code. (C)</p> Signup and view all the answers

If given the method public static void printWord(String w); What can be said about it?

<p>It returns nothing. (D)</p> Signup and view all the answers

If given the method public static boolean isDivisible(double d, int i); what can we assume must happen?

<p>Either return true or false at the end. (C)</p> Signup and view all the answers

Which of the following isn't true about writing good methods?

<p>The number of parameters do not matter. (B)</p> Signup and view all the answers

What of the following would we call is the parameter in public static int getAge(int birthYear, String name);

<p><code>birthYear, name</code> (A)</p> Signup and view all the answers

Flashcards

What is a method?

A named block of code that performs a specific task.

Why use methods?

Methods are convenient because they can be called over and over again without re-writing the code.

Access Modifiers (public, static)

Keywords defining access level. Public means accessible from anywhere, static is associated with the class itself.

Return Type

Specifies the data type the method will return.

Signup and view all the flashcards

Method Name

Descriptive name for the method following variable naming conventions.

Signup and view all the flashcards

Method Parameters

Values passed into the method when it is called.

Signup and view all the flashcards

Method Body

The code that runs when the method is called.

Signup and view all the flashcards

Return Keyword

Tells a method that its job is done and returns a value.

Signup and view all the flashcards

Void Return Type

A method with no return value.

Signup and view all the flashcards

Parameter Matching

Ensures correct data types and number of parameters

Signup and view all the flashcards

Where do I write methods?

Methods must be written outside of other methods, but inside a class.

Signup and view all the flashcards

Benefits of writing methods

Simplified debugging, less code repetition, and easier to read.

Signup and view all the flashcards

Study Notes

  • Writing methods is about creating named blocks of code

What is a Method?

  • A method is a named block of code that is executed when is called by its name.
  • The code runs top to bottom
  • The code runs until it returns a value or reaches the end of the code block.
  • Methods are like miniature programs, they take in data as parameters, perform a function, and return a value.
  • Methods contains the code that runs when called by name, and executes all the code within its code block.
  • A code block consists of everything between the method's curly brackets.
  • Methods can contain other code blocks, such as if statements and "for" loops.
  • All code inside curly brackets directly under the method declaration forms the method.

Why Use Methods?

  • Methods are convenient as they can be called repeatedly without rewriting the code.
  • Methods prevent the need to rewrite "for" loops for similar calculations each time.

Basic Syntax

  • public static void main(String[] args) is the basic syntax:
    • public and static are access modifiers.
    • void specifies the return type.
    • main is the method name
    • String[] args represents the method's parameters.
  • Running a program in a block of code is running a method.
  • Right-clicking a class and running void main(String[] args) executes that method which contains all of the code.
  • public void main(String[] args) is a special method in Java.
  • When a method is called, the main program execution jumps to that method, completes its task, and then returns to the main program.

Parts of Method: Header

  • public static boolean isPositive(int num) is the method's header.
  • The header declares a method and includes the return type, name, and parameters.
  • The method's signature consists of the name and parameter list

Parts of Method: Access Modifier

  • public and static are access modifiers

Parts of Method: Return Type

  • boolean is the return type
  • Once finished, the method return a boolean value to where it was called.
  • Return type indicates the type of value or an answer gets sent back.

Parts of Method: Name/Identifier

  • isPositive is the name of the method
  • Methods should follow variable naming conventions, such as being descriptive and using camelCase, starting with a lowercase letter.
  • Methods are called using parentheses, differentiating them from variables

Parts of Method: Parameter List

  • int num is the method's parameter list
  • Methods can have multiple parameters or none.
  • Parameters are placeholders for values passed in by the caller.
  • The parameter list specifies what information a method needs from the main program to execute.

Parts of Method: Body

  • The method's body is the block of code between curly brackets, which runs when the method is called.
  • Methods must return a value of the specified type before the closing curly bracket (unless the return type is void).

The Return Keyword

  • The return keyword indicates that a method's job is done and it is immediately returning a value.
  • Methods must return the data type specified in the method header.
  • Any code after a return is unreachable because the method has already returned to where it as called.

isPositive(int num) Method

  • If the num parameter is greater than 0, the isPositive(int num) method needs to return a value of true.
  • If the condition num > 0 isn't met, the method won't return anything without fixing this.
  • To ensure the isPositive(int num) method always returns a value, an else statement can be used to return false when num is not greater than 0.
  • Simplified versions of the isPositive() method achieve the same result. return num > 0;
  • The isPositive() method can be used in a main program and called when needed.

More on the Return Keyword

  • When the return keyword is encountered, the method stops executing and returns a value.
  • Code after a return statement is unreachable.
  • If a return statement is inside an if statement or a loop, there is a chance the statement won’t run. There must be a return statement in an else statement.
  • When a method returns a value, its job is done.
  • Code that cannot be executed is called unreachable code

The Void Return Type

  • A void return type means a method does not return a value.
  • Methods with a void return type still run its code block, but do not return a value.
  • Void methods can still return, but will not return a value.
  • Void return type methods are the only methods where a return isn't explicitly required.
  • If a method returns a `boolean, a boolean must be returned.
  • When a method returns void, it could return, but the compiler won’t complain if it returns at the end.

Calling Methods/Parameter Matching

  • avgOf2()method call needs to use the method name.
  • The avgOf2 method requires 2 parameters, so data needs to be sent over for it to complete its job.
  • The avgOf2 method is designed to return a double, so the returned value must be stored to continue

Where to Write Methods

  • Methods must be written outside of other methods, but inside a class.

Benefits of Writing Methods

  • Methods allows for easier debugging since, any issues can be located quickly.
  • Methods result in less code repetition, and called with new parameters.
  • Methods are easier to read.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser