tailieunhanh - C++ Programming for Games Module I phần 4

Tham khảo tài liệu 'c++ programming for games module i phần 4', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Chapter 3 Functions Introduction 75 A function is a unit of code designed to perform a certain task. In order to perform its task a function typically inputs some information and or returns some information. The concept is somewhat similar to mathematical functions. Consider the trigonometric function sin x which takes a parameter x and evaluates returns some value namely the sine of x. For example the sine of 45 is approximately sin 45 that is to say given 45 as a parameter the function sin x works to compute the sine of the given angle and then returns or evaluates to the result . The utility of functions can be be easily demonstrated if we first consider a program without them. To illustrate suppose we have a program that must input the radius of a circle from the user and compute the area throughout the program. Recall the area of a circle is given by A n- r2 where r is the radius of the given circle. As a first attempt we might do the following Program Program without Functions. include iostream using namespace std int main float PI Input a radius and output the circle area. float radius cout Enter a radius of a circle cin radius float area PI radius radius cout Area area endl Do some other work. cout Other work. endl Input another radius and output the circle area. cout Enter a radius of a circle cin radius area PI radius radius cout Area area endl Do some other work. cout Other work. endl Input another radius and output the circle area. cout Enter a radius of a circle cin radius area PI radius radius cout Area area endl and so on. Output . Enter a radius of a circle 2 76 Area Other work. Enter a radius of a circle 3 Area Other work. Enter a radius of a circle 1 Area Press any key to continue The main problem which Program suffers is code duplication there is duplicate code which essentially performs the same task. Besides bloating the code programs with duplicated code are hard to maintain because if .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.