How to assign return value of a function into a variable in c++?

i want to assign a returned value of a function into a Variable in C++, but the program is exit without any output.

int numberPosition = binarySearch(arr, num, searchNumber);

program output screenshot

Full code---

#include <iostream>
using namespace std;

int binarySearch(int arr[], int num, int searchNumber){
    int start=0, end=num;

        int mid = (start+end) / 2;
        if(arr[mid] == searchNumber){
            return mid;
        }else if(arr[mid] <= searchNumber){
            start = mid+1;
            end = mid-1;
    return -1;

int main(){
    int num, arr[num], searchNumber;
    cout<<"How many Elements u want to insert? \n";
    cout<<"\n Enter ut Numbers:- \n";
    for(int i=0; i<num; i++){
    cout<<"\n Enter the Number u want to search:- ";
    int numberPosition = binarySearch(arr, num, searchNumber);
        cout<<searchNumber<<" is founded at position: "<<numberPosition;
        cout<<searchNumber<<" is Not Founded";
    return 0;

Read more here:

Content Attribution

This content was originally published by Numan Ahmed at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: