PSC 2020 Lecture Notes Unit 3
PSC 2020 Lecture Notes Unit 3
UNIT-3:
Arrays: Declaration, accessing array elements, Storing values, Operations on arrays, Multi-
dimensional arrays.
Functions: Introduction, Us i n g Functions, Function declaration, Function definition and Function
call, Parameter passing, passing arrays to functions, Recursion, Storage classes.
Learning Outcomes: The students will be able to
• Writing Structured programs using Functions (L5).
• Apply arrays concepts on real time applications (L6).
ARRAY:
Array is a data structure which can store fixed-size sequential collection of elements of the same
type.
Purpose of Array:
Instead of declaring individual variables, such as number0, number1, ..., and number99, you
declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to
represent individual variables. A specific element in an array is accessed by an index.
The lowest indexis always zero which corresponds to the first element/location and the highest
index(maximum size -1) refers to the last element/location.
Declaring Arrays
To declare an array in C, a programmer specifies the type of the elements and the number of elements
required by an array as follows:
Data type arrayName[arraySize];
This is called a single-dimensional array. The arraySize must be an integer constant greater than zero
and type can be any valid C data type. For example, to declare a 10-element array called balance of
type double, use this statement:
double balance[10];
Now balance is avariable array which is sufficient to hold upto 10 double numbers.
Initializing Arrays
You can initialize array in C either one by one or using a single statement as follows:
double balance[5]={1000.0,2.0,3.4,7.0,50.0};
The number of values between braces { } can not be larger than the number of elements that we declare
for the array between square brackets [ ].
The above statement assigns element number 5th in the array with a value of 50.0. All arrays have 0 as
the index of their first element which is also called base index and last index of an array will be total
size of the array minus 1. Following is the pictorial representation of the same array we discussed above:
• Once we know the length of an array, we can input values using a for loop:
array name : scores[9], arrayLength = 9;
scores[5] = 42;
scores[3] = 5 + 13;
scores[8] = x + y;
scores[0] = pow(7, 2);
“Copying” Arrays
• We cannot directly copy one array to another, even if they have the same length and share the
same data type.
• Instead, we can use a for loop to copy values:
for(m = 0; m < 25; m++)
{
array2[m] = array1[m];
}//end for
To swap (or exchange) values, we must use a temporary variable. A common novice’s
mistake is to try to assign elements to one another:
/*The following is a logic error*/
numbers[3] = numbers[1];
numbers[1] = numbers[3];
Initializing Arrays
You can initialize array in C either one by one or using a single statement as follows:
int balance[2][5]={10,1,2,3,4,50,6,7,80,99};
The number of values between braces { } can not be larger than the number of elements that we declare
for the array between square brackets [ ].
• Once we know the length of an array, we can input values using a for loop:
array name : matrix[3][3], arrayLength = 9;
For(i=0;i<3;i++)
for (j = 0; j <3; j++)
{
scanf(“%d”, &matrix[i][j]);
}//end for
“Copying” Arrays
• We cannot directly copy one array to another, even if they have the same length and share the
same data type.
• Instead, we can use a for loop to copy values:
• for(i=0;i<3;i++)
• for(j = 0; j < 3; j++)
{
array2[i][j] = array1[i][j];
}//end for
Programming language C internally converts an array as pointer. So, whenever array name is
specified in a printf statement as shown below:
printf(“%d”,a[i]);
It will be interpreted as
printf(“%d”,*(a+i));
scanf(“%d”,&a[i]);
It will be interpreted as
scanf(“%d”,a+i);
Note: Just referring the name of the array gives the starting address of the array, * followed by name
of the array gives the value at that address.
Multidimensional Arrays:
Arrays with more than two dimension are called multidimensional arrays. Although
humans cannot easily visualize objects with more than three dimensions, representing
multidimensional arrays present no problem to Computers.
FUNCTIONS
System defined functions: These are functions that are supported along with the programming
language. The block of instructions of these functions and the task to be performed is predefined. For
e.g. clrscr(), printf(), scanf(), pow(), sqrt() etc.
User defined functions: These are functions that are not supported along with the programming
language. The block of instructions of these functions and the task to be performed is defined by the
programmer.
Important Terminologies:
Function call is the reference made in the calling function by specifying the name of the
function along with arguments if any exists and should be terminated with semicolon.
Calling function is the function in which we write function call statements.
Called function is the definition of function call
for e.g.
Function Header:
The function header comprises of three parts namely
1. Return type
2. Function name
3. arguments
Return type: Specifies the type of data (primitive/user-defined) that the function will
return.
Function Name: Takes a valid name to identify the function and this name should
follow the rules for naming a user defined identifier.
Arguments: the values/variables that we receive from function call
Function body:
The function body comprises of the following parts enclosed in flower
Braces:
1. Local variable declaration
2. Executable statements
3. Return statement
Local variable declaration: Variables that are needed in that function are declared
for its type.
Executable Statements: These are statements that are needed in the function to
assign a value or to read a value or to print the value or to perform some
computations.
Return statement: Every function body ends with return statement. This statement is
used to return a value to the calling function. If the function does not return any value
then the return statement is written as
Return; or return 0;
If the function returns any value then the return statement is written as
Return value; or return expression;
The function definition can be done either above the main function or after the main function.
Example of function definition:
float mul(x,y)
float x,y;
{
float result;
result=x*y;
return result;
}
In the above example in the function header the return type is float and the name of the function
In the function body, result is a variable that is needed in that function and is declared locally
for its type. Result=x*y is the executable statement and the return statement returns the value available
in the variable result.
Function call: The function definition is invoked by referring the function name along with arguments
if any exists. The arguments in the function call, if exists will take data or variables declared for its
type matching to the function declaration and definition.
The arguments in the function are called as actual arguments.
The function call should be made always within another function.
The general format for function call which returns value is given below:
Variable=functionname(arguments);
Or
printf(“format string”,functionname(arguments));
Functionname(arguments);
main()
{
mul(); /* function call */
}
/* complete program showing the usage of function declaration, function call and definition */
#include <stdio.h>
main()
{
float y;
y=mul(23.4,12.5); /* function call */
printf(“product of two numbers is %f\n”,y);
}
/* function definition */
float mul(x,y)
float x,y; /* argument declaration */
{
float result; /* local variable declaration */
result=x*y; /* executable statement */
return result; /* returns the computed value in result to the calling function */
}
Functions Arguments:
Arguments and parameters refer to the variables or expressions passed from the function.
Each and every argument is separated with comma.
Actual Argument names and Formal Argument names can be same or different.
Actual Argument names are recognized only in that function.
Formal Argument names are recognized only in the definition of the function.
Example:
Sample(x1,y1)
Main() Int x1,y1;
{ {
intx,y; Formal Arguments
Actual Arguments
Sample(x,y);
} }
/* function call with arguments and no /* function call with arguments and no
return value */ return value */
swap(a,b); swap(&a,&b);
/* function definition with arguments and no /* function definition with arguments and
return value */ no return value */
Automatic variables, or variables with local lifetimes, are allocated new storage each time
execution control passes to the block in which they are defined. When execution returns, the
variables no longer have meaningful values.
Storage specifiers are:
auto
register
static
extern
S. Storage Storage Initial / default Scope Life
No. Specifier place value
1 auto CPU Garbage value local Within the function only.
Memory
2 extern CPU Zero Global Till the end of the main program.
memory Variable definition might be
anywhere in the C program
3 static CPU Zero local Retains the value of the variable
memory between different function calls.
4 register Register Garbage value local Within the function
memory
Important Questions:
Assignment Questions.