tailieunhanh - Lecture Programming in C++ - Chapter 7: Functions

Lecture Programming in C++ - Chapter 7: Functions. On completion of this chapter students will know how to: Define and call functions, determine the scope of a variable, pass values by reference, overload functions, create random numbers. | Chapter 7 - Functions Functions Code group that performs single task Specification refers to what goes into and out of function Design refers to function’s task(s) Two groups Library Programmer-defined Lesson Function Categories Return no value Return a single value Use “pass by reference” Lesson Function Basics Need three things to use function Function declaration (prototype) Indicates function exists and describes it Must be done before use of function Function call (invocation) Causes control to transfer from function to function Function definition Header line followed by function body enclosed in braces Lesson Function Declaration Indicates function name, return type and types of values passed to function General form ftype fname (atype, atype); ftype is function return type fname is function name atype is argument type (may be different) multiple argument separated by commas Lesson Function Call Transfers program control to function General form fname (exp1, . | Chapter 7 - Functions Functions Code group that performs single task Specification refers to what goes into and out of function Design refers to function’s task(s) Two groups Library Programmer-defined Lesson Function Categories Return no value Return a single value Use “pass by reference” Lesson Function Basics Need three things to use function Function declaration (prototype) Indicates function exists and describes it Must be done before use of function Function call (invocation) Causes control to transfer from function to function Function definition Header line followed by function body enclosed in braces Lesson Function Declaration Indicates function name, return type and types of values passed to function General form ftype fname (atype, atype); ftype is function return type fname is function name atype is argument type (may be different) multiple argument separated by commas Lesson Function Call Transfers program control to function General form fname (exp1, exp2); fname is function name exp1 and exp2 are arguments Variables or constants Called argument list Lesson Function Definition Includes function’s executable statements General form (example with two arguments) Lesson Function return type Function return type Function argument declarations Function body ftype fname (atype aname, atype aname) { } Argument Lists Lists in function call and function header must coordinate number order type Example: Lesson kinetic_energy (mass, velocity); void kinetic_energy (int m, double v) Function call in main Function header 2 arguments in each mass should be declared an integer and velocity a double in program Function Arrangement Not necessary for first function to be main Function definition must be located outside body of any other function Can write function declaration outside body of all functions or within function if within, can only be called from function where declared Function declaration MUST appear before call Lesson

TỪ KHÓA LIÊN QUAN