California State University, Sacramento College of Engineering and Computer Science and Snarky Professors

Computer Science 10517: Super Mega Crazy Accelerated Intro to Programming Logic

Spring 2016 – Activity I – Functions

Functions are a key part to any . They differ from modules (as the textbook calls them) in that they can return a value. You have used this quite a bit during your time as a student. You should recall the Sin(x) and Cos(x) functions from trigonometry, the int(x) function from basic math, and sqrt(x) for those times you can't draw that funky square root line.

Programming languages often let you create your own. And, in this lab, that is exactly what you are going to do!

Part 1 – Your First Function

In many programming languages the term "module" and "function" are one and the same. The only difference is that a function returns something while a module does not.

NOTE: Redundant Terminology

Programming languages often use different terms to describe the same concept.

In the Family (which includes Java), modules and functions are synonymous. Pascal calls a module a "procedure". Visual Basic calls it a "sub" (for ).

Whatever you call it…. programming languages let you create your own functions. They are defined pretty much the same way you create a module. To get started with functions, let's create a function that adds two numbers together and returns the result.

1. Start by selecting "Add a Function" from the Program Menu or clicking on the add icon on the main toolbar.

2. A new window will open. In the Function Name field, enter Sum.

3. Add two parameters to your function. Call them num1 and num2. Make both of them Integer. They will represent the two numbers the caller will pass in.

4. For the "Return Type", select "Integer". The "Return Variable" box is now enabled.

5. Enter the variable name result. This will be a variable that you have to declare inside your function. Don't use the same name as the function. Flowgorithm will throw a temper tantrum if you do. 2

NOTE: Returning Values in Flowgorithm

Many programming languages can return the result of an expression. This can be a single variable (which is a very simple expression) or something far more complex.

Flowgorithm just lets you return the contents of a variable.

6. Click OK

7. You should see a blank flowchart now with the word “sum” in the top bubble. Make it look like flowchart below. Notice that we declare an integer called result. This is the variable we will return to the caller.

8. Go back to your main function (click on the dropdown box on the toolbar)

9. Add an output shape.

10. In this output shape, type Sum(2, 2).

11. Execute your flowchart

This makes it far more versatile than our earlier module which always just printed the sum. Excellent! Your flowchart uses a function to add two numbers and return the sum. However, this really isn't that useful. It's a useful start, but not still useful.

So, let's work on something a bit more impressive! 3

Part 2 – Composition

One of the great benefits of functions is the ability to use the output of one function as the input to another. In great languages, like Java, you can create sophisticated mathematical expressions that make use of operators such as: +, -, *, /

In reality, these are functions, but they're in a nice easy-to-read format. So, for example, anytime you use the result of a multiplication, and add it to another number, you are using the output of one function as input into another.

Let's try that out using your excellent sum function.

1. Double-click on output shape in your main module.

2. Change the expression to: Sum(2, Sum(2, 2))

Look at it really closely. Look at the first Sum in the expression. The first argument is the value 2. That's easy to understand. However, look at the second argument. That argument is the result of calling Sum with 2 and 2. What's the result?

3. Execute your flowchart. Did it output what you expected?

Very cool! Let's see if we can get different values.

4. Add a new output shape

5. Double-click on it.

6. Figure out an expression that will print 10 to the screen. …but ONLY uses combinations of 2 and calls to Sum.

7. Execute your flowchart. If you didn't get 10, double-check your expression.

Let's save your wonderful expression for later (we don't want to lose all that hard work).

8. Create another function – call it CompositionExample.

9. Go back to your main function

10. Cut your two output shapes. You should now have nothing in your main module.

11. Paste the output shapes into the function you just created. 4

Part 3 – Geometry

Let's take a look at a sphere. If you forgot, the volume of a sphere can be computed with the following mathematical expression:

4 Diameter Volume = π × 3 3 ( 2 )

This equation uses Diameter/2 as a substitution for Radius.

1. Add a new function called sphereVolume

2. Add a parameter for the diameter. Make it a real.

3. Calculate the volume of a sphere, and return that value. Recall that Flowgorithm has π built-in as a constant called pi.

Ah, now that you have written such a nice function, it is time to check if it works correctly. Look at the following pseudocode (written in the book's format).

Example Pseudocode

Module main Declare Real d

Input d

Display sphereVolume(d) End Module

This program declares a real variable called d, inputs it, and then displays the result of the function. This is a great technique for testing if you got the expression correct.

4. You main function should be blank. Implement the pseudocode above.

5. Execute it your flowchart and test it with a few values.

6. Check your results with the table below

Diameter Volume

1 0.523598833333332

2 4.18879066666666

10 523.598833333332

15 1767.1460625

20 4188.79066666666

Did everything match up? If so…. great work! It's time to move on. Otherwise, go back to your sphere function and double-check the expression. 5 Part 4 – Flowcharts and the Chocolate Factory

You have a really cool job at a candy factory. The factory can make custom candies for anyone in the World.

You work in the division that creates chocolate-covered caramels. The factory equipment can create chocolate-caramels of any size – and does so regularly. They can be as small as a pea or as big as a house. The company’s main problem is determining how much chocolate and caramel will be needed for a shipment.

Fortunately, you can write a program that will solve this dilemma! Let's look at the type of candy you are creating: chocolate-covered caramels. Basically, these are spheres.

However, there is a catch …

Both the caramel center and the candy itself are spheres, but what exactly is the volume of the chocolate shell? It's definitely not the volume of a sphere with the full diameter. The center doesn't take up delicious chocolate… it is delicious caramel!

You'll need to figure out how to compute the volume of the chocolate shell. Let's create a new function that will get us this value!

1. Add a new function

2. Give it a name – it's up to you.

3. For parameters, you want to pass in the total diameter of the larger sphere and the diameter of the internal sphere.

Wait a sec! Didn't you already create a function for a sphere? Use that in your new function!

4. Using your existing sphereVolume function (you might call it twice), compute and return the volume of the shell.

Now that you finished that. It's time to test it and make sure the values look correct! Testing is an important part of being a great computer programmer.

5. Modify your main program so you can type in 2 values – the outside diameter and the inside diameter. The pseudocode is below. I called my function sphereShell; replace this with whatever your function name is.

Example Pseudocode

Module main Declare Real d Declare Real i

Input d Input i

Display sphereShell(d, i) End Module 6 6. Execute your flowchart. Check your results with the table below.

Outside Diameter Inside Diameter Volume

1 0.5 0.458148979166665

2 1 3.66519183333333

2 1.9 0.59742626883333

20 12 3284.01188266666

100 99 15551.4089488329

Did you get the correct results? If not double check your logic, your parameters, your data types etc….

Part 5 – Computing a Shipment

Now that you can compute the volume of the caramel center (using the volume of a sphere function) and the chocolate shell (using your new function), you can finally make a program that computes orders!

You going to compute the caramel and chocolate needed for a large for a shipment of candy. To accomplish this, you need to compute the required chocolate and caramel and store those values into variables. Then, you can multiply those values by the total number of candies that will be produced.

1. Select all the shapes in your main function

2. Delete them (they were just for testing anyway)

3. Declare real variables that will store the amount of chocolate and caramel in a single candy.

4. Declare real variables that will store the total amount of chocolate and caramel for the order.

5. Declare a variable that represents the total number of candies to be produced.

6. Declare real variables for the two diameters – the whole candy and the caramel center.

7. Input both diameters. Give them nice prompts.

8. Input the total number of candies with a nice prompt.

9. Call your two functions – the volume of a sphere function and the volume of a shell function. Store the results. This will be for a single candy.

10. Compute the total amount of chocolate and caramel for the shipment. (hint: total chocolate = chocolate in one candy × the number of candies)

11. Now, output the results with nice labels.

12. Execute and test your program! 7

Part 6 – Running Total

Currently, the program just reads in values and outputs the results for a single shipment. What if they want to handle multiple orders? We can use a do-while loop for this. After each shipment, you can ask the user if they need to enter another.

You are going to need a few more variables and a loop. You will be moving most of your program inside the loop like in our previous lab.

1. Declare a string variable called again, more, or a something else (the name is up to you).

Since we are going to compute an entire shipment, we need new variables to store the grand totals for the chocolate and caramel needed. These values will start at zero and increase for each order.

2. Declare these two variables. Give them good names.

3. Add a Do Loop to your flowchart below all of the declarations.

4. Select your existing shapes from the previous parts of the lab – not the declarations. These should include the input, output and assignment shapes.

5. Cut them.

6. Paste them into the Do Loop.

7. Before the loop, initialize both of your new totals to zero.

8. Inside your loop, after the rest of the shapes, add the running total logic. This way, each time you compute the total chocolate and caramel needed for a shipment, they are added to the grand totals.

9. Okay, now we need to ask the user if they want another shipment. Input that string variable you declared earlier. Add a nice prompt.

10. Set up the Do Loop logic so it continues while: again == "y". (Of course, you might have named it something different)

11. Finally, print of the grand totals for the chocolate and caramel after the loop completes. Add some nice labels.

Like before, go to the Tools Menu on Flowgorithm and select "Layout Windows" (you can also press Control+L). This time select "Variables & Console".

EXPLORE: Variable Watch Window

Flowgorithm can graphically show you the current value of your variables. This works anytime you are stepping through your program. Select the "Variable Watch Window" from the Tools menu. There is also an icon on the main toolbar.

Different data types are displayed in different colors: integers are blue, reals are purple & strings are red.

12. Execute and test your program. 8

Part 7 – Intrinsic Functions

Many programming languages have built-in functions that designed to make it easy to write programs. These include the classic trigonometric functions such as Cos(), Sin(), and Tan(). They also often include ones designed to make the output look cleaner, work better with strings, and more! These intrinsic functions are different in easy programming language – though there is considerable overlap.

Flowgorithm has an intrinsic function that lets you control how many digits a real number will display after the decimal point. Normally, programming languages like to print all the digits they can – which is nice, but not very esthetically appealing. Take, for example, how we write out monetary values. We always use 2 digits after the decimal point: $43.50 rather than $43.5.

1. Go to the Flowgorithm documentation and look at the list of intrinsic functions.

2. Find the one that displays a fixed number of digits.

3. Use it on your final output statements. Display the grand totals with 3 decimal digits.

4. Execute your flowchart.