{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Function Basics

Function Basics - Function Basics Introduction to Functions...

Info icon This preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Function Basics
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
Introduction to Introduction to Functions Functions The most useful and important series of lectures you will have. Most students avoid using functions. You shouldn’t Learn how to use them and use them well! Copyright Dr. David A. Gaitros, Florida State University, 2011. 3-2
Image of page 2
Predefined Functions Predefined Functions Libraries full of functions for our use! Two types: Those that return a value Those that do not (void) Must "#include" appropriate library e.g., <cmath>, <cstdlib> (Original "C" libraries) <iostream> (for cout, cin) 3-3
Image of page 3

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

View Full Document Right Arrow Icon
Using Predefined Using Predefined Functions Functions Math functions are very plentiful Found in library <cmath> Most return a value (the "answer") Example: theRoot = sqrt(9.0); // theRoot – Where the answer goes // = - The Assignment operator // sqrt – The function in cmath // = library // (9.0) – The parameter, what we // want the square root of 3-4
Image of page 4
The Function Call The Function Call Back to this assignment: theRoot = sqrt(9.0); The expression "sqrt(9.0)" is known as a function call , or function invocation The argument in a function call (9.0) can be a literal, a variable, or an expression The call itself can be part of an expression: bonus = sqrt(sales)/10; A function call is allowed wherever it’s legal to use an expression of the function’s return type. In this case double. 3-5
Image of page 5

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

View Full Document Right Arrow Icon
Functions Functions Modules Divide problems into smaller pieces that can be solved individually easier. Divide and conquer A function is written once but can be called over and over again. Statements are hidden from other functions Access function via a function call consisting of: Function Name Arguments Return value (if specified) Copyright Dr. David A. Gaitros, Florida State University, 2011. 3-6
Image of page 6
More Predefined More Predefined Functions Functions #include <cstdlib> Library contains functions like: abs() // Returns absolute value of an int labs() // Returns absolute value of a long int *fabs() // Returns absolute value of a float *fabs() is actually in library <cmath>! Can be confusing Remember: libraries were added after C++ was "born," in incremental phases Refer to appendices/manuals for details 3-7
Image of page 7

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

View Full Document Right Arrow Icon
More Math Functions More Math Functions pow(x, y) Returns x to the power y double result, x = 3.0, y = 2.0; result = pow(x, y); cout << result; Here 9.0 is displayed since 3.0 2.0 = 9.0 Notice this function receives two arguments A function can have any number of arguments, of varying data types 3-8
Image of page 8
Even More Math Even More Math Functions Functions Name Descr Arg type Retur n Library sqrt square root doubl e double cmath pow absolute doubl e double cmath abs int int int cstdlib labs absolute long long long cstdlib fabs absolute double doubl e double cmath ceil round up doubl e double cmath floor round down doubl e double cmath exit end prog int void cstdlib rand random num none int cstdlib srand set seed unsig ned int void cstdlib Copyright Dr. David A.
Image of page 9

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

View Full Document Right Arrow Icon
Image of page 10
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