6 - Functions II
6 - Functions II
Functions II
Remember pointers
int x = 1, y = 2, z[10];
int *ip; /* A pointer to an int */
ip = &x; /* Address of x */
y = *ip; /* Content of ip */
*ip = 0; /* Clear where ip points */
ip = &z[0]; /* Address of first element
of z */
Pass-by-Reference
void
set_x_and_y(int *x, int *y)
{
*x = 1001;
*y = 1002; a 1001
1
}
b 1002
2
int main()
{
int a = 1; x
int b = 2;
cout<<a<<b; y
set_x_and_y(&a, &b);
cout<<a<<b;
}
Remember the bad Swap Example
#include <iostream>
void swap(int , int); //function declaration
int main(){
int a=4;
int b=5;
swap(a,b);
cout <<“The value of ‘a’ is”<<a<<“and the value of ‘b’ is”>>b;
return 0;
} THE VALUES OF A AND B DID NOT SWAP ☹
//function definition
void swap(int val1,int val2){
int temp;
cout <<“The two values entered are”<<val1<<val2;
temp=val1;
val1=val2;
val2=temp;
}
Solution
#include <iostream>
//function definition
int swap(int *val1,int *val2){
int temp;
cout <<“The two values entered are”<<val1<<val2;
temp=*val1;
*val1=*val2; int main(){
*val2=temp; int a=4;
} int b=5;
swap(&a,&b);
cout <<“The value of ‘a’ is”<<a<<“and the value of ‘b’ is”>>b;
return 0;
} // the values of a and b did swap ☺.
Another example
Write a function that computes the area and circumference of a circle, given its radius
#include <iostream>
void area_circum (float radius, float *area, float *circum);
int main (void) {
float radius, area, circum ;
cin>>radius;
area_circum (radius, &area, &circum) ;
cout<<"the area is” << area<< “and circumference is <<circum;
return 0;
}
void area_circum (float radius, float *area, float *circum) {
*area = 3.14 * radius * radius ;
*circum = 2 * 3.14 * radius ;}
Write a C function to get first 3 even number after an
input value and
- return to main / - print in main
cin >> n;
int main(void) {
int a=10, b=16; void test2(int *m, int *n) {
cout<<“a=“<a<<“b”<b; *m=5;
test1(a,b); *n=24;
cout<<“a=“<a<<“b”<b; }
test2(&a,&b);
cout<<“a=“<a<<“b”<b; void test3(int a, int *b) {
test3(a,&b); a=38;
cout<<“a=“<a<<“b”<b; *b=57;
return 0;} }
Calling Functions with
Arrays
Calling Functions with Arrays
• In C programming, a single array element or an entire array can
be passed to a function.
• Also, both one-dimensional and multi-dimensional array can be
passed to function as argument.
Passing a single element of an array
to function
#include <iostream>
void display(int a)
{ cout<<a;}
int main()
{int c[]={2,3,4};
display(c[2]);//passing array element c[2] only.
return 0;}
output
4
Pointers and arrays contd..
int *p;
int sample[10];
p = sample; // same as p = &sample[0];
int *p;
int sample[10];
p = sample;
p[5] = 100; // Both these statements
*(p+5) = 100; // do the same thing
Passing single dimension arrays to
functions
• In C, you cannot pass the entire data of the array as an argument to a
function. int main() {
int sample[10];
• How to pass array then? func1(sample);
• Pass a pointer to the array. …
}
void func1(int *x) {
…
}
void func1(int x[10]) {
…
}
void func1(int x[]) {
…
}
Passing Entire One-Dimensional Array
to a Function
#include <iostream>
float average(float a[]);
int main()
{
float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18};
avg=average(c,6);/* only name of array is passed as argument. */
printf("average age=%f",avg);
return 0;}
float average(float a[],int x)
{int i;float avg, sum=0.0;
for(i=0;i<x;++i)
{ sum+=a[i];}
avg =(sum/x);
return avg;}
Passing Arrays to Functions
• Specify the name without any brackets
• To pass array myArray declared as
int myArray[ 24 ];
to function myFunction, a function call would resemble
myFunction( myArray, 24 );
• Array size is usually passed to function
• Arrays passed call-by-reference
• Value of name of array is address of the first element
• Function knows where the array is stored
• Modifies original memory locations
• Individual array elements passed by call-by-value
• pass subscripted name (i.e., myArray[ 3 ]) to function
Passing Arrays to Functions
• Function prototype:
void modifyArray( int b[], int arraySize );
• Parameter names optional in prototype
• int b[] could be simply int []
Passing Two-Dimensional Arrays as
Parameters to Functions
• Two-dimensional arrays can be passed as parameters to a
function
• The base address, that is, the address of the first component of
the actual parameter is passed to the formal parameter
Two-Dimensional Arrays
• Two-dimensional arrays can be passed as whole
• Pass certain row(s)
• Pass certain columns(s)
• Pass certain element
• When passing two-dimensional arrays, it is not mandatory to
specify the number of rows in the array.
• However, the number of columns should always be specified.
Write a C function that calculates the sum of a 1D
array
Write a main function that
uses the previous
function (without
modification) to calculate
the sum of each row in a
2D array. Array size is m
xn cin>>n>>m ;
cin>>a[i][j] ;
cin>>a[i][j] ;
cout>>s ;
cout<<s ;
Write a main function that
uses the previous
function (without
modification) to calculate
the sum of a 2D array.
Array size is m x n cin>>n>>m ;
cin>>a[i][j] ;
cin>>a[i][j] ;
{s += sum(m,a[i])
cout>>s ; ;
cout<<s ;
Write a function called delete_repeats that has a partially filled array of characters as a formal parameter and that deletes all
repeated letters from the array.
Since a partially filled array requires two arguments, the function will actually have two formal parameters: an array parameter and
a formal parameter of type int that gives the number of array positions used. This second formal parameter will be a call-by-
reference parameter and will be changed to show how much of the array is used after the repeated letters are deleted.
When a letter is deleted, the remaining letters are moved forward to fill in the gap.
For example, consider the following code:
char a[10];
a[0] = 'a';
a[1] = 'b';
a[2] = 'a';
a[3] = 'c';
int size = 4;
delete_repeats(a, size);
After this code is executed, the value of a[0] is 'a’, the value of a[1] is 'b', the value of a[2] is 'c', and the value of size is 3. (The value of a[3]
is no longer of
any concern, since the partially filled array no longer uses this indexed variable.) You may assume that the partially filled array contains only
lowercase letters.
Solution
Examples
Trace
cout<< X[0]<<“\n”;
cout<< X[2]<<“\n”;
cout<< I<<“\n”;
cout<< x[i];
cout<< X[3]<<“\t”<<X[5]<<“\n”<<G;
Trace- solution
15.000000XXYY15.0000001.000000
13.000000
12
15.000000
15.000000 15.000000
555.000000
Write a function to find the bigger number of two inputs –
return pointer