intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

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

Chia sẻ: _ _ | Ngày: | Loại File: PDF | Số trang:132

2
lượt xem
1
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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!

Chủ đề:
Lưu

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

  1. 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
  2. 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
  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++ 3
  4. 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
  5. The basics of functions ❖ Defining, calling and declaring functions ❖ The return value of functions ❖ Parametrizing functions ❖ Programming with functions 5
  6. 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
  7. 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
  8. 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
  9. The basics of functions ❖ Defining, calling and declaring functions 9
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. The basics of functions ❖ The return value of functions • Functions can return pointers or references 16
  17. 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
  18. The basics of functions ❖ Parametrizing functions ▪ Parameter passing methods • Passing parameters by value 18
  19. The basics of functions ❖ Parametrizing functions ▪ Parameter passing methods • Passing parameters by value 19
  20. The basics of functions ❖ Parametrizing functions ▪ Parameter passing methods • Passing parameters by reference 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
11=>2