You are on page 1of 13

Passing parameters INCORRECTLY

void main( )
{ char ch = ‘Q’; int a = 15; double b =5.6;
function1(a);
function2(ch,a);
function3(b,a,ch);
cout << ch << a << b;
}
void function3(char c, int x,double s)
{ cout << c << x << s;}
Passing Arrays to Functions
void ShowValue(int);
int main()
{ const int SIZE = 8;
int Collection[SIZE] = {5,10,15,20,25,30,35,40};
for (int Cycle =0; Cycle < SIZE; Cycle++)
ShowValue (Collection[Cycle]); // pass by
return 0; } // value
void ShowValue(int Num)
{ cout << Num << “ “;}
}
Passing Arrays to Functions
void ShowValues(int [ ], int);
int main()
{ const int SIZE = 8;
int Collection[SIZE] = {5,10,15,20,25,30,35,40};
ShowValues (Collection, SIZE); // pass by
return 0; } // reference
void ShowValues(int Nums[ ], int size)
{ for (int Index = 0; Index < size; Index++)
cout << Nums[Index] << “ “;}
Passing Arrays to Functions
int main()
{const int SIZE = 7;
int Set[SIZE]={1,2,3,4,5,6,7};
DoubleArray(Set,SIZE);
return 0; }
void DoubleArray(int Nums[ ], int Size)
{ for ( int Index = 0; Index < Size; Index++)
Nums[Index] *=2; }

Set

1 2 3 4 5 6 7
Passing Arrays to Functions
int main()
{ const int SIZE = 7;
int Set[7]={1,2,3,4,5,6,7};
DoubleArray(Set,SIZE) &Set[0]
; return 0; }
void DoubleArray(int Nums[ ], int Size)
{ for ( int Index = 0; Index < Size; Index++
Nums[Index] *=2; }
Set

12 2
4 63 8
4 10
5 12
6 14
7
Passing Arrays to Functions
int main ()
{ int a[5]={0,1,2,3,4};
modifyElement(a[3]); return 0; }
//pass 1 element by value
void modifyElement (int e) 3
{ e *= 5;
e
cout << e;} No change in array in main

0 1 2 3 4
a
Passing Arrays to Functions
int main ()
{ int a[5]={0,1,2,3,4};
modifyElement(a[3]); return 0; }
//pass 1 element by value
void modifyElement (int e) 15
{ e *= 5;
e
cout << e;} No change in array in main

0 1 2 3 4
a
Returning a value from a
function
A function may return a value back to
the part of the program that called the
function by using a return statement

Argument
Argument Return
Function
Value
Argument
Argument
Returning a value from a
function
int Square (int); // function prototype
void main(void)
{ int Value, Result;
cout << “Enter a number and I will square it:”;
cin >> Value;
Result = Square (Value);
cout << Value << “squared is” << Result;}
int Square (int Number)
{ return Number * Number; }
WAP to add two numbers using functions

#include<iostream.h>
#include<conio.h>
int f(int ,int);
void main()
{
clrscr();
int a,b,c;
cout<<"enter numbers";
cin>>a>>b;
c=f(a,b);
cout<<c;
getch();
}
int f(int p,int q)
{
int r;
r=p+q;
return r;
}
WAP to find the largest of two numbers using function

#include<iostream.h>
#include<conio.h>
int f(int,int);
void main()
{
clrscr();
int a,b,c;
cout<<"enter numbers";
cin>>a>>b;
c=f(a,b);
cout<<"largest number is"<<c;
getch();}
int f(int a,int b)
{
if(a>b)
return a;
else
return b;
}
WAP to sort numbers using function

#include<iostream.h>
#include<conio.h>
void sortf(int[],int);
void main()
{
clrscr();
int a[10],n,i;
cout<<"enternumber of numbers";
cin>>n;
cout<<"enter numbers";
for(i=0;i<n;i++)
cin>>a[i];
sortf(a,n);
for(i=0;i<n;i++)
cout<<a[i];
getch();
}
void sortf(int a[],int n)
{
int temp,i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

You might also like