Vous êtes sur la page 1sur 10

Low-level Languages Machine Languages The fundamental language of any computer is machine language which essentially consist of strings

of zeros and ones. Writing programs in machine language is extremely time consuming and tedious, Assembly Languages Assembly language allows the programmer to represent machine language instructions by means of mnemonic words and symbols. To use assembly language the programmer must have a thorough understanding of the internal architecture of the computer. Although assembly language is a significant improvement over machine language programming, it is still a low-level language in that it is oriented more towards the computer than towards the people programming the computer. . !. "igh-level Languages "igh-level languages are geared more towards the people writing the programs rather than the computer. These languages provide the interface between the user and the machine. They are closer to #nglish but are sufficiently rigorous to permit the computer to translate the programs into machine language. This simplifies the tas$ of programming and saves time. Advantages of low-level programming language; %. &achine level programs run faster and re'uire less memory . (ome tas$s that re'uire direct access to hardware features are more suitable for programmed in machine or assembly language Disadvantages of low-level programming language; %. )rograms are machine dependent and cannot be easily transferred from one computer system to another. )rograms written in these languages can be executed only on computers of identical design. . )rogramming is very time consuming to learn. *t re'uires considerable experience to become proficient. +. &ost of the program is occupied with internal details which have very little to do with actual tas$ to be accomplished !. )rograms are difficult to alter due to their complexity ,. *t is easy to introduce errors

Advantages of high-level programming languages include the following: %. These languages are problem oriented. Their structure and operations closely resemble the language in which the problem is formulated rather than the structure and organisation of the computer . The programmer does not need to $now about the organisation or internal architecture of the computer +. Learning to program in a high-level language is easy since the programs are written in a language that the programmer is accustomed. !. "igh-level programs re'uire fewer statements. A single source statement generates many machine language statements. ,. Theoretically, high-level programs are machine independent, and programs can be run on a great many different computers. *n practice, however, there are considerable variation among different computer systems, and this can cause programs in programming languages such as -.-// to be machine dependent and less easily transportable from one computer system to another. 0ava, on the other hand, is designed to machine independent. 1. "igh-level languages have extensive error diagnostics. These help the programmer in locating and correcting errors in the programs and generally result in a more friendly environment. 0. In C++, the fundamental types include: char, short, int, long, float, double and bool where: type char includes characters, types short, int, and long are integers (numbers without fractions) types float and double are floating point numbers(i.e. reals, numbers with fractions expressed in a form of scientific notation) type bool defines a boolean ariable that can stored one of two alues: true or false. !ormally true is numerically " and false is numerically 0.

Statement #he statement element is used to gi e a graphical representation of a program statement. #hese would normally be an assignment or a calculation that the program would need to perform. Decision #he decision element shows where the flow of control within a program can ta$e one of two different paths. If some condition is true the %y% or %yes% path is ta$en otherwise the %n% or %no% path is tra elled. Input/Output #he input&output element is used to show where the program communicates with the $eyboard or the screen. the if statement #he basic decision ma$ing statement in the C++ language is the if statement. #he simplest form of the if statement is: #his is shown in flowchart form as: if (condition) statement; The if - else statement #he if statement allows to chose whether or not to execute a statement (or bloc$ of statements). #he if ' else statement allows us to chose between two statements (or two bloc$s of statements). #he format of this statement is: or in flowchart form: if (condition) statement1; else statement2; #he while statement is the main looping statement in the C++ language. #he general form of the while loop is : while (condition) statement;

#he general form of the do'while loop is shown below. do{ statement; } while(condition);

The for loop #he general form of the for loop is: for (initialisation; condition; increment) statement;

(. What is a function? A -// function is a bloc$ of statements called by name to carry out a specific tas$, for example, mathematical operations on variables. A function is invo$ed 2i.e. made to perform its designated tas$3 by a function call. The function call specifies the function name and provides information 2arguments3 that the called function needs to do its 4ob. When the function completes its 4ob, it can return a value to the calling function. All programs have at least one function called main. There are many examples of functions in the -// Library, such as s'rt to calculate the s'uare root of a number. We can also write our own functions to perform a particular tas$.

Why might a programmer want to write his or her own functions? To avoid repetition5 6ften a programmer needs to use a certain se'uence of code or perform a particular mathematical operation in more than one place within a program. 7y using functions long se'uences of code or large mathematical operations do not have to be rewritten. To ma$e a program modular5 &odularity is a well researched topic in software engineering and computer science. The main reason for modularity is that, if a program is made up of distinct, well defined modules 2functions3, it is easier to understand and change the program if necessary. To allow code reuse5 Li$e system-defined functions user-defined functions can be used in more than one program. This means that a programmer can build up a supply of useful functions that are available for use in other programs. 0. Modularity 8sing a Top-9own design approach, a problem is bro$en down into smaller more manageable sub- problems, each problem is then considered independently. The program is then decomposed into a number of smaller independent modules. A module usually has its own instructions and data. With &odular design in -.-//, the overall tas$ is performed by the main function and each sub-tas$ is performed by a separate function. When designing software for a system, first it is important to identify the subtas$s and the decisions about information flow. (ystem design consists of ta$ing a functional specification of a software system and expressing that system as a series of functions. :or example5 system design identifies5 What functions should be used; What data should be used; What are the important variables and constants to use and where are these to be declared; "ow will the various functions interact as the program executes; What are the details of the interfaces, e.g. what data will be passed between functions; Writing a programmer-defined function When writing a programmer-defined function there are three elements5 ! "he #unction $rototype %! "he #unction Definition

&! "he #unction call statement ". %.,.% #unction $rototype (tandard programming style dictates that a function definition is placed in the source file after the main program. To allow the main program to use the function, a function prototype must be inserted at the top of the file above main. A function prototype is a copy of the first line of the function definition with a semicolon at the end i.e. #unction'"ype #unction'(ame )$arameter'List*; The function prototype defines the interface with the function and allows the main program 2or another function3 to use the particular function. The function prototype declares what type of input parameters are re'uired by the function and what type of data is returned by the function. . %.,. #unction Definition As we have stated the programmer has the ability to write new functions as re'uired. To do this he or she has to define how the function operates. This is the purpose of the function definition. A function definition has the following format5 #unction'"ype #unction'(ame )$arameter'List* + Declarations ,tatements #unction'"ype: This specifies the value that the function returns, if any. *t can be any -// data type. When programmers define their own functions, they have to explicitly state the data types the functions will return. *t is possible to have functions that do not return any value. These are mainly output type functions. When a function is not re'uired to return a value then its type is the data type void. #unction'(ame: This is the name of the function. <ames should be chosen that do not conflict with names of variables, constants or other functions. $arameter'List: The parameter list is a list of the variables used by the function to hold the data that is passed to it. *t has the following format5 )type var . type var%. type var&. !!!!!. type var(* The type of each variable must be stated explicitly even if a group of variables have the same type. (ome functions have no parameters. The system defined function rand, for instance, generates a random number. *t does not re'uire any values from

the main program. *f a function has no parameters, the word void is used in place of the parameter list. (/"05 The parameters in the parameter list of a function definition are $nown as the formal parameters. They are created when the function is called and are discarded when the function finishes. 1ody5 The body of a function is enclosed in 7races =>. The body contains a declaration section and a set of statements. The declaration section consists of declarations for constants and variables that can only be used within the function. *f none of these local variables.constants are re'uired then the declaration section can be omitted. The declaration section is followed by the statements of the function. These are normal -// statements and they can use the local variables and constants of the function and the formal parameters in the parameter list. *f a function returns a value to the main program it must have a return statement. The return statement has the following format. return value; where value is an expression or variable of the same type as the function i.e. :unction?Type above. %.,.+ #unction 2all )rogrammer-defined functions are called the same way as built-in function, i.e. by writing the name of the function, with any arguments listed between parenthesis #unction'(ame )argument list*;

2.1.1 Declaring a function: The :8<-T*6< )@6T6TA)# is used to declare a function, i.e. tell the compiler the name of our function, what parameters it has, and the type of information it returns. void draw'line)void*; void tells us that the function does not return any data to main draw?line is the name of the function which we use in main to call the function 2void3 tells us that no data is passed to the function 2.1.2 Defining a function: The :8<-T*6< 9#:*<*T*6< describes exactly what action the function is to ta$e. The first line of the function definition is similar in format to the function prototype but it does <6T re'uire a semicolon. void draw'line)void* + cout 33 4555555555555555555555554 33 endl; 2.1. !alling a "unction The function is called using the function name, for example5 draw?line23B will tell the program flow to go to the draw?line23 function. The draw?line23 code is then executed and on completion the program execution returns to the <#CT line of the calling function. Thus the order of execution of the program would be as follows5 1) cout << "This program calls a function to draw a single line\n\n"; 2) cout << "***********************\n" << endl; ) cout << "!nd of program" << endl; ") return #;

6- 7 :unctions that do not return a value As mentioned previously functions sometimes do not need to return values. *n these cases the function type is the data type void and the function call can not be used in an e'uation. These type of functions are usually concerned with data output, where data is being sent out to a peripheral such as the screen or to a file. Let us see an example of this. 88 #unction $rototype void display,um) int sum*; : 88 "wo calls to the function display,um) sum *; : 88 #unction Definitions void display,um) int sum* +!!!! 88 display result cout 99 4"he sum is 4 99 sum 99endl; The function call to display(um proceeds as follows. %. when display(um is called, memory locations are assigned to5 the formal parameter sum. There are no local variable re'uire for this function. . <ext the values of the actual parameters are copied into their corresponding formal parameters. *n the case, the value of the actual parameter sum is copied to the formal parameter sum. <ote5 the name of the formal and actual parameters do not have to be the same. +. The statements of the function body are executed. *n this example, the value of sum is displayed to the user.

Vous aimerez peut-être aussi