CSAlpha Assign3: Functions

Notice: Assignments are currently being rewritten for CSAlpha. This assignment was created for an old version of the curriculum and may no longer align with the current curriculum.

If you have any questions regarding the assignment or see any errors in the assignment writeup, please let me know.

Create a text file called assign3.txt. You will be using this to respond to questions in the assignment writeup and paste your code.

Understanding Functions

To begin this assignment, you must have a basic understanding of functions and their uses. The function below, called "square", takes an integer as a parameter and returns the value of the integer squared:

Let's go over each individual part of the function for review:

Calling a function

In the main method, I've called the square function above as an example:

To call a function, write the function's name and follow with the parameters you want to pass in inside parentheses. When you call a function with a return value, imagine that the return value or "output" of the function is basically replacing the place where you called the function. This is why we are able to set result to the function call; the function returns a value so the function call becomes the return value after it is finished running.

There are cases where functions don't need to return any value or even take in any parameters. Look at the two examples below:

The first function, printString, takes in a String argument and prints it. Since the function does not need to return a value, it has the keyword void for its return datatype.

The second function, printHello, prints the string "hello". Since the function has no need to return a datatype or take in any parameters, it has the keyword void for its return datatype and empty parentheses after the function name.

With all that knowledge under your belt, you are ready to start the assignment!

Core Assignment

For each of the following functions, make sure to test the examples given by calling the functions in the main method. For functions that do not print to the console, check your results manually by printing the function result in the main method after calling them.

Once you are finished with each function declaration, please paste the function into your text file named assign3.txt.

Assignment Extension

If you can complete the assignment extension, you are well on your way to mastering functions.

Go back to assignment 2; think about how you can use functions to clean up your code. What functions could you declare that would reduce the repetition in your code and also make it easier to add new events (events are the console output after the user makes a choice)?

(Hint: Is there a way to take user input and process it in a separate function? Doing this would require you to restrict user input to a few values you can work with. How can you make the user's input consistent without having to be restricted to only asking yes or no questions?)

You do not have to submit the extension via text file. We will go over it in lesson if you have completed it.

Additional Practice

Declare functions of anything that you think would be useful! Please paste these additional functions into your text file.