Home c++ Using Return in C / C++

Using Return in C / C++




How to understand what should be put in the expression RETURN? When to put Return 0 and all RETURN with other expressions? How to determine what you need to return? I mean how to determine what to put after Return (?)?
I just do not understand what you need to return, I do not understand what to write after Return in Return brackets (?)?
For example:

int find_substr (char * s1, char * s2)
 char * p, * p2;
 For (T = 0; S1 [T]; T ++) {
  p = & amp; s1 [t];
  P2 = S2;
  While (* P2 & amp; & amp; * p2 == * p) {
   P ++;
   P2 ++;
  if (! * p2) Return T; / * 1st operator RETURN * /
  Return -1; / * 2nd operator RETURN * /

Why is there Return -1, that it is -1 denotes why there is exactly -1, and not 0 or not p?
How to understand in what circumstances and what to put after Return?

Answer 1, Authority 100%

Your question in essence does not make sense. He is too common or akin to the question “When you need to use the letter and . The answer to the question “what to put in the RETURN expression” in each particular case depends on the logic of the function, so there is no such universal recipe that it would say that “here I write return 0 , and here Return “Hello World” .
As for the example above, try to understand that (and most importantly, how) makes this function, then you will probably see the logic in why this function returns certain values. In particular, -1 in search functions typically implies that the desired value was not found. However, I repeat once again is just a specific example of one function. The answer to your question must flow from the logic of the function implemented in each case

Answer 2, Authority 62%

This function, apparently, is looking for a substring in the row.

In case of success, it returns T – the position found substring (the sequence number of the first substring symbol in this line).

In case of failure, returns -1 , because This is not a valid sequence number of the symbol in the string (no symbol with the -1 number) and the one who caused this function will understand that the substring is not found.

In general, the refund -1 is a fairly common practice in features that return the position of any object in the ordered list.

Answer 3, Authority 12%

int find_substr (char * s1, char * s2)

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

You need to decide something from the function to get or not.

Let’s say

int iResult = find_substr ("line 1", "page");
if (iResult == - 1) // Search ended in failure
ELSE // Search Successful!

In the Return operand, add the values ​​you need (0, -1, -2, anything int), and then analyze the result in iResult.

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

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions