1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Programming in C++ - Chapter 7: Functions

30 72 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 30
Dung lượng 528,7 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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.

Trang 1

Chapter 7 ­ Functions

Trang 2

Code group that performs single task

Specification refers to what goes into and out of function

Trang 4

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

Trang 5

Indicates function name, return type and types of values passed to function

Trang 6

Transfers program control to functionGeneral form

Trang 7

ftype fname (atype aname, atype aname)     {   

      …

      …

     }

Trang 8

mass should be declared 

   an integer and velocity

   a double in program

Trang 9

– if within, can only be called from function where  declared

Function declaration MUST appear before call

Lesson 7.1

Trang 10

– Can prevent lost and maintain

Called multiple times, allocated and freed repeatedly

Lesson 7.1

Trang 13

else    {     return (b);     }

Trang 14

Returning Value From  main

void type function do not return values

– control transfers back to calling function  automatically

Trang 15

Default

Function called

– Memory set aside and value copied into new  memory location

– Calculations done inside function using value – When function finished executing, memory  location destroyed

Lesson 7.2

Trang 16

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

Trang 17

b        c

d         e

Trang 18

Refers to region in which declaration is activeThree kinds of scope

Trang 19

for (int n=0;n<8;n++)     {

     }

Function main ( )     int  x;

for (int j=0;j<8;j++)     {

Trang 20

Allows manual modification to scope and storage rules

Lesson 7.5

Trang 21

Variable maintains storage space and value after function finishes executing

Trang 22

– With extern from any file

Lesson 7.5

Trang 23

Multiple files help manage large programsEach compiled separately then linked to 

create executable file

Share variable

– Variable declared as global without specifier in  one file

– Other files     extern  type variable;

Lesson 7.5

Trang 25

values: 15.0 for second argument and 8 for third

Cannot specify first and third and use default for second – must have all defaults last!

Lesson 7.6

Trang 26

Defining two or more functions with same name

Each function takes different number of arguments

– C++ knows which function is being called by  counting number of arguments used

– Should not use default arguments since 

ambiguity could result

Lesson 7.7

Trang 28

rand( ) and srand ( ) work together

– srand ( ) automatically "seeds" function rand ( )  – Functions linked through global variable

Trang 30

Define and call functions

Determine the scope of a variablePass values by reference

Overload functions

Create random numbers

Chapter 7

Learned how to:

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

TỪ KHÓA LIÊN QUAN