{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

manskijoshCSE100Lab06

manskijoshCSE100Lab06 - Lab Section Name Lab 6 Functions...

Info icon This 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
Image of page 1

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

View Full Document Right 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, in the example). So in this case if yourScore were equal to 10, then pow would set myScore equal to 100.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern