Arrays and Functions
Arrays and Functions
ARRAYS
Arrays are data structures which hold multiple variables of the same data type. An array is an
identifier to store a set of data with common name. Note that a variable can store only a
single data. Arrays may be one dimensional or multi dimensional.
Arrays, like other variables in C, must be declared before they can be used.
int names[4];
names[0] = 101;
names[1] = 232;
names[2] = 231;
names[3] = 0;
We created an array called names, which has space for four integer variables.
Arrays have the following syntax, using square brackets to access each indexed value (called
an element).
x[i]
so that x[5]refers to the sixth element in an array called x. In C, array elements start with 0.
Assigning values to array elements is done by,
x[10] = g;
and assigning array elements to a variable is done by,
g = x[10];
Array Initialization
Although it is not possible to assign to all elements of an array at once using an assignment
expression, it is possible to initializesome or all elements of an array when the array is
defined. The syntax looks like this:
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
The list of values, enclosed in braces {}, separated by commas, provides the initial values for
successive elements of the array.
MULTI-DIMENSIONAL ARRAYS
Multi-dimensional arrays are defined in the same manner as one dimensional arrays except
that a separate pair of square brackets is required to each subscript.
Example: float matrix[20][20] (two dimensional)
Intx[10][10][5] (3-dimensional)
Initiating a two dimensional array we do as int x[3][4]={1,2,3,4,5,6,7,8,9,10,11,12}
Or
/* Before accepting the Elements Check if no of rows and columns of both matrices is
equal */
if (row1 != row2 || col1 != col2) {
printf("\nOrder of two matrices is not same ");
exit(0);
}
return (0);
}
Output:
Enter the number of Rows of Mat1 : 3
Enter the number of Columns of Mat1 : 3
FUNCTIONS
Functions are programs.There are two types of functions- library functions and programmer
written functions. We arefamiliarised with library functions and how they are accessed in a C
program.
The advantage of function programs are many
1) A large program can be broken into a number of smaller modules.
2) If a set of instruction is frequently used in program and written as function program, it can
be used in any program as library function.
Defining a function.
Generally a function is an independent program that carries out some specific well defined
task. It is written after or before the main function. A function has two components-definition
of the function and body of the function.
Generally it looks like
datatype function name(list of arguments with type)
{
Function body
return;
}
Return Type or data type: A function may return a value. The return_type is the data type
of the value the function returns. Some functions perform the desired operations without
returning a value. In this case, the return_type is the keyword void.
Function Name: This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value
to the parameter. This value is referred to as actual parameter or argument. The parameter list
refers to the type, order, and number of the parameters of a function. Parameters are optional;
that is, a function may contain no parameters.
Function Body: The function body contains a collection of statements that define what the
function does.
If the function does not return any value to the calling point (where the function is accessed)
.The syntax looks like
function name(list of arguments with type)
{
statements
return;
Accessing a function
A function is accessed in the program (known as calling program)by specifying its name with
optional list of argumentsenclosed in parenthesis. If arguments are not required then only
with empty parenthesis.
The arguments should be of the same data type defined in the function definition.
Example:
1) inta,b,y;
y=maximum(a,b);
2) char name[50] ;
While calling a function, there are two ways that arguments can be passed to a function:
#include <stdio.h>
int main ()
{
/* local variable declaration */
int a, b;
int c;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
Global Variables
Global variables are defined outside of a function, usually on top of the program. The global
variables will hold their value throughout the lifetime of your program and they can be
accessed inside any of the functions defined for the program.
A global variable can be accessed by any function. That is, a global variable is available for
use throughout your entire program after its declaration. Following is the example using
global and local variables:
#include <stdio.h>
/* global variable declaration */
int g;
int main ()
{
/* local variable declaration */
int a, b;
/* actual initialization */
a = 10;
b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0;
}
A program can have same name for local and global variables but value of local variable
inside a function will take preference. Following is an example:
#include <stdio.h>
/* global variable declaration */
int g = 20;
int main ()
{
/* local variable declaration */
When the above code is compiled and executed, it produces the following result:
value of g = 10
Recursion
It is the process of calling a function by itself ,until some specified condition is satisfied. It is
used for repetitive computation ( like finding factorial of a number) in which each action is
stated in term of previous result
Example:
#include<stdio.h>
longint factorial(int n);
main( )
{
int n;
longint m;
scanf(“%d”,&n);
m=factorial(n);
printf(“\n factorial is : %d”, m);
}
longint factorial(int n)
{
if (n<=1)
return(1);
else
return(n*factorial(n-1));
}
In the program when n is passed the function, it repeatedly executes calling the
same function for n, n-1, n-2,………………..1.