0

Please help me with this:

My code for linear search:

// To check n is a positive int.

if (n<0)
{
 return false;
}

// To check if value can be found in values array. if values is found return true.

for (int i = 0; i<=n; i++)
{
 if (values[i] == value)
 {
  return true;
 }
}

Now what I dont understand is how to code if the value is not found in values array. Some people have written 'return false' after for loop. That does not make sense to me. If value is found in values array for loop will return true and once the for loop is completed there is return false syntax. This seems contradicting that search function will return true when the value is found using for loop and then return false.

return false

1 Answer 1

5

This seems contradicting that search function will return true when the value is found using for loop and then return false.

Not really. A return statement is capable of returning from a function (i.e., ending the function's execution and returning control to the caller).

A function either returns a value or it does not. In case it returns a value, the type of the value returned is specified as part of the function's signature. This type is often referred to as the function's return type. An example on a function's return type is the type bool in

bool search(int values[], int value, int n)
{
    // some code
}

A function that does not return a value has return type void as it

void foo(int x)
{
    // some code
}

In case a function returns a value, it has to be guaranteed that the closing brace that is delimiting the body of the function is never reached before a value of the specified return type is returned (using a return statement). For example

int abs(int x)
{
    if (x < 0)
        return -x;

    return x;
}

If the statement return x; was omitted, it would be possible that the closing brace of the function abs is reached before returning a value of type int, that is, when x >= 0 in which case the body of the if statement is never executed.

Having the statement return x; after the body of the if statement above guarantees that x is returned whenever x < 0 is false.

If, however, the body of the if statement was executed (i.e., x < 0) was true, the statement return -x; is executed and the function terminates immediately with the value -x returned to the caller.

By default, a function that does not return any value (i.e., has the return type void) terminates as the closing brace that is delimiting its body is reached. However, you can still forcibly terminate the execution of this function at any point by having a return; statement. For example,

void foo (int x)
{
    if (x > 0)
        return;

    printf("I got here!\n"); // executed only when x <= 0
}

For more information, you may watch the short on functions!

2
  • Cheers mate! ... I understand it now. Thanks for your help.
    – Malik
    Commented Feb 16, 2015 at 12:49
  • @Malik if this answers your question, you may up-vote the answer and accept it to mark this question as solved! Thank you!
    – kzidane
    Commented Feb 16, 2015 at 13:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .