Defining & Designing your Functions - Your Code Structure and Foundations - Java Programming Box Set: Programming, Master's Handbook & Artificial Intelligence Made Easy; Code, Data Science, Automation, problem solving, Data Structures & Algorithms, 1st Edition (2015)

Java Programming Box Set: Programming, Master's Handbook & Artificial Intelligence Made Easy; Code, Data Science, Automation, problem solving, Data Structures & Algorithms, 1st Edition (2015)

PART I: Your Code Structure and Foundations

Chapter 5a. Defining & Designing your Functions

Now we’ll move on to the parts of programming where the magic happens.

Functions.

Where data structures are used to represent “things” in this universe, you define functions as the “actions” or verbs in this universe.

And you can make your functions do whatever you want/need it to do, as long as you know what you’re doing. You can calculate math, write sentences for you, change or update data, sort out lists with tens of thousands of items, make websites for you, whatever you like. In reality, the possibilities can be endless.

But first let’s understand the core parts of function design: its inputs, its output, its signature, its effects, and its functionality.

A Function’s Inputs

You can set your function to accept whatever data you need it to.

These are called a function’s arguments or parameters. Your function will use this incoming data to perform what you intend it to.

Or, on the other hand, you can also have a function NOT require any inputs. Your function will then perform what you’ve programmed it to, but it won’t need any incoming data.

For practice, let’s use comments to declare what inputs we want our function to have. Let’s say we want a name (a string) as an input

// INPUT: - a name (String)

A Function’s Output

Your function can also return a data value - based on whatever you want to set it to. You can then program your function to output that same data type.

Or, you can also have a function NOT return anything. You can then program your function to do what you intend it to do, but it won’t return any data after it executes.

In most programming languages, functions only return ONE thing - whether it be a data value, an entire list, compound data, or more.

However, you must make sure your function outputs whatever you have set it to. Say, if you want your function to output a String, the very last line of that function MUST return a String data type. If you set your function to have no outputs, your function MUST NOT return any data types after it executes.

For practice, let’s use comments to declare what outputs we want our function to have. We’ll continue designing our function. We now have an input, now we want to have an output.

// INPUT: - a name (String)

// OUTPUT: - an ID (Number)

Defining what your Function will Do

Now we figure out what EFFECT our function will have once we run it.

This is your function’s main purpose - it’s the reason why you’re going to write these lines of code!

Your function’s effect will be whatever you intend it to do. Change data, create new data, calculate a few values together, whatever you want.

But isn’t an Output and Effect the same thing? Well, no.

There is a difference between a function’s OUTPUT and EFFECT. A function’s output is the data it returns, while a function’s effect is anything that the function does or anything the function’s action has affected.

For practice, let’s use comments to declare what effects we want our function to have. We’ll continue designing our function. We now have an input and output. Now we figure out what it does when we run it.

Let’s say we want it to come up with a random number. We first put in the comments of what we intend it to do

// INPUT: - a name (String)

// OUTPUT: - an ID (Number)

// EFFECT: generates a random number for a given name

Key Function Rule-of-Thumb:

Make sure your function only does the only one thing you want it to do. A function that does too many things will not only complicate your code and make it look bad, but it will cause headaches and frustration for programming teammates.

However, your function can include and call on many other functions to help process something. These are called Helper Functions (we’ll cover this later!)

A Function’s Signature

Here is when we start writing our function’s lines of code.

In most programming languages, a function’s signature defines a function’s name, inputs, outputs, and even particular traits it has.

Remember when we declared our Composite data? We first started out designing the name of our whole data structure, then we started designing what it consisted of.

For function signatures, we first code what its name is - as well as any inputs and outputs it has.

Now, let’s look back at the function we were designing for practice. We now know what it does, what it requires and what it returns.

For now, we’ll use Pseudocode to design our function’s Signature. Our signatures will then be structured in this form:

(OutputType) functionName(InputType inputName)

So our function’s signature will look like this:

// INPUT: - a name (String)

// OUTPUT: - an ID (Number)

// EFFECT: generates a random number for a given name

number createID(string name)

Implementing your Function

This can be the tricky part - unless you know exactly what you’re doing.

In designing functions, the last thing you do is to program your function’s actual functionality. You would now know what inputs & outputs it has, as well as what it’s trying to do. You’ve essentially planned what your function will do.

Now you’ll have your function do what you planned it to.

You program the functionality in the next few lines after your function’s signature.

In pseudocode, we’ll use curly brackets ( { } ) right after the function’s signature to include its functionality code. Our functions will then be structured in this form:

(OutputType) functionName(InputType inputName) {

(your function’s code)

return OutputType if any

}

Finally, let’s look back at the function we were designing for practice. We are ready to finish it.

Let’s say there’s such thing as a function named randomNumber() that creates a random number for us.

// INPUT: - a name (String)

// OUTPUT: - an ID (Number)

// EFFECT: generates a random number for a given name

number createID(string name) {

randomNumber()

}

A Common Error in Function Design

But wait. The above code isn’t going to work. Can you guess why?

Oh right.

In our signature, our function is supposed to RETURN a number. So in order for this function to work, it needs to actually return a number.

So we make the function RETURN whatever random number is generated by the inside function randomNumber()

// INPUT: - a name (String)

// OUTPUT: - an ID (Number)

// EFFECT: generates a random number for a given name

number createID(string name) {

return randomNumber()

}

and now, we finally complete our function - from design to code.

Calling your Function Procedure

To have your code execute whatever effect or procedure you’ve defined in your functions, you simply put your function name in your code - but in a particular way

Keep in mind that you can only place your function wherever its output data type is expected to be. The exception is when your function has no data output (returns void). You can place this function on lines by itself.

For example, take these two functions:

// INPUT: - none

// OUTPUT: - an ID (Number)

// EFFECT: ?????

number procedureA() {…}

// INPUT: - none

// OUTPUT: - none

// EFFECT: ?????

void procedureB() {…}

Procedure A outputs a number.

Place the function wherever the data type Number is expected:

Number x = procedureA();

Number y = 10.213 * procedureA();

Procedure A outputs nothing. Place the function on its own line of code.

procedureB();

If a function belongs to a data class, make sure to access that function from an instance of that data class:

ClassC instanceC = new ClassC;

instanceC.procedureC();