0

Can a function return other function , but how ? I was implementing binary search in C

bool search(int value, int values[], int n)
{
    // TODO: implement a searching algorithm
    if(n<1)
    {
        return false;
    }
    else
        binary_search(value,values,n);
 }  

and binary search code was:

 //binary search
   bool binary_search(int value,int values[],int n)
   {
    int start = 0;
    int end = n-1;
    while(end>=start)
    {
        int mid = (start+end)/2;
        if(values[mid]==value)
        {
            return true;
        }
        else if(values[mid]>value)
        {
            end = mid-1;
        }
        else
        {
            start = mid+1;
        }
    }
    return false;
    }

This is not compiling it says "control may reach end of non-void function". If the function I am calling returns bool then why am i supposed to put a return statement before

binary_search(value,values,n);

like

return binary_search(value,values,n);
shashank2806
  • 480
  • 6
  • 14

5 Answers5

11

With

return binary_search(value,values,n);

you are not returning a function, you are calling a function, and returning what the function returns.

And in the context of the search function the above return statement is the correct one.

If you don't return a value from a function declared to return a value, it will lead to undefined behavior.

To return a value from a function you need to explicitly use the return statement. There's no chaining of function calls or implicit returns (except as a special case of the main function, where an implicit return 0; statement will be inserted last if there is none).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

I think you have some confusion about the difference between an expression and a statement. This question will help to explain it. It's really worth your time to try to understand this, because it's one of the most tricky things for beginning programmers to learn.

Briefly, binary_search(value,values,n) has both an action (calling the function) and a value (the return value from the function). When you put it on a line by itself, the value is calculated and thrown away, but the action still happens. If you want the value, you need to do something with it, such as returning it.

Community
  • 1
  • 1
Dan
  • 12,409
  • 3
  • 50
  • 87
2

When you write

binary_search(value,values,n);

it means that only a function call is made. The control will be transferred to "binary_search" function and after it is executed control will be given back to the calling function and the next line of the calling function will be executed which in this case is nothing.

Hence the message "control may reach end of non-void function" is displayed.

You have to explicitly tell the computer to return the required value.

To better understand, you can think of it as

bool someTemporaryVariable = binary_search(value,values,n);
return someTemporaryVariable;
Akash Mahapatra
  • 2,988
  • 1
  • 14
  • 28
1

You can return the value of the function that the function returns by

return binary_search(value,values,n);

what you are doing is only calling the function, not returning it. You need a return keyword before function call

  • 1
    _You can only return the value of the function that the function returns_.... You can easily return a function pointer too. ;) – LPs Mar 20 '17 at 13:34
0

Some programmer dude answered the question you're actually asking.

To answer the title of the question, no, a function cannot return a value of function type (i.e., a function cannot return another function). However, a function can return a value that's a pointer to a function type:

/**
 * Returns a pointer to a search function
 */
bool (*get_func( /* lookup params */ ))(int, int *, int)
{
  ...
  if ( /* lookup params indicate binary search */ )
    return binary_search; // note that we return the function name only
  ...
}

/**
 * get_func returns a pointer to one of several different searching
 * functions, based on the type of search we want to perform
 */
bool (*search_func)(int, int *, int ) = get_func( /* lookup params */);

/**
 * Execute the searching function on values through the search_func
 * pointer.
 */
if ( search_func != NULL && search_func( value, values, n ) )
  // do something

The way to read the declaration of get_func is

       get_func                                            -- get_func
       get_func(                     )                     -- is a function taking
       get_func( /* lookup params */ )                     -- some parameters (won't get into details here  )
      *get_func( /* lookup params */ )                     -- returning a pointer
     (*get_func( /* lookup params */ ))(               )   -- to a function taking
     (*get_func( /* lookup params */ ))(int            )   --     parameter unnamed of type int
     (*get_func( /* lookup params */ ))(int, int *     )   --     parameter unnamed of type int *
     (*get_func( /* lookup params */ ))(int, int *, int)   --     parameter unnamed of type int
bool (*get_func( /* lookup params */ ))(int, int *, int)   --     returning bool

Similar to how array expressions "decay" to pointer expressions, a function expression will automatically be converted to a pointer expression as well. When we write return binary_search in get_func, we're not calling binary_search; instead, the binary_search expression is converted from type "function returning bool" to "pointer to function returning bool". Note that the return type (bool) and the number and types of parameters (int, int *, and int) must all match up between the search_func pointer, the get_func function, and the actual binary_search function.

A typedef makes this easier to read:

/**
 * searchFuncType is an alias for type "function taking an int,
 * int *, and int, and returning a bool"
 */
typedef bool searchFuncType( int, int *, int );
...
searchFuncType *get_func( /* lookup params */ )
{
  ...
  return binary_search;
  ...
}
...
searchFuncType *searchFunc = get_func( /* lookup params */ );
...

although I prefer to use "naked" types, unless I intend to hide that type behind an API.

John Bode
  • 119,563
  • 19
  • 122
  • 198