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 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 3-6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 3-6 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
A Larger Example: Display 3.A Predefined Function That Returns a Value 1 //Computes the size of a doghouse that can be purchased 2 //given the user's budget. 3 #include<iostream心 4 #include <cmath> 5 using namespace std; 6 int main() 7 8 const double COST_PER_SQ_FT 10.50; 9 double budget,area,lengthSide; 10 cout <<"Enter the amount budgeted for your doghouse $" 11 cin >budget; 12 area budget/COST_PER_SQ_FT; 13 lengthSide sqrt(area); Copyright006 Pearson Addison-Wesley.All rights reserved. 3-7
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 3-7 A Larger Example:
A Larger Example: 14 cout.setf(ios:fixed); 15 cout.setf(ios:showpoint); 16 cout.precision(2); 17 cout <<"For a price of $"<budget <endl 18 <<"I can build you a luxurious square doghouse\n" 19 <<"that is "<<lengthSide 20 <<"feet on each side.\n"; 21 return 0; 22} SAMPLE DIALOGUE Enter the amount budgeted for your doghouse $25.00 For a price of $25.00 I can build you a luxurious square doghouse that is 1.54 feet on each side. Copyright 2006 Pearson Addison-Wesley.All rights reserved. 3-8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 3-8 A Larger Example:
More Predefined Functions ◆include<cstdlib> Library contains functions like: ◆abs0 /Returns absolute value of an int ◆labs0 /Returns absolute value of a long int ·*fabs0∥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 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 3-9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 3-9 More Predefined 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
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.02.0=9.0 Notice this function receives two arguments A function can have any number of arguments,of varying data types Copyright 2006 Pearson Addison-Wesley.All rights reserved. 3-10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 3-10 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.02.0 = 9.0 ¨ Notice this function receives two arguments ¨ A function can have any number of arguments, of varying data types