Lecture Programming in C++ - Chapter 7: Functions

30 72 0
Lecture Programming in C++ - Chapter 7: Functions

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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 7.1 Function Categories Return no value Return a single value Use “pass by reference” Lesson 7.1 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 7.1 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 7.1 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 7.1 Function Definition Includes function’s executable statements General form  (example with two arguments)       ftype fname (atype aname, atype aname)      {    Function return type Function return type           … Function body           … Function argument declarations      } Lesson 7.1 Argument Lists Lists in function call and function header  must coordinate – number – order – type mass should be declared     an integer and velocity Example: Function call in main 2 arguments in each    a double in program kinetic_energy (mass, velocity); Function header void kinetic_energy (int m, double v) Lesson 7.1 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 7.1 Function Storage When function called, memory allocated for – Variables in argument list – Variables declared in function body When completes execution, memory freed  and variable values lost – Can prevent lost and maintain Called multiple times, allocated and freed  repeatedly Lesson 7.1 Pass By Reference Use argument list to allow function to  directly use calling function's values Reference symbol  & required in function  declaration and header – Indicate arguments that will have values  modified – Create aliases for original variable names Lesson 7.3 Pass By Reference Example: Declaration void  func_name (int, double, double&, int&); x        y        z        a func_name (x, y , z, a); Call Header d         e void func_name ( int b, double c, double& d, int& e) b        c Lesson 7.3 Scope Refers to region in which declaration is active Three kinds of scope – Block Variable valid with block of code enclosed in braces – Function Variable valid only within function where defined – File Variable valid throughout all modules in file Determined by identifier's declaration location Lesson 7.4 Scope File containing functions main ( ) and calc ( ) int  days; Function main ( )     int  x; days x days for (int j=0;j

Ngày đăng: 30/01/2020, 01:21

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan