-
Notifications
You must be signed in to change notification settings - Fork 2
Functions
Jack Schumacher edited this page Jun 19, 2026
·
1 revision
- Open and run Functions.java.
- What do you notice about the code?
- What do you notice about the result?
- Break down what you see.
This tells Java that we want a *function* in our class
| This tells Java we won't return anything
| | This is what we name our function
| | | These parentheses include paramaters. (none)
v v v v
static void printHi(){
// This is the code we are running
System.out.println("Hello!");
}
- This breakdown uses unknown language like class, and paramaters, which we will learn later.
- Edit the code in the function.
- Add some other println to the function. Remember, the code should be put in after the first println, but before the ending curly bracet (
}). - Run the code, and see what happens.
- Functions make code easier to write and read.
- Think back on the MathProblems.java you wrote. How could you improve this with functions?
- You will need to learn one more thing about functions before you can rewrite MathProblems with functions.
- Open Parameters.java, and run it.
- What is different about the calls (the function name with parentheses) in main?
- Change some of the numbers in the parentheses of the calls.
- Look through the
square(int num)function. What do you think it does?
- Change the square function into a multiply function.
- While this may seem redundant (Why not just use
*?), it is easy for practice. - Add another parameter into the square function, by adding a comma, and then another variable declaration.
Click for solution.
static void square(int num, int num2)- Rename the function by right-clicking on "square" and in the menu, clicking on "Rename Symbol".
- Update all of the function calls to use this new paramater with a comma.
Click for solution.
multiply(3, 6); multiply(2, 9);- Pass in some variables to
multiply().
- Create some
intvariables in main. - Pass in the variables to the
multiply()function, by just saying their name instead of a number. Example:multiply(myNum,myNum2);
- Try to pass in a decimal number (double or float) to multiply.
multiply(2.3,6.1);- The red squigglies on
multiplyread:The method multiply(int, int) in the type ParametersSolution is not applicable for the arguments (double, double).This is basically telling you that multiply MUST take in 2 ints, and cannot take in 2 doubles.
- Duplicate (copy & paste) multiply, and modify it to take in 2 doubles, and name it
multiplyDecimals.
- Try to change the
multiply(2.3,6.1);into amultiplyDecimals(2.3,6.1);.
- Problem with functions currently:
- What if you didn't want to just print out the result, but pass it into another function, or do something else rather than printing?
- Open Return.java, and look through the code.
- Run the code. What is it doing?
- The
returnkeyword is put at the end of the function running. It is when the function is done running, and it gives a result. - You can use the return value like a variable. This means, you can call the function wherever you would use a variable, even storing the return in a variable!
- Remove the variables, replacing them with function calls in the code.
- Call
square(5)andsquare(2)in place of squareFive and squareTwo in the println statement. - Try adding more
square()calls in code, like another print statement.
- Assign the result from a
square()to a float.
- Before running, what do you think will happen?
- Try fixing the function.
- Remember - the paramater used in the template is a float.
Click for solution.
static float square(float num){ //code }Turns into...
static int square(int num){ //code }