Page 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. Trương Công Tuấn Đơn vị: Bộ môn Cơ điện tử, Viện Cơ khí 1
Génération de l'aperçu...
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. Trương Công Tuấn Đơn vị: Bộ môn Cơ điện tử, Viện Cơ khí 1
Chapter II. Modular programming in C++ • Structured programming relies on top-down design, which consists of dividing programming tasks into smaller units until program blocks easy to handle and to test are achieved. 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. Modules can be compiled and tested separately, and they can be imported into other projects as well. The contents of modules (compiled or source code version) can be made available for other modules by interfaces (header files in C/C++). And certain parts of these modules are hidden from the outside (data hiding). Structural programming also contributes to creating new programs from achieved modules (components) by bottom-up design. • The next parts aim at introducing our readers into modular and procedural programming in C++. Procedural programming means solving a task by subprograms (functions) that are more or less independent from one another. These subprograms call one another directly or indirectly from the main program (main()) and communicate with each other by parameters. Procedural programming can be well combined with structural and modular programming. 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. A traditional C++ program has small size and easy to handle functions. Compiled functions can be put in libraries, from which the development kit integrates the code of the referenced functions in our programs. • In order to use a function efficiently, some of its inner variables are assigned a value when the function is called. These storage units called parametersshould be declared in parentheses in the function definition after the function name. When a function is called (activated), the values (arguments) to be assigned to each parameter have to be passed in a similar way. • When a function is called, arguments (if there are) are passed to the called function and control passes to the activated function. After the algorithm of a function is executed by a return statement, or the physical end of the function is reached, 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. We only have to declare these functions before using them. For that purpose, the corresponding header file should be included in the source code. 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): A parameter declaration list in the function header enumerates each parameter separated by each other by a comma, and every parameter is preceded by its type. Function definition 8
The basics of functions Defining, calling and declaring functions • The steps of calling a function function_name (〈argument , argument , … argument 〉) 1 2 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. Defining a function is therefore declaring a function. Function declaration Function definition 11
The basics of functions Defining, calling and declaring functions • The complete declaration of a function (its prototype ) contains the name and the type of the function and provides information about the number and the type of the parameters: 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 (...). A function defined in that way can be called with at least one parameter but also with any number or type of arguments. Let's look at the prototype of the function sscanf (). • 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 • In a C++ function definition, each parameter is preceded by its type, no simplification is allowed. A declared parameter can be used as a local variable within a function; however, it is only accessible from the outside when they are passed as arguments. 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 If the type qualifier const is placed in the parameter list, we can restrict the modification of the memory space to which a pointer points ( const double*p) and restrict the modification of the value of the pointer ( double * const p) within a function. 19
The basics of functions Parametrizing functions Parameter passing methods • Passing parameters by reference 20
Affichage des 20 premières pages. Téléchargez le document pour le voir en entier.
Aucun commentaire pour le moment. Soyez le premier !
Commentaires (0)
Aucun commentaire pour le moment. Soyez le premier !