{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Function Basics

# Function Basics - Function Basics Introduction to Functions...

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

Function Basics

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern