Last index using recursion approach

When I am initializing the index variable locally it will gives me the wrong result -1. I didn’t get this?

// Return the index of the number else return =1

#include
using namespace std;

// Last Index Function

//Iterative approach

/int lastIndexIterative(int a[], int n, int key) {
int ans = -1;
for(int j=n-1; j>=0; j–) {
if(a[j] == key) {
ans = j; // store the index
break;
}
}
return ans;
}
/

// Recursive Solution
int lastIndex(int a[],int i,int n, int key) {
// Base case
int index=-1;
if(i==n) {
return -1;
}
if(a[i] == key) {
index = i;
}
lastIndex(a,i+1,n,key);
return index;
}

int main() {
int a[] = {86, -16, 77, 65, 45, 77, 28};
int n = sizeof(a)/sizeof(int);
int result = lastIndex(a,n,77);
cout << result;
return 0;

}