Tải bản đầy đủ (.pdf) (132 trang)

Kỹ thuật lập trình hệ cơ điện tử= programming engineering in mechatronics chapter ii modular programming in c++

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.34 MB, 132 trang )

TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI

KỸ THUẬT LẬP TRÌNH HỆ CƠ ĐIỆN TỬ

Programming Engineering in Mechatronics
Giảng viên: TS. Nguyễn Thành Hùng
Đơn vị: Bộ môn Cơ điện tử, Viện Cơ khí

Hà Nội, 2020

1


Chapter II. Modular programming in C++
• Structured programming relies on top-down design.
• In C and C++ languages, the smallest structural unit having
independent functionality is called function.
• If functions or a group of functions belonging together are
put in a separate module (source file), modular
programming is realised.

• Structural programming also contributes to creating new
programs from achieved modules (components) by bottomup design.
• This chapter aims to introduce the modular and procedural
programming in C++.
2


Chapter II. Modular programming in C++
❖ The basics of functions


❖ How to use functions on a more professional level?
❖ Namespaces and storage classes
❖ Preprocessor directives of C++

3


Chapter II. Modular programming in C++
❖ The basics of functions

❖ How to use functions on a more professional level?
❖ Namespaces and storage classes
❖ Preprocessor directives of C++

4


The basics of functions
❖ Defining, calling and declaring functions
❖ The return value of functions

❖ Parametrizing functions
❖ Programming with functions

5


The basics of functions
• In C++, a function is a unit (a subprogram) that has a name and that can
be called from the other parts of a program as many times as it is

needed.

• In order to use a function efficiently, some of its inner variables
(parameters) are assigned a value when the function is called.
• When a function is called (activated), the values (arguments) to be
assigned to each parameter have to be passed in a similar way.

• The called function passes control back to the place where it was called
by a return statement.
• The value of the expression in the return statement is the return
value returned back by the function, which is the result of the function
call expression.

6


The basics of functions
❖ Defining, calling and declaring functions


C++ Standard Library provides us many useful predefined
functions.
function

header file

sqrt()
isalpha()
atoi()
rand()


cmath
cctype
cstdlib
cstdlib

strlen()
wcslen()

cstring
cwchar
7


The basics of functions
❖ Defining, calling and declaring functions


The general form of a function definition is the following (the
signs 〈 〉 indicate optional parts):

Function definition

8


The basics of functions
❖ Defining, calling and declaring functions

9



The basics of functions
❖ Defining, calling and declaring functions


The steps of calling a function
function_name (〈argument 1 , argument 2 , … argument n 〉)

Steps of calling a function
10


The basics of functions
❖ Defining, calling and declaring functions


C++ standards require that functions have to be declared before
they are called.
Function declaration

Function definition

11


The basics of functions
❖ Defining, calling and declaring functions



The complete declaration of a function (its prototype ):
return_value function_name(〈parameter declaration list〉);
return_value function_name(〈type_list〉);

declaration

C interpretation

C++ interpretation

type funct();

type funct(...);

type funct(void);

type funct(...);

type funct(...);

type funct(...);

type funct(void);

type funct(void);

type funct(void);

12



The basics of functions
❖ Defining, calling and declaring functions


C++ makes it possible that a parameter list containing at least one parameter
should end with three dots (...).



The transferring (throw) of exceptions to the caller function can be enabled or
disabled in function header
return_type function_name (〈parameterlist〉) 〈throw(〈type_list〉)〉
{
〈local definitions and declarations〉
〈statements〉
return 〈expression〉;
}

13


The basics of functions
❖ The return value of functions


The return_type figuring in the definition/declaration of a function
determines the return type of the function, which can be of any C++ type with
the exception of arrays and functions.


14


The basics of functions
❖ The return value of functions


By using the type void, we can create functions that do not
return any value.

15


The basics of functions
❖ The return value of functions


Functions can return pointers or references

16


The basics of functions
❖ Parametrizing functions


A parameter can be scalar (bool, char, wchar_t, short, int, long, long long,
float, double, enumeration, reference and pointer) or structure, union, class
or array.
The general form of polynomials:

Horner's scheme

17


The basics of functions
❖ Parametrizing functions


Parameter passing methods



Passing parameters by value

18


The basics of functions
❖ Parametrizing functions


Parameter passing methods



Passing parameters by value

19



The basics of functions
❖ Parametrizing functions


Parameter passing methods



Passing parameters by reference

20


The basics of functions
❖ Parametrizing functions


Parameter passing methods



Passing parameters by reference

21


The basics of functions
❖ Parametrizing functions



Parameter passing methods



Passing parameters by reference

22


The basics of functions
❖ Parametrizing functions


Using parameters of different types



Arithmetic type parameters

23


The basics of functions
❖ Parametrizing functions


Using parameters of different types




Arithmetic type parameters

24


The basics of functions
❖ Parametrizing functions


Using parameters of different types



User-defined type parameters

25


×