Programming Functions in C part two

Previously we had discussed regarding the usage functions in C programming and this is a continuation for it.

Use of Functions in C programming

Writing functions avoids rewriting the same code over and over.

Explanation : Suppose we have a section of code in your program that calculates area of a triangle. If later in the program we want to calculate the area of a different triangle, we nee d not write the entire code once again and we would prefer to jump to a ‘section of code’ that calculates area and then jump back to the place from where you left off. This section of code is nothing but a function.

Using functions it becomes easier to write programs and keep track of what they are doing. If the operation of a program can be divided into separate activities, and each activity placed in a different function, then each could be written and checked more or less independently. Separating the code into modular functions also makes the program easier to design and understand.

We have to break a program into small units and write functions for each of isolated sub divisions. We can even write functions that are called only once and these functions perform some logically isolated task.

How to Pass Values between Functions ?

The mechanism used to convey information to the function is the ‘argument’. We use the arguments in the printf( ) and scanf( ) functions; the format string and the list of variables used inside the parentheses in these functions are arguments. The arguments are sometimes also called ‘parameters’.

Sending and receiving values between functions Example

Consider the following program where, in main( ) we receive the values of a, b and c through the keyboard and then output the sum of a, b and c.

The calculation of sum is done in a different function called calsum( ). If sum is to be calculated in calsum( ) and values of a, b and c are received in main( ), then we must pass on these values to calsum( ), and once calsum( ) calculates the sum we must return it from calsum( ) back to main( ).

main( )

{

int a, b, c, sum ;

printf ( "\nEnter any three numbers " ) ;

scanf ( "%d %d %d", &a, &b, &c ) ;

sum = calsum ( a, b, c ) ;

printf ( "\nSum = %d", sum ) ;

}

calsum ( x, y, z )

int x, y, z ;

{

int d ;

d = x + y + z ;

return ( d ) ;

}


And here is the output... Enter any three numbers 10 20 30 Sum = 60

Explanation

1.In this program, from the function main( ) the values of a, b and c are passed on to the function calsum( ), by making a call to the function calsum( ) and mentioning a, b and c in the parentheses: sum = calsum ( a, b, c ) ; In the calsum( ) function these values get collected in three variables x, y and z:

calsum ( x, y, z )

int x, y, z ;

2.The variables a, b and c are called ‘actual arguments’, whereas the variables x, y and z are called ‘formal arguments’. Any number of arguments can be passed to a function being called. The type, order and number of the actual and formal arguments must always be same.

3.The return statement serves two purposes:
1. On executing the return statement it immediately transfers the control back to the calling program.
2. It returns the value present in the parentheses after return, to th3e calling program.

4.There is no restriction on the number of return statements that may be present in a function. Also, the return statement need not always be present at the end of the called function.

5.Whenever the control returns from a function some value is definitely returned. If a meaningful value is returned then it should be accepted in the calling program by equating the called function to some variable.

6.If we want that a called function should not return any value, we must mention so by using the keyword void .

void display( )
{
printf ( "\nHeads I win..." ) ;
printf ( "\nTails you lose" ) ;

7.A function can return only one value at a time.

OTHER PROGRAMMING COURSES:
INTRODUCTION TO C PROGRAMMING

Programming with C an introduction part two

Data types for C programming


C PROGRAMMING CHARACTER SET

CONSTANTS IN C PROGRAMMING

PROGRAMMING C VARIABLES

C PROGRAM INSTRUCTIONS

COMPILATION AND EXECUTION OF C PROGRAM

C PROGRAMMING RULES PART ONE

C PROGRAMMING RULES PART TWO

COMPILATION AND EXECUTION OF C PROGRAM

INSTRUCTIONS TO WRITE C PROGRAM

ARITHMETIC INSTRUCTIONS TO WRITE C PROGRAM

CONVERSION OF CONSTANTS IN C PROGRAM

PRIORITY OF AR THEMATIC OPERATIONS IN C

OPERATORS ASSOCIATIVITY IN C

IF STATEMENT

MULTIPLE STATEMENTS IN IF

IF AND ELSE

NESTED IF AND ELSE


BREAK

CONTINUE AND DO WHILE IN C LANGUAGE

SWITCH IN C PROGRAMMING

FUNCTIONS IN C PROGRAMMING


Functions and usage in C part two

Coding in C functions


Software Testing Process Overview

The testing and development processes are in a feedback loop. The testing process feeds identified failures back into the development process.

Failure reports provide a set of symptoms that a developer uses to identify the exact location of a fault or error. The development process feeds new and revised designs and implementations into the testing process.

Testing of development products will help identify defective test cases when testers determine that "failures" result from problems with test cases themselves or the drivers that execute them, and not the software under test.

The form and content of development products affect the testing process. When developers select methods and tools, they establish constraints on the testing process. The testing perspective must be considered, preferably by the presence of professional testers, when development methods and tools are selected.

The form and quality of a requirements specification also affects the process. Product requirements comprise the source of test cases in system and acceptance testing. System testers should participate in the gathering and validation of the requirements in order to have a sufficient understanding of them to assess risks and testability.

Three-step approach for each type of testing performed on a project.

  1. Analysis:The product to be tested is examined to identify any special features that must receive particular attention and to determine the test cases that should be constructed. We will present a number of analysis techniques.

  2. Construction: In this phase the artifacts that are needed for testing are created. The test cases identified during analysis are translated into programming languages and scripting languages, or they are entered in a tool-specific language.

  3. Execution and Evaluation: This is the most visible and often the only recognized part of the test effort; however, it is also typically the quickest part of the test effort. The test cases that were identified during analysis and then constructed are executed. The results are examined to determine whether the software passed the test suite or failed it.

Test Cases and Test Suites

The basic component of testing is a test case. Test case is a pair (input, expected result), in which input is a description of an input to the software under test and expected result is a description of the output that the software should exhibit for the associated input.

Inputs and expected results are not necessarily simple data values, such as strings or integer values, but they can be complex.

Inputs often incorporate system state information as well as user commands and data values to be processed. Expected result includes not only perceivable things, such as printed reports, audible sounds, or changes in a display screen, but changes to the software system itself.

A test case execution is a running of the software that provides the inputs specified in the test case and observes the results and compares them to those specified by the test case. If the actual result varies from the expected result, then a failure has been detected and we say the software under test "fails the test case."

If the actual result is the expected result for a test case, then we say the software "passes the test case."

Test cases are organized into a test suite. Most test suites have some sort of organization based on the kinds of test cases. If software passes all the test cases in a test suite, then we say that the software "passes the test suite."


Software testing other interesting topics:

SOFTWARE TESTING AND EFFECTING FACTORS
SOFTWARE QUALITY ASSURANCE AND CONTROL

SOFTWARE QUALITY AND COST ASPECT

STABLE PROCESS OF SOFTWARE TESTING

STABLE PROCESS OF SOFTWARE TESTING PART TWO


DEFECTS IN SOFTWARE TESTING

REDUCTION OF DEFECTS IN SOFTWARE TESTING

SOFTWARE TESTING AND EFFECTING FACTORS

SCOPE OF SOFTWARE TESTING

TESTING LIFE CYCLE PART ONE

TESTING LIFE CYCLE PART TWO

TESTING LIFE CYCLE PART THREE

SOFTWARE TESTING AND CONSTRAINTS WITH IN IT



Java Arrays Programming for beginners

Java arrays are objects.

In arrays we can store multiple variables of same type that are referred by a common name.
An array is always an object,even if the array is declared to hold object references or primitives .

In array variables are placed in indexed order.These variables are called elements of an array. So in java arrays we can access the elements or values through the index.Every element in an array is just a variable.

Now we have to know how to declare the array ,creation of array and accessing the elements from array.

Declaration of arrays are in two forms:

form1: type arrayname[]; Type followed by name of the array,followed by Square bracket .

ex: int abc[];

form2: type[] arrayname; Type followed by bracket,followed by name of the array.

ex: float[] abc;

form2 is the recommended way,because name is clearly separated from the type.

At the time of declaration, we should not enter the size of an array.violation leads to compile time exception.

for example int[5] a; it gives compile time exception.

After declaration of an array ,we can't put any other data type except the declared array type.
but implicitly promoted data types we can use, means less than the declared array type.for
example int[] x, the allowed data types are byte,short,char,int.

After creation of java arrays,once you fix the size of an array,we can't change the size of an array.But you can change an individual array element. If it is need to change the array size frequently while the program is running then we should go for another concept, known as Arraylist.

In java arrays are work differently than they do in c,c++.

creation of java arrays:

After declaring an array,we need to put it into memory.arrays must be declared in the memory before they are used.

Java arrays are creating only using new keyword .

array type[] array name=new array type[size];

Ex: int[] x=new int[50];

Array type is int , the allowed data types are byte/short/char/int.
This statement sets up an array that can hold 50 integers means 0 to 49 ,not 1 to 50 .

At the time of creation we must specify the size, otherwise compile time exception occurs.Even
we can give zero number also.but we can't specify the size with negative number we will get Run time Exception(RTE).we have to use integer to specify the size violation leads to compile time exception.

for example:

int[] x = new int[55]; It is OK

int[] x = new int[0]; It is OK

int[] x = new int[]; it gives compile time exception.

int[] x = new int[-5]; RTE, Negative Array Size Exception

int[] x = new int[9.5]; CTE,Possible loss of precission

we can write declaration and creation separately like this,

int[] x;

x=new int[8];

If you want to find the number of elements of an array, use arrayName.length.

For example :

for (int i = 0; i < style="font-weight: bold; color: rgb(51, 51, 255);">

Initialization of Arrays:

Once an array is created,all the elements will assign with default values based on declared data type.

For example : for int default value is zero.
for string default value is null.
for Boolean default value is false.

java arrays are created starting with a subscript of zero and ends with a value one lessthan the size declared.

In java arrays the declaration,creation and initialization in a single line is possible.
for this syntax, specify the declaration and size,later we can give the values at run time.

int a[];

a=new int[5];

a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;

Instead of above declaration,creation and initialization we can write like this,

int[] a={10,20,30,40,50};

But for this syntax, at the time of declaration we must know the array size and initialization values,can't give the values at run time.

It is possible to convert an array object to another array,but the array types must same.

int[] x={10,20,30};

int[] y;

y=x;

Accessing array elements:

int[] a=new int[5];

a[5]=60 it leads RTE, "Array Index Out Of Bound Exception".

a[4.0]=50;

If you are accessing array element with unmatched data type then it gives compile time exception, "Incorrect Syntax".

a[-4]=50 ; it leads RTE,"Array Index Out Of Bound Exception".

a[2.5] =50; it leads CTE, "Possible Loss of precision"

Either Float/Double/Long data type indexes should not use to accessing the array elements,violation leads to compile time exception.

Array element assignment is possible , when the class object is the subtype of element type.That means if an array is declared as object reference array ,we are allowed to assign the objects of either declared type or it's child classes.


ex 1: Number[] a = new Integer[5]; // Length 10, element type Integer

Double f = new Double(3.14); // Type Double, class Double

Integer i = new Integer(115); // Integer, class Integer

Number n = i; // Type Number, class Integer

a[0] = i; // OK, Integer is subtype of Integer

a[1] = n; // OK, Integer is subtype of Integer

a[2] = f; // No, Double not subtype of Integer

The above program successfully compile,but at run time a[2] =f throws "Array Store Exception", because the class of the object bound to f is not a subtype of a's element type (that is, Integer).

ex 2: String[] s = new String[5];

s[0] = "A"; it is OK

s[1]= new Integer[12]; CTE

Integer is not a child class of String,but expected thing is String or child class of String class .

ex 3: Object ob = new Object[8];

ob[0] = "X";

ob[1]= new Thread();

ob[2] = new Exception();

ob[3] = new Number();

ob[4] = new Integer(15);

the above program compile and run successfully, because all are subclasses of declared array type.
How do we Assignment of Array variables :

In general, we are allowed to place int variable in the place of double variable, because implicitly int will be promoted to double.but it is not possible to place int array in the place of double array. Like this , a character element can be assigned in the place of int element.But the char array can't be assigned in the place of int array.It gives compile time exception.


example : int[] a= {1,3,6,8};

char[] c={'a','b','c','d','e'};

int[] b = c; CTE, Incompatible Type


In the case of Object arrays ,we are allowed to place child class arrays in the place of parent class array.

example 1: wheels[] w= new wheels[8];
CLASSES AND OBJECTS IN JAVA

JAVA PROGRAMMING ENVIRONMENT

JAVA EXECUTION

JAVA CONSTRUCTORS

FIELD DECLARATIONS IN CLASS OF JAVA

EXCEPTIONS IN JAVA

EXCEPTIONS IN JAVA PART TWO

VARIABLE PARAMETERS IN JAVA

COMMENTS TYPES AND VARIABLES

JAVA JDBC AND ODBC

INTERFACE IN JAVA

OPERATORS IN JAVA