L10_PDS
L10_PDS
Example - 1
#include <iostream>
using namespace std;
void print(int a)
{
a = a + 5;
}
int main()
{
int a = 5;
print(a);
cout << a<< " ";
return 0;
}
Output: 5
Methods to Pass Array to a
Function in C++
In C++, we have the following ways to pass an array as a parameter to
the function:
• As a sized array
• As an unsized array
• As a pointer (pass by pointer)
• As a reference (pass by reference)
Passing as a Sized Array
• In this method, we pass the array in the same way we declare it with
the array type, name, and size.
Syntax:
Syntax
• Syntax:
#include <bits/stdc++.h>
using namespace std;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, low, mid - 1, x);
if (arr[mid] < x)
// Element to be searched
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? cout << "Element is not present in array“ : cout << "Element is present at index "
<< result;
return 0;
}
Output:
Element is present at index 3