C - Returning early from a function

Introduction

The return keyword can stop running a function at any time, sending execution back to the statement that called the function.

In the case of the main() function, return exits the program.

The following code uses return to exit a Function.

Demo

#include <stdio.h> 

void display(int stop); 

int main() //from w  ww .ja  va2  s .c om
{ 
   int s; 

   printf("Enter a stopping value (0-100): "); 
   scanf("%d",&s); 
   display(s); 
   return(0); 
} 

void display(int stop) 
{ 
   int x; 

   for(x=0;x<=100;x=x+1) 
   { 
       printf("%d ",x); 
       if(x==stop) 
       { 
           puts("You won!"); 
           return; 
       } 
   } 
   puts("I won!"); 
}

Result

Related Topic