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

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.

## This note was uploaded on 11/07/2011 for the course COP 3014 taught by Professor Tyson during the Fall '10 term at FSU.

### Page1 / 34

Function Basics - Function Basics Introduction to Functions...

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

View Full Document
Ask a homework question - tutors are online