Function Basics

Function Basics - CHAPTER 3 FUNCTION BASICS In this chapter...

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

View Full Document Right Arrow Icon
CHAPTER 3 FUNCTION BASICS
Background image of page 1

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

View Full DocumentRight Arrow Icon
In this chapter, you will: Learn about standard (predefined) functions Learn about user-defined functions Construct value-returning functions, including actual and formal parameters
Background image of page 2
Functions are like building blocks . They allow complicated programs to be divided into manageable pieces. They are often referred to as modules. They are like miniature programs. They can be put together to form a larger program.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Advantages of functions: Its lets you focus on a part of the program and thereby you can construct it, debug it, and perfect it independently. Different people can work on different functions simultaneously. If a function is needed in more than one place in a program, or in different programs, you can write it once and use it many times.
Background image of page 4
Pre-defined Functions Some of the pre-defined mathematical functions are abs(x) , sqrt(x) , and pow(x,y) . The power function, pow(x,y) , calculates x y ; that is, the value of pow(x,y) = x y . pow(2,3) = 8.0 and pow(2.5,3) = 15.625 . The function pow is of the type double or that the function pow returns a value of the type double . x and y are called the parameters (or arguments) of the function pow .
Background image of page 5

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

View Full DocumentRight Arrow Icon
The s quare root function, sqrt(x) , calculates the non- negative square root of x for x >= 0.0 . sqrt(2.25) is 1.5 . The function sqrt is of the type double and has only one parameter. I/O functions are contained in the header file iostream . Math functions are contained in the header file cmath .
Background image of page 6
Background image of page 7

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

View Full DocumentRight Arrow Icon
Example: // How to use predefined functions. #include <iostream> #include <cmath> #include <cctype> #include <cstdlib> using namespace std; int main() { int x; double u,v; cout<<"Line 1: Uppercase a is " << static_cast < char >(toupper('a')) <<endl; //Line 1 u = 4.2; //Line 2 v = 3.0; //Line 3 cout<<"Line 4: "<<u<<" to the power of " <<v<<" = "<<pow(u,v)<<endl; //Line 4 cout<<"Line 5: 5 to the power of 4 = " <<pow(5,4)<<endl; //Line 5
Background image of page 8
u = u + pow(3,3); //Line 6 cout<<"Line 7: u = "<<u<<endl; //Line 7 x = -15; //Line 8 cout<<"Line 9: Absolute value of "<<x <<" = "<<abs(x)<<endl; //Line 9 return 0; } Output: Line 1: Uppercase a is A Line 4: 4.2 to the power of 3 = 74.088 Line 5: 5 to the power of 4 = 625 Line 7: u = 31.2 Line 9: Absolute value of -15 = 15
Background image of page 9

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

View Full DocumentRight Arrow Icon
USER-DEFINED FUNCTIONS User-defined functions in C++ Value-returning functions - functions that have a data type. Void functions - functions that do not have a data type.
Background image of page 10
VALUE-RETURNING FUNCTIONS Some predefined C++ functions are: pow , islower, toupper . To use these functions in our programs we must know: • Name of the header file containing this function. • The name of the function
Background image of page 11

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

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/14/2008 for the course CS 115 taught by Professor Ryon during the Fall '06 term at NJIT.

Page1 / 40

Function Basics - CHAPTER 3 FUNCTION BASICS In this chapter...

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

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