Procedures

Home Up Contents Job Links Download

Search this site(enter key word):  

Helping the mankind

Error Handling

 

 

Arrays

What if you want to process large set of numbers, or 1000s of employees records.   For instance, consider you need to sort 10 numbers in ascending order.  Will you have 10 different variables of 'int' data types?  No.  Arrays are the easiest way of declaring such variables.

int a[10] ;

where square brackets ( '[', ']', some cases it is '(' and ')' are used ) indicate, it is an array variable. 10 is the number 'int' values you want to store. Each value is called an 'element' and is accessed by its 'subscript' or 'index'. So,

int a[10] ;
int i = 0 ;
for ( i = 0 ; i < 10 ; i++ )
    a[i] = i + 1 ;

In some languages, the starting 'index' is '0' and others it is '1'.  In the above example, the starting element is '0'.

Suppose there is another set of 10 'int'

int b[10] ;

The following 'assignment' in invalid in most of the languages.

a = b ;

In stead, you have to use a 'loop' to assign each array element to another array element.

int a[10] ;
int i = 0 ;
for ( i = 0 ; i < 10 ; i++ )
    a[i] = i + 1 ;
 
for ( i = 0 ; i < 10 ; i++ )
    b[i] = a[i] ;

Procedures

As the size of the application increases, some tasks are repeated in different places resulting in duplicates of codes.  Which also leads to errors.  If the complete program is written sequentially, it is difficult to maintain, test, and debug the program easily.  It is more and more complicated when many people are involved in the development process.

Functional decomposition (modular) is a method to divide a large program into small maintainable and reusable codes.  It is implemented through using 'procedures' and 'functions'.

Procedures are a subset of a large program which perform specific tasks by a call to that procedure from the main program or from another procedure or function.

You can use 'loops', 'conditional statements' in your procedures and functions.   You can pass 'arguments' ( also called 'parameters') of any data type to your procedures.

Syntax for declaring 'procedures' are different from languages to languages.   Example,

main()
{
    int iBase = 2 ;
    int iPower = 8 ;
    Find_Power( int base, int power ) ;         //procedure declaration
    //Call procedure, also called invoking procedure
    Find_Power( iBase, iPower ) ;
}
 
//Procedure Definition
 
Find_Power( int base, int power )
{
    int pwr_value = 1 ;
    int i  ;
    for ( i = 1 ; i <= power ; i++ )
        pwr_value = pwr_value * power  ;
    print( "Power = " + pwr_value ) ;
}

The procedure has to be declared first, before it is called from a program. You have to declare the datatype of each argument to be passed to the procedure.  When the procedure is called the value of 'iBase' is assigned to 'base' and the same way 'iPower' is assigned to 'power'. Arguments 'base' and 'power' are local variables within the procedure.  That means, any changes to 'base' and 'power' will not affect the values stored in 'iBase' and 'iPower'.

Naming Convention:

Prefix the datatype when declaring variables:

e.g.,

int iBase ;
char cMarried ;
string sName ;
long lSalary ;
date dtDOB ;

Most of the languages support long names for variables, procedures, functions.

Long names can be written like,

Find_Power( ... ) ;
FindPower( ... ) ;

Whatever the 'convention' you follow, stick to that throught out your life time.

Functions

Difference between 'procedures' and 'functions' is that the 'function' can return a value to the called program.

main()
{
    int iBase = 2 ;
    int iPower = 8 ;
    int iRetValue = 0 ;
    int Find_Power( int base, int power ) ;         //function declaration
    //Call procedure, also called invoking procedure
    iRetValue = Find_Power( iBase, iPower ) ;
}
 
//Function Definition
 
int Find_Power( int base, int power )
{
    int pwr_value = 1 ;
    int i  ;
    for ( i = 1 ; i <= power ; i++ )
        pwr_value = pwr_value * power  ;
    return( pwr_value ) ;
}

Functions can return only one value to the called program.  First part of the function declaration says what is the datatype of the return value.

There are two methods to pass a value to a function/procedure. 

Passing by value

Above examples use this method to pass values. In this any changes to the arguments will not affect the variables in the called program.  But it takes additional memory, which is same as the variables in the called program.  It may use large memory for arrays.

Passing by reference

Any changes to the arguments will affect the corresponding variables in the called program.  Because it references (pointing to the variable in the called program), it reduces the amount of memory used.  This allows you to return (indirectly) more than one value to the called program.

Constant variables

Sometimes you know in advance that a variable will have a constant value. e.g.,

const float pi=3.14 ;        //const key word

int iMaxSize = 10 ;             // used for array size, counters.

You can use 'const' in argument passing also.  e.g.,

Find_Power( const int base, const int power ) ;

In case of 'passing by reference variables', it prevents the actual value to be changed, but still uses the power of 'less memory'.

Libraries

Library files store set of related procedures, functions and variables which can be shared with other application.  It allows you to maintain the codes easily.   Also gives independence to programmers to create their own libraries that can be integrated with the main application.

Project/Application

Any software development results in 'project' or 'application' development.  Some languages uses 'project' to refer to an application.  'Project' and 'application' are synonymous with each other.  A project file will reference to all other library files used in the development of an application.

For example in a Visual Basic application, a project file refers to:

Form files,
Module files,
Class files,
User Controls files,
Property pages

Source Control

Version control is important as changes are made to your application by more than one people every day.  Source control system allows you share an application with many users maintain the 'changes' history.  You can rollback to previous version of code from the 'history'.

 

Home ] Up ] Error Handling ]

Sign My Guestbook Guestbook by GuestWorld Sign My Guestbook

Join EITC's mailing list!
Enter your email address below,
then click the 'Join List' button:
Powered by ListBot

Send mail to arungomes@eircom.net with questions or comments about this web site.
Copyright © 1999 Empire Cyber IT Consultancy
Last modified: January 26, 2000