
Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 2 - TS. Nguyễn Thành Hùng
lượt xem 1
download

Bài giảng "Kỹ thuật lập trình hệ cơ điện tử" Chương 2 - Lập trình mô-đun trong C++, cung cấp cho sinh viên những kiến thức như: Cơ bản về hàm; Làm thế nào để sử dụng hàm ở cấp độ chuyên nghiệp hơn; Không gian tên và lớp lưu trữ; Chỉ thị tiền xử lý của C++;...Mời các bạn cùng tham khảo!
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 2 - TS. Nguyễn Thành Hùng
- 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 bottom- up 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() cmath isalpha() cctype atoi() cstdlib rand() cstdlib strlen() cstring wcslen() 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

CÓ THỂ BẠN MUỐN DOWNLOAD
-
Bài giảng Kỹ thuật thi công 1: Phần III - Đặng Xuân Trường
150 p |
360 |
134
-
Bài giảng Kỹ thuật lập trình vi điều khiển - ĐH Phạm Văn Đồng
115 p |
163 |
33
-
Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 9 - TS. Đặng Thái Việt
34 p |
20 |
4
-
Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 8 - TS. Đặng Thái Việt
43 p |
31 |
4
-
Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 3 - TS. Đặng Thái Việt
48 p |
14 |
3
-
Bài giảng Kỹ thuật thi công - Trường Đại học Kỹ thuật Công nghiệp
193 p |
11 |
3
-
Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 13 - TS. Đặng Thái Việt
83 p |
27 |
3
-
Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 12 - TS. Đặng Thái Việt
16 p |
16 |
3
-
Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 11 - TS. Đặng Thái Việt
22 p |
23 |
3
-
Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 10 - TS. Đặng Thái Việt
33 p |
12 |
3
-
Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 1 - TS. Đặng Thái Việt
33 p |
18 |
3
-
Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 2 - TS. Đặng Thái Việt
25 p |
21 |
3
-
Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 7 - TS. Đặng Thái Việt
31 p |
19 |
3
-
Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 6 - TS. Đặng Thái Việt
61 p |
13 |
3
-
Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 5 - TS. Đặng Thái Việt
35 p |
19 |
3
-
Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 4 - TS. Đặng Thái Việt
36 p |
10 |
3
-
Bài giảng Kỹ thuật và công nghệ thi công: Chương 8 - TS. Mã Chí Hiếu
50 p |
1 |
1
-
Bài giảng Kỹ thuật thi công (Phần 3): Chương 2 - Th.S Đỗ Thị Xuân Lan
71 p |
1 |
1


Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn
