Lecture #14
Lecture #14
BİL1102
Programlamaya Giriş
2024 Güz
Passing Arrays to Functions (1)
• To pass an array argument to a function, specify the array’s name without any brackets.
• Ex int myArray[24];
myfunction(myArray, 24);
1-2
Passing Arrays to Functions (2)
• C automatically passes arrays to functions by reference - the called
functions can modify the element values in the callers’ original arrays.
• The name of the array evaluates to the address of the first element of the
array.
• Because the starting address of the array is passed, the called function
knows precisely where the array is stored.
• Therefore, when the called function modifies array elements in its function
body, it’s modifying the actual elements of the array in their original memory
locations.
• An array name is really the address of the first element of the array.
1-3
Passing Arrays to Functions (5)
Passing array elements
•Passed by call-by-value
•Pass subscripted name (myArray[3]) to function
Function prototype
1-6
Passing Arrays to Functions (6)
Passing array elements
1-7
Passing arrays to Functions Ex
void fun(int arr[])
or
void fun(int *a)
1) Passing an element of an array
Determine if an array element is even.
int even (int);
int main()
{ int arr[10], i;
for (i=0; i < 10; i++)
{ scanf(“%d”, &arr[i]);
if (even(arr[i])) int even (int a)
printf(“%d is even”, arr[i]); {
else
printf(“%d is odd”, arr[i]);
if (a % 2 == 0)
} return(1);
return(0); else
} return(0);
}
1-9
2) Passing the whole array
Write a function to find the sum of an array element is even.
????
1-10
2) Passing the whole array
Because arrays are implemented as pointers, they are always
passed as call by reference.
void change(int[ ], int);
int main()
{ int one[ ] = {3, 5, 7}, i;
change(one, 3);
for (i=0; i < 3; i++)
printf(“%d”, one[i]);
}
void change(int xarr[ ], int s) OUTPUT
{ int i; 4 6 8
for (i=0; i < s; i++)
xarr[i]++;
}
1-11
Example1
Find the max of a 5-element int array
#include <stdio.h>
int findmax(int [], int);
int main()
{ int a[ ] = {5, 4, 6, 8, 1};
printf(“max of the array : %d”, findmax(a, 5));
}
int findmax(int arr[ ], int size)
{ int i, max=arr[0];
for (i=1; i < size; i++)
if (arr[i] > max)
max = arr[i];
return(max);
}
1-12
Example2
Declare 2 10-element double arrays & input their elements using
a function.
#include <stdio.h>
void input( double [], int size);
int main()
{ double a[10], b[10];
input(a, 10);
input(b, 10);
....
}
void input(double p[ ], int s)
{ int i;
for (i=0; i < s; i++)
scanf(“%lf”, &p[i]);
}
1-13
Example3
Write a program that inputs the size of the array n(<=100) &
then reads n elements into the arrays x. Using a function, the
program should find the reverse of the array.
Eg x: 5 6 7 8
reverse x : 8 7 6 5
1-14
void reverse(int size, int arr[])
Example3
#include <stdio.h> { int i, temp;
void reverse(int size, int a[]); int mid = size / 2;
int main() for (i=0; i < mid; i++)
{ int x[100], n, i; {
printf(“Enter the size of the array:”); temp= arr[i];
scanf(“%d”, &n); arr[i] = arr[size- i -1];
printf(“Enter array elements:”); arr[size- i -1] = temp;
for (i=0; i < n; i++) }
scanf(“%d”, &x[i]); }
reverse(n, x);
printf(“Reversed array elements:”);
for (i=0; i < n; i++)
scanf(“%d”, &x[i]);
}
...
1-15
Arrays as Function Parameters
1-16
The square brackets next to the formal parameter arr indicates
that it is an array. We don’t write the size of the array within the
brackets, but instead we pass it as another parameter to the
function.
double maxar (double arr[], int size)
1-36