How to understand what to put in the return statement? When to put return 0 and all return with other expressions? How to determine what to return? I mean how to determine what to put after return (?)? I just don’t understand what to return, I don’t understand what to write after return in parentheses return (?)? For example :

int find_substr(char *s1, char *s2) { register int t; char *p, *p2; for(t=0; s1[t]; t++) { p = &s1[t]; p2 = s2; while(*p2 && *p2==*p) { p++; p2++; } if(!*p2) return t; /* 1-й оператор return */ } return -1; /* 2-й оператор return */ } 

why is there a return -1, what does this -1 mean, why is there -1, and not 0 or not p? How do you understand in what circumstances and what to put after the return?

  • eight
    1) In return, you need to put what should return the function. And what it should return, in what situations, and what the returned values ​​mean - its author decides, and describes it in the documentation. In this case, obviously, -1 means that the substring is not found,> = 0 is the position of the found substring. 2) Before asking such questions it would be good to look through a primer at least a little. Nobody will read it out loud to you here. - user6550

3 answers 3

Your question is essentially meaningless. It is too general or something, akin to the question "when should I use the letter A ". The answer to the question “what should be put in the return expression” in each specific case depends on the logic of the function, therefore there is no universal recipe that says “write here return 0 , and then return“ Hello world ” . What for the given example - try to understand what (and most importantly how) this function does, then you will probably see logic in why this function returns certain values, in particular, -1 in search functions usually implies that the desired value was not found. However, I repeat once again - this is only specific first example of a function. The answer to your question should derive from the logic functions implemented in each case

    This function, apparently, searches for a substring in the string.

    If successful, it returns the t - position of the found substring (the sequence number of the first character of the substring in the given string).

    In case of failure, returns -1 , since this is not a valid sequence number of the character in the string (there is no character number -1 ) and the one who called this function will understand that the substring was not found.

    In general, returning -1 is a fairly common practice in functions that return the position of an object in an ordered list.

       int find_substr(char *s1, char *s2) 

      In the function description, the first is the type of the variable that it should return after execution.

      You have to decide whether to get something from the function or not.

      Let's say

       int iResult=find_substr("строка 1","стр"); if (iResult==-1) //поиск окончился неудачей ... ; else //поиск успешен! ... ; 

      In the return operand, you add the values ​​you need (0, -1, -2, whatever of type int), and then you analyze the result in iResult.

      If you do not need to wait for something from the function, then instead of int you specify the void type.