manskijoshCSE100Lab06 - Lab Section: _ Name: _ Lab 6...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lab Section: _____ Name: _________________________________ Lab 6 Functions Pre-Lab: Please read the pre-lab and answer the accompanying questions before your lab session. In this lab session, you will learn: when to use functions how to write algorithms for functions how to call functions what the basic parts of function syntax are how to write custom functions Earlier when we learned to write out algorithms, we noticed that some plans repeat steps over again. We were able to use loops to make repeating code easier to program. Similarly, functions allow us to write code that performs a specific task and can be called multiple times. This makes it possible to split up and organize code in a more efficient manner. When writing your plan, you may notice similar steps being repeated. For example, in this algorithm: Get name of first puppy Get breed of first puppy Calculate cuteness factor of first puppy Get name of second puppy Get breed of second puppy Calculate cuteness factor of second puppy Compare cuteness factors Output which puppy is cuter The parts to get the name, breed, and cuteness of each puppy are very similar. In fact, the only real difference between them is that one is the "first" and one is the "second". It would be nice if we could shorten the algorithm to look like this: Get information for first puppy Get information for second puppy Compare cuteness factors Output which puppy is cuter With the magic of functions, now we can! Functions are subprograms (separated, independent sections of code) that perform a specific task. A function is like a machine in a factory: you put raw materials in the machine, it cranks some gears, blows some whistles, hopefully doesn't catch fire, and out the other end comes a finished part. Similarly, when you call a function, you pass some values into it, it performs some computations, and returns a value. We'll cover the details of how that works later. Lab 6 1/7
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
There are two parts to using functions: writing them and calling them. Writing them is more complex, so we'll address calling them first. Here are some examples of calling functions: finalDistance = abs(initialDistance); printf("I've got a lovely bunch of coconuts!"); system("pause"); myScore = pow(yourScore, 2); The first example is the function abs , which takes a number and returns its absolute value. To call it, you take a variable ( finalDistance , in this case) and set it equal to abs . This means that whatever value is returned by abs will be assigned to that variable. Then, inside the parentheses, you give the name of a variable or variables that you want to have serve as the input ( initialDistance , in this case). Each thing you put in between the parentheses is called an argument or a parameter to the function. Most functions get passed at least one argument, and many get passed more. For instance, the function pow takes its first argument ( yourScore , in the example) and raises it to the power of the second argument (2,
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/20/2010 for the course CSE 100 taught by Professor Na during the Spring '09 term at University of Arizona- Tucson.

Page1 / 7

manskijoshCSE100Lab06 - Lab Section: _ Name: _ Lab 6...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online