You are on page 1of 10

Introduction to programming I 2007

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

1. Declaring and Defining function

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

calling function head

function call

function Definition

Example

int sample( );

void main( )

By Deres G. RVU Page 1


Introduction to programming I 2007
E.C

int y=sample( );

.
}

Example

/* A program that incorporates function to return sum of two sum of


two integer */
#include <iostream.h>

void main( )

int sum(int a, int b); // prototype

void main ( )

int num1, num2;

cout<<"Enter two Integers "<<endl<<endl;

cout<<"Input 1: ";

cin>>num1;

cout<<" Input 2: ";

cin>>num2;
By Deres G. RVU Page 2
Introduction to programming I 2007
E.C

int s=sum(num1, num2);

cout<<"Sum of "<<num1 <<" and "<<num2<<" is "<<s<<endl;

cout<<" ********** HAVE A NICE DAY **********";

//Start defining function sum

int sum(int j, int k)

int add;

add=j+k;

return(add)

Function prototype is declaration of a statement in the calling argument.

syntax

type function name( type of argument);

Type of argument contain data types of argument that must be passed to the
function and optional names of arguments.

e.g. float sum-up(float x, float y, float z);

float sum-up(float x,y,z); illegal.

The calling statement should not include type name in the argument list.

Defining the Function

The definition of a function consists of function header and its body. the header is
exactly like a function prototype.

By Deres G. RVU Page 3


Introduction to programming I 2007
E.C

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.

//Program print a triangle of stars

#include<iostream.h>

void printstars(int blanks, int stats)

void main( )

int nolines;

int counter, noblanks;

cout<<"Enter the number of stars lines (1 to 20) to be printed ";

cin>>nolines;

while(nolines<0 || nolines>20

cout<<"Number of star line should be between 1 and 20 "<<endl;

cout<<"Enter the number of star lines to be printed :";

cin>>nolines;

cout<<endl<<endl;

noblanks=30;

for (counter=1; counter<=nolines; counter++)

printstars (noblanks, counter);

noblanks- -;
By Deres G. RVU Page 4
Introduction to programming I 2007
E.C

void printstars(int blanks, int starline)

int count

for (count=1; count<=blanks; count++)

cout<<" ";

for (count=1; count<=starline; count++)

cout<<" * ";

cout<<endl;

By Deres G. RVU Page 5


Introduction to programming I 2007
E.C

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[ ].

1. One dimensional array

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

/* A program that accepts result of five students' and display back */

#include<iostream.h>

void main( )

By Deres G. RVU Page 6


Introduction to programming I 2007
E.C

float result[50];

cout<<"Enter result of five student ";

for (int i=0; i<5; i++)

cout<<" Enter result "<<i+1<<": ";

cin>>result[i];

cout<<"\n Results you entered are \n";

for (i=0; i<5; i++)

cout<<"Result "<<i+1<<" = "<<result[i];

cout<<" \n Have A Nice Week End! ";

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

To declare two dimensional array of type integer data type.

By Deres G. RVU Page 7


Introduction to programming I 2007
E.C

int A[10][20]

initializing array

Example

int A[4]={1,2,3,4};

int A[ ]={1,2,3,4,5,6};

int A[3]={1,2,3,4}; //illegal


float A[2][3]={1,2,3,4,5,6};

float A[2][3]={{1,2,3}{4,5,6}};

Example.

/*A program to accept table of real number and display back */

#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";

By Deres G. RVU Page 8


Introduction to programming I 2007
E.C

for(i=0; i<row; i++)


{
for(i=0; j<col; j++)
cout<<table[i][j]<<"\t";
cout<<endl;
}
}
Exercise

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;
}

By Deres G. RVU Page 10

You might also like