Dani 4,084 The Queen of DaniWeb Administrator Featured Poster Premium Member

Functions
Locality encompasses the idea that it often takes many, many programmers to come up with a single software program. Virtually all programming languages are constructed in such a way that these various programmers don't even need to interact with eachother. This is possible because of locality.

Programs are broken down into what are known as functions. Each function may be written by a different person. By separating local and global variables, functions can theoretically stand on their own. Local variables are those variables which are created inside a function and used inside that function, alone. Global variables are those which can be seen throughout multiple functions.

Passing Parameters
Parameters are variables which are passed into functions from other functions. Once they are passed in, the function can use them in the same way as they use other local variables. This ability adds to the use of locality. For example, suppose someone in California were to write a program to calculate loan interest when the amount for the loan and the interest rate are passed in as parameters. Someone in New York can make use of this function by passing in the two parameters needed. Someone in New Jersey can make use of this function, as well, by passing in his or her own two parameters. Not only do the three programmers never need to meet. In addition, the programmers from N.Y. and N.J. don't need to know exactly how the function was written.

The function performing the calculations might contains 500 local variables. It might be 4,000 lines of confusing code. But the N.Y. and N.J. programmers do not need to know that, nor are they interested in it. All that matters to them are the preconditions and postconditions for the function in question.

Pre- and Postconditions
Because a function can be run multiple times in many different ways, preconditions and postconditions must exist to bring some form of order to programming. Preconditions are what must be true before a function can be run. When a programmer writes a function, they can assume that these preconditions are automatically true and don't have to worry about them. For example, suppose a function is to be written to withdraw money from a back account. A precondition states that the account holder has enough money to withdraw the set amount. Therefore, no test is required inside the function to check to make sure this amount can be withdrawn. The programmer can just write the code needed to perform the actual withdrawl.

Postconditions state what state the function will leave the computer after it is executed. Postconditions include how global variables are altered, what is printed on the screen, or any other information someone planning on calling the function would need to know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.