Use return statement to exit a function - C Function

C examples for Function:Function Return

Description

Use return statement to exit a function

Demo Code

#include <stdio.h>

int find_substr(char *s1, char *s2);

int main(void)
{
   if (find_substr("C is fun", "is") != -1)
      printf("Substring is found.");

   return 0;//  w w  w  . ja  va2  s.  c  o  m
}

/* Return index of first match of s2 in s1. */
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; /* 1st return */

   return -1; /* 2nd return */
}

Related Tutorials