Page 82 - DLIS108_INFORMATION_AND_COMMUNICATION_TECHNOLOGY_APPLICATIONS
P. 82
VED1
e\L-lovely-eng\comm8-1.pmd IInd 16-9-11 IIIrd 22-12-11 IVth 4-1-12
Unit 8: Programming Language: Types and Functions
• Second Solution: Forbid recursion, but allow iteration by other means. VAL does this by al-
lowing a very limited form of reassignment, which creates new, tagged variables Notes
• All variables in Lucid are streams, which make the idea of new values without reassignment
come natural.
Write a Short note about Imperative language.
8.8 Functions
All programming languages have a set of constructs which allow the programmer to break up their
code into smaller, more manageable, chunks which can be called by name and which may or may
not return a value.
The purpose of these blocks is to decrease the complexity of a piece of code, and allow the programmer
to reuse specific functions, rather than constantly repeating the same block of code performing a
specific operation.
For example, a language that has no built-in command for displaying text will need to provide a
function to interface with the operating system to display a text string. If there was no function for
this, the programmer would need to copy the same code over and over each time they wanted to
print something on the screen.
If the operating system changed in some way, they would have to change many pieces of code,
unless a function was used, in which case they could just update the single block that is called each
time output is required.
Functions are used extensively in computer languages and spreadsheets. Recall that a function
takes an input, does some calculations on the input, and then gives back a result. In computer
programming they are a very similar idea, with a few changes to naming and properties.
A function in a programming language is a program fragment that ‘knows’ how to perform a defined
task. For example a function may be written that finds the average of three supplied numbers. Once
written, this function may be used many times without having to rewrite it over and over.
Example-the Function avg
function avg(a,b,c)
{ var result = (a+b+c)/3;
return result;
}
The above, written in Javascript, performs the average function. On the first line, the name of the
function is ‘avg’, It expects three inputs called a,b and c. In computer programming these are called
parameters; they stand for the three values sent when the function is used. The function has it’s
own private variable called result which is calculated from the parameters and then the function
‘returns’ the result;
Using the Function
In computer programming the act of using the function is “calling the function”. In the program
below there are two “calls” to the function. In each case, three particular values are sent as parameters
and the result will be the average of the three.
LOVELY PROFESSIONAL UNIVERSITY 77