Your First Function

Your First Function

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 programming language. 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 C Family (which includes Java), modules and functions are synonymous. Pascal calls a module a "procedure". Visual Basic calls it a "sub" (for subroutine). 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.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us