Functions (Modules) : Introduction To Programming I Chapter - 4
Functions (Modules) : Introduction To Programming I Chapter - 4
E.C
CHAPTER -4
Functions (Modules)
Function is a subprogram that can act on data return value. a function is always
called by another function(exception of main function) which is called by operator
system. The called function must often able to receive data from calling function.
This data is passed to called function through its parentheses.
There are various type of function such as void functions and a function that return
a value
Using function in a program requires declaring and defining the function. The
declaration is called the function prototype. It tells the compiler Name, return type
and parameters of the function. Function definition tells how the function works.
Syntax
function prototype
function call
function Definition
Example
int sample( );
void main( )
int y=sample( );
.
}
Example
void main( )
void main ( )
cout<<"Input 1: ";
cin>>num1;
cin>>num2;
By Deres G. RVU Page 2
Introduction to programming I 2007
E.C
int add;
add=j+k;
return(add)
syntax
Type of argument contain data types of argument that must be passed to the
function and optional names of arguments.
The calling statement should not include type name in the argument list.
The definition of a function consists of function header and its body. the header is
exactly like a function prototype.
The body of a function is a set of statement enclosed in Brasses. every function has
a return type. The function that has not return value its return type will be void.
#include<iostream.h>
void main( )
int nolines;
cin>>nolines;
while(nolines<0 || nolines>20
cin>>nolines;
cout<<endl<<endl;
noblanks=30;
noblanks- -;
By Deres G. RVU Page 4
Introduction to programming I 2007
E.C
int count
cout<<" ";
cout<<" * ";
cout<<endl;
Chapter 5
Arrays
What is array?
Array is a structured with homogenous elements of data type. It is a collection of
data storage locations each of which holds the same type of data. It is storage
location is called an element of the array.
The program declares an array by writing data type followed by array name and
subscript. The subscript (index) is number of elements in array sorrowended by
square brackets[ ].
One dimensional array is column data items. all of them have the same data type
that are collectively referred by the same name.
Example
float result[50]
The program access each of the array element by specifying array name followed
by index enclosed by square brackets [ ]. Array elements are count from zero to n-1
when n is the size of the array.
Example
Example
#include<iostream.h>
void main( )
float result[50];
cin>>result[i];
Home work
Write a simple code that accepts results of n students and displays number of
students who scored less than average .
Multi-Definitional array
Arrays can have any number of diminutions. Each dimensions represented by index
in the array.
Example
int A[10][20]
initializing array
Example
int A[4]={1,2,3,4};
int A[ ]={1,2,3,4,5,6};
float A[2][3]={{1,2,3}{4,5,6}};
Example.
#include<iostream.h>
void main( )
{
int row, col, i, j ;
folat table[100][100];
cout<<"Enter number of rows : ";
cin>>row;
cout<<" Enter number columns :";
cin>>col;
cout<<" Now enter "<<row<<" X "<<col<<"real numbers\n";
for (i=0; i<row; i++)
for(j=0; j<col; j++)
cin>>table[i][j];
cout<<" Your table of real numbers is :\n\n";
A. Write a program that accepts n real numbers and displays max and min from
the list.
B. Write a program that returns the sum of two Matrix
Example
/* The following program finds average of numbers that are stored in array*/
#include<iostream.h>
void main( )
float av, sum=0;
int num[10];
cout num[10]; i;
cout<<"Enter the numbers \n ";
for (i=0; i<10; i++)
{
cout<<"Enter number "<<i+1 <<" : ";
cin>>num[i];
}
for (i=0; i<10; i++)
sum+=num[i];
By Deres G. RVU Page 9
Introduction to programming I 2007
E.C
av=sum/10;
cout<<"\n\n The average is "<<av;
}
Example
/* The following program finds the highest temperature out of the three cities of four
seasons*/
void main( )
{
const int rows=3;
const int columns=4;
int seasontemp[rows][columns]={{26,34,22,17},{24,32,19,13},{28,38,25,20}};
int highest=0, i, j;
for (j=0; i<rows, ++i)
for( j=0; j<columns; ++j)
{
if(temp[i][j]>highest
highest=temp[i][j];
}
cout<<" The highest temperature is ="<<highest;
}