tailieunhanh - Chapter 3 Function Basics

Transfers control back to "calling" function For return type other than void, MUST have return statement Typically the LAST statement in function definition return statement optional for void functions. | Chapter 3 Function Basics Learning Objectives Predefined Functions Those that return a value and those that don’t Programmer-defined Functions Defining, Declaring, Calling Recursive Functions Scope Rules Local variables Global constants and global variables Blocks, nested scopes Introduction to Functions Building Blocks of Programs Other terminology in other languages: Procedures, subprograms, methods In C++: functions I-P-O Input – Process – Output Basic subparts to any program Use functions for these "pieces" 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 ., , (Original "C" libraries) (for cout, cin) Using Predefined Functions Math functions very plentiful Found in library Most return a value (the "answer") Example: theRoot = sqrt(); Components: sqrt = name of library function theRoot = variable used to assign "answer" to = | Chapter 3 Function Basics Learning Objectives Predefined Functions Those that return a value and those that don’t Programmer-defined Functions Defining, Declaring, Calling Recursive Functions Scope Rules Local variables Global constants and global variables Blocks, nested scopes Introduction to Functions Building Blocks of Programs Other terminology in other languages: Procedures, subprograms, methods In C++: functions I-P-O Input – Process – Output Basic subparts to any program Use functions for these "pieces" 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 ., , (Original "C" libraries) (for cout, cin) Using Predefined Functions Math functions very plentiful Found in library Most return a value (the "answer") Example: theRoot = sqrt(); Components: sqrt = name of library function theRoot = variable used to assign "answer" to = argument or "starting input" for function In I-P-O: I = P = "compute the square root" O = 3, which is returned & assigned to theRoot The Function Call Back to this assignment: theRoot = sqrt(); The expression "sqrt()" is known as a function call, or function invocation The argument in a function call () 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 A Predefined Function That Returns a Value (1 of 2) A Larger Example: Display A Predefined Function That Returns a Value (2 of 2) More Predefined Functions #include 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 ! Can be confusing Remember: libraries were added