2.array and Functions
2.array and Functions
1
C++ Programming | CCIT
Table of Contents
Array 3
Introduction to array 3
Create an array 3
Array Initialization 4
Functions 8
Introduction to Array 8
General Format of Function 8
Return type9
Calling functions 9
Functions Arguments 12
Functions returning value 21
Function declaration/prototype 29
Scope of Variables 30
Scope Resolution Operator 31
Reference Variables 31
Reference Arguments 33
Function Overloading 34
Default arguments 40
2
C++ Programming | CCIT
Array
An array is a derived data type that can store multiple values of same type.
Array is a group of elements of same type sharing same name and stored in
consecutive memory locations.
Creating an Array
An Array can be created by specifying data type, array name and its size.
Syntax:
5050 5054 5058 5012 5016 5020 5024 5028 5032 5036
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
arrayname [ index ]
Array Initialization
While defining an array we can directly store values into it.
Syntax:
datatype arrayname[size]={values…};
4
C++ Programming | CCIT
WAP to read 10 nos in an int array and find sum of all elements.
#include<iostream> Enter 10 int no.
using namespace std; 8 5 6 3 7 4 5 9 6 4
main() Sum is 57
{
int a[10],s=0;
cout<<"Enter 10 int no."<<endl;
for(int i=0;i<=9;i++)
{
cin>>a[i];
s=s+a[i];
}
cout<<"Sum is "<<s;
} 5
C++ Programming | CCIT
WAP to read temperature values for 7 days of a week and find average
temperature of that week.
#include<iostream>
using namespace std;
main()
{
int a[6],s;
float Avg;
cout<<"Enter temperature 7 days"<<endl;
for(int i=0;i<=6;i++)
{
cin>>a[i];
s=s+a[i];
Avg=s/7.0;
}
cout<<"Average temperature is "<<Avg;
}
6
C++ Programming | CCIT
WAP to read 10 nos in an int array and find sum of all even nos.
#include<iostream>
using namespace std;
main()
{
int a[9],s;
cout<<"Enter 10 no."<<endl;
for(int i=0;i<=9;i++)
{
cin>>a[i];
if(a[i]%2==0)
s=s+a[i];
}
cout<<"Sum of even no. is "<<endl;
}
7
C++ Programming | CCIT
Functions
Functions
A function is a block of code design to perform some specific task. Functions are
used to divide program into logical modules. They are used to avoid repetition of
code in different part of program. Once a function is designed it can be used
anywhere in program whenever required.
8
C++ Programming | CCIT
return type
It indicates the datatype of value the function is going to return.
If function is returning a value then it can be any datatype such as int, long,
float, double, char, pointer, structure etc.
If function is not returning any value then return type must be void.
if no return type is specified then by default return type is int.
Calling functions
Once a function is designed it can be call whenever required.
Syntax:
9
C++ Programming | CCIT
CCIT
*************************
Amravati
*************************
10
C++ Programming | CCIT
Design a function display which will print all nos from 1 to 100 .
#include<iostream> Number is 1 2 3 4 5 6 4
using namespace std; 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22
void display() 23 24 25 26 27 28 29 30
{ 31 32 33 34 35 36 37 38
39 40 41 42 43 44 45 46
int i;
47 48 49 50 51 52 53 54
for(i=1;i<=100;i++) 55 56 57 58 59 60 61 62
cout<<i<<" "; 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78
}
79 80 81 82 83 84 85 86
main() 87 88 89 90 91 92 93 94
{
95 96 97 98 99 100
cout<<"Number is ";
display ();
}
Design a function even which will print all even nos in range 1 to 100
#include<iostream> Even nos. are 2 4 6 8 10
using namespace std; 12 14 16 18 20 22 24 26
28 30 32 34 36 38 40 42
void even() 44 46 48 50 52 54 56 58
{ 60 62 64 66 68 70 72 74
76 78 80 82 84 86 88 90
for(int i=2;i<=100;i=i+2) 92 94 96 98 100
cout<<i<<" ";
}
main()
{
cout<<"Even nos are ";
even();
}
11
C++ Programming | CCIT
Design a function odd which will print all odd nos in range 1 to 100
#include<iostream> Odd nos. are 1 3 5 7 9 11
13 15 17 19 21 23 25 27
using namespace std;
29 31 33 35 37 39 41 43
void odd() 45 47 49 51 53 55 57 59
{ 61 63 65 67 69 71 73 75
77 79 81 83 85 87 89 91
int i; 93 95 97 99
for(i=1;i<=100;i=i+2)
cout<<i<<" ";
}
main()
{
cout<<"Odd nos. are ";
odd();
}
Functions Arguments
formal parameters
While calling a function
we can pass some data returntype functionName(datatype args1,...)
(actual parameters). {
actual parameters Statements
----------
For ex: even( 25 ) ;
return value ;
This data is passed on }
to function as
arguments (formal parameters).
According to argument value received the function can perform different task.
Note:
No of actual parameters and formal parameters must be same.
Datatype of actual parameters and formal parameters must be same.
Sequence of actual parameters and formal parameters must be same.
12
C++ Programming | CCIT
Example
#include<iostream>
using namespace std;
void star(int n)
{
int i;
for(i=0;i<=n;i++)
cout<<"*";
cout<<endl;
}
main()
{
cout<<"CCIT"<<endl;
star(15);
cout<<endl<<"Amravati"<<endl;
star(25);
}
CCIT
***************
Amravati
*************************
13
C++ Programming | CCIT
Design a function display which will print all nos from 1 to specified no which is
passed as argument.
#include<iostream> 1 2 3 4 5 6 4 5 6 7 8 9 10 11 12 13 14
using namespace std; 1 2 3 4 5 6 4 5 6 7
void display(int n)
{
for(int i=1;i<=n;i++)
cout<<i<<" ";
}
main()
{
display(14);
cout<<endl;
display(7);
}
Design a function odd which will print all odd nos in range 1 to specified no which
is passed as argument.
#include<iostream> 1 3 5 7 9 11 13 15 17 19 21 23 25
using namespace std;
1 3 5 7
void odd(int n)
{
for(int i=1;i<=n;i=i+2)
cout<<i<<" ";
}
main()
{
odd(25);
cout<<endl;
odd(7);
}
14
C++ Programming | CCIT
Design a function even which will print all even nos in range 1 to specified no
which is passed as argument.
#include<iostream> 2 4 6 8 10 12 14 16 18 20 22 24
using namespace std; 2 4 6
void even(int n)
{
for(int i=2;i<=n;i=i+2)
cout<<i<<" ";
}
main()
{
even(25);
cout<<endl;
even(7);
}
Design a function display which will print all nos from 1st argument to 2nd
argument. (1st arg < 2nd arg)
#include<iostream> 10 11 12 13 14 15 16 17 18 19 20
using namespace std;
void display(int a,int b)
{
int i;
for(i=a;i<=b;i++)
cout<<i<<" ";
}
main()
{
display(10,20);
} 15
C++ Programming | CCIT
Design a function interest which will calculate and print simple interest form 3
arguments p,r,t (si=(p*r*t)/100)
#include<iostream> Simple interest is 183.75
using namespace std;
void interest(int p,float r,int t)
{
float si=(p*r*t)/100;
cout<<"Simple interest is
"<<si<<endl;
}
main()
{
interest (5000,12.25,3);
}
Design a function volume which will calculate and print volume of box form 3
arguments l,b,h V=L*B*H
#include<iostream> Volume of Box is 40
using namespace std;
void volume(int l,int b,int h)
{
int v=l*b*h;
cout<<"Volume of Box is "<<v<<endl;
}
main()
{
volume(2,4,5);
}
16
C++ Programming | CCIT
Design a function volume which will calculate and print volume of sphere when
radius is pass as argument.
v=4/3*3.14*r3
#include<iostream>
using namespace std;
void volume(float r)
{
float v=4/3.0*3.14*r*r*r;
cout<<"Volume of sphere is "<<v<<endl;
}
main()
{
volume(2.5);
}
Volume is 65.4167
17
C++ Programming | CCIT
Design 2 functions area and circumference which will calculate and print area and
circumference of circle when radius is pass as argument .
A=3.14*r*r
C=2*3.14*r
void area(float r)
{
float a=3.14*r*r;
cout<<"Area is "<<a<<endl;
}
main()
{
float n;
cout<<"Enter Radius ";
cin>>n;
area(n);
circumference(n);
}
18
C++ Programming | CCIT
Design 2 functions even and odd which will print all even / odd nos from 1 to
specified no.
#include<iostream> 1 3 5 7 9 11 13 15 17 19 21 23
using namespace std; 1 3 5 7
void odd(int n)
{
for(int i=1;i<=n;i=i+2)
cout<<i<<" ";
}
void even(int n)
{
for(int i=2;i<=n;i=i+2)
cout<<i<<" ";
}
main()
{
even(25);
cout<<endl;
odd(100);
cout<<endl;
odd(10);
}
19
C++ Programming | CCIT
Design a function sum which will calculate and print sum of all nos form 1 to a no.
which is pass as argument.
#include<iostream> Sum is 15
using namespace std;
void sum(int n)
{
int s=0;
for(int i=1;i<=n;i++)
s=s+i;
cout<<"Sum is "<<s<<endl;
}
main()
{
sum(5);
}
Design a faction area which will print area of rectangle when length and breadth is
passed as argument.
#include<iostream> Area is 35
using namespace std;
void area(int l,int b)
{
int a=l*b;
cout<<"Area is "<<a<<endl;
}
main()
{
area(5,7);
}
20
C++ Programming | CCIT
Design a function fact which will calculate and print factorial of all nos form 1 to a
no. which is pass as argument.
#include<iostream> Factorial is 120
21
C++ Programming | CCIT
Design a faction area which will print area of rectangle when length and breadth is
passed as argument.
#include<iostream>
using namespace std;
main()
{
int z=area(5,7)+area(10,20);
cout<<"Total area is "<<z<<endl;
}
22
C++ Programming | CCIT
Design a function fact which will calculate and return factorial of a no. which is
pass as argument.
#include<iostream>
using namespace std;
main()
{
int z=fact(5)-fact(4);
cout<<"Result is "<<z<<endl;
}
Result is 96
23
C++ Programming | CCIT
Design a function sum which will calculate and return sum of all nos form 1 to a
no. which is pass as argument.
#include<iostream>
using namespace std;
int sum(int n)
{
int s=0;
for(int i=1;i<=n;i++)
s=s+i;
return s;
}
main()
{
int z=sum(5);
cout<<"Result is "<<z<<endl;
}
Result is 15
24
C++ Programming | CCIT
Design a function interest which will calculate and return simple interest from 3
arguments p,r,n.
si=p*r*n/100
#include<iostream>
using namespace std;
main()
{
float z=interest(5000,10.25,3);
cout<<"Result is "<<z<<endl;
}
Result is 1537.5
25
C++ Programming | CCIT
Design a function volume which will calculate and return volume of box from 3
arguments l,b,h
V=l*b*h
#include<iostream>
using namespace std;
main()
{
int z=volume(3,4,5);
cout<<"Result is "<<z<<endl;
}
Result is 60
26
C++ Programming | CCIT
Design a function volume which will calculate and return volume of sphere when
radius is pass as argument. ( V=4/3*3.14*r3 )
#include<iostream>
using namespace std;
float volume(int r)
{
float v=4/3.0*3.14*r*r*r;
return v;
}
main()
{
int n;
cout<<"Enter Radius ";
cin>>n;
float z=volume(n);
cout<<"Volume of sphere is "<<z<<endl;
}
Enter Radius 2
Volume of sphere is 33.4933
27
C++ Programming | CCIT
Design 2 functions area and circumference which will calculate and return area
and circumference of circle when radius is pass as argument.
#include<iostream> Enter Radius 5
using namespace std; Area is 78.5
float circumference(int r) Circumference is 31.4
{
return 2*3.14*r;
}
float area(int r)
{
return 3.14*r*r;
}
main()
{
int n;
cout<<"Enter Radius ";
cin>>n;
float p=area(n);
float q=circumference(n);
cout<<"Area is "<<p<<endl;
cout<<"Circumference is "<<q<<endl;
}
28
C++ Programming | CCIT
Function declaration/prototype
Function declaration is used to provide information about function in advance
to compiler.
If a function is called before its definition then such function has to be declared
before function call.
Syntax:
returntype functionName(data type of args…);
In function declaration argument name are optional.
Example:-
#include<iostream>
using namespace std;
float interest(int,float,int);
main()
{
float z;
z=interest(5000,10.25,3);
cout<<"Simple intrest is "<<z<<endl;
}
float interest(int p,float r,int n)
{
float si;
si=p*r*n/100;
return si;
}
29
C++ Programming | CCIT
Scope of Variables
In C++ we can define variables at any point within our program.
Scope of variable depends on block or the function in which the variable is
defined.
Example:
int a ;
void demo( int x )
{
int y ;
………….
}
void main()
{
int b ;
………
for( int i = 1 ; i <= 10 ; i++ )
{
int j ;
…………..
}
int k ;
………………
}
30
C++ Programming | CCIT
int a=5; 10
void main() 5
{
int a=10;
cout<<a<<endl;
cout<<::a<<endl;
}
Reference Variables
A reference is an alias name for a variable i.e. when we create a reference, we
assign a new name to a variable.
Syntax:
31
C++ Programming | CCIT
Example:
#include<iostream> 5 5 5
using namespace std ; 8 8 8
main()
{
int a=5;
int &b=a;
int &c=b;
cout<<a<<b<<c<<endl;
a++;
b++;
c++;
cout<<a<<b<<c<<endl;
}
Example:
#include<iostream> 5
using namespace std; 5
void change(int a)
{
a++;
}
main()
{
int a=5;
cout<<a;
change(a);
cout<<a;
32
}
C++ Programming | CCIT
Reference Arguments
If a function argument is defined of type reference argument.
Syntax:
datatype &argname
Then the reference argument refers to the variable which is pass as argument.
Due to this the operations performed by the function by using that reference
are actually performed on original variable.
Design a function swap which will exchange values of 2 variables which are passed
as arguments.
#include<iostream> A=5 B=7
A=7 B=5
using namespace std;
void swap( int &x ,int &y)
{
int temp=x;
x=y;
y=temp;
}
main()
{
int a=5,b=7;
cout<<"A="<<a<<"B="<<b<<endl;
swap(a,b);
cout<< "A="<< a << " B=" << b << endl;
}
33
C++ Programming | CCIT
Function Overloading
Defining multiple functions in a program having same name is called as function
overloading.
Only precaution to be taken is that
1. No of arguments or
2. Type of arguments or
3. Sequence of arguments must be different.
Compiler will decide which function to call depending on no of arguments or
type of arguments or sequence of arguments that are passed while calling the
function.
Example:
#include<iostream> RollNo is 4117
using namespace std; Name is Amit jain
void display(int rn ,string nm) RollNo is 3012
{ Name is Gopal Pandey
cout<<"RollNo is "<<rn<<endl; Branch is CS
cout<<"Name is "<<nm<<endl;
}
void display(int rn,string nm,string br)
{
cout<<"RollNo is "<<rn<<endl;
cout<<"Name is "<<nm<<endl;
cout<<"Branch is "<<br<<endl;
}
main( )
{
display(4117,"Amit jain");
display(3012,"Gopal Pandey","CS");
34
}
C++ Programming | CCIT
Example:
#include<iostream>
using namespace std;
void display(int rn, string nm)
{
cout<<"RollNo is "<<rn<<endl;
cout<<"Name is "<<nm<<endl;
}
void display(string nm, int rn)
{
cout<<"RollNo is "<<rn<<endl;
cout<<"Name is "<<nm<<endl;
}
main()
{
display(4117,"Amit jain");
display("Gopal Pandey",3012);
}
RollNo is 4117
Name is Amit jain
RollNo is 3012
Name is Gopal Pandey
35
C++ Programming | CCIT
Example:
#include<iostream>
using namespace std;
void display(int rn)
{
cout<<"RollNo is "<<rn<<endl;
}
void display(string nm)
{
cout<<"Name is "<<nm<<endl;
}
main()
{
display(4117);
display("Gopal Pandey");
}
RollNo is 4117
Name is Gopal Pandey
36
C++ Programming | CCIT
Design 2 function volume which will calculate volume of box and cube.
#include<iostream>
using namespace std;
int volume(int L, int B, int H)
{
int v=L*B*H;
return v;
}
int volume(int n)
{
int v=n*n*n;
return v;
}
int main()
{
int a=volume(5,7,8);
cout<<"Volume of box is "<<a<<endl;
int b=volume(5);
cout<<"Volume of cube is "<<b<<endl;
}
37
C++ Programming | CCIT
38
C++ Programming | CCIT
39
C++ Programming | CCIT
Default arguments
While defining a function, we can assign default values for each of the last
arguments.
Syntax :
41