free 8051 Microcontroller Projects AVR PIC Microcontroller Projects Tutorials Ebooks Libraries, interfacing tutorials, lcd tutorial, stepper motor, dc motor 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems

 
8051 microcontroller 8051 microcontroller
Forums

Moderators: Ajay, Junied , abbas1707, Arun Kumar V, pdi33, Shailesh NAYAK, ۞ TPS ۞, shyam, sashijoseph
Author Post
nismo
Mon Aug 11 2008, 08:49AM
 User Offline
Registered Member #9479
Joined: Thu Jul 31 2008, 09:03AM

Posts: 77
Thanked 1 time in 1 posts
Does anyone knows what does return 1 does?

Back to top


shyam
Mon Aug 11 2008, 12:03PM

 User Offline

Registered Member #2984
Joined: Mon Aug 06 2007, 11:33AM

Posts: 725
Thanked 108 times in 104 posts
suppose u have a fun say add


you can use return as follows..
CODE:

int add(void);
main()
{
printf("sum of 2 and 3 is %d ",add());
}
int add(void)
{
return 5;
}



thus we can say return 1 returns a 1

[ Edited Wed Aug 13 2008, 03:31AM ]

lProgress is not made by early risers or hard workers, but by LAZY people, trying to find easier ways to do the same........
Back to top


Ajay
Tue Aug 12 2008, 04:31AM
Rickey's World Admin

 User Offline

Registered Member #1
Joined: Fri Feb 24 2006, 04:56AM

Posts: 4042
Thanked 753 times in 712 posts
In programming language, return 0 and return 1 are used to return the status of function.

return 0 <- return false
return 1 <- return true

ex.

CODE:
int greater (int a, int b){
   if(a>b)
      return 1;
   return 0;
}

void main(){
    if(greater(3,2)){
         printf("its greater");
     } else
         printf("its smaller");
}
 


www.rickeyworld.info
If you feel satisfied with the user's forum reply please click on the thank button.

Obey forum rules!
Respect others!
Back to top



This post has been thanked 1 time
 nismo 
say2paul
Tue Aug 12 2008, 04:54AM
 User Offline
Registered Member #8461
Joined: Tue Jun 17 2008, 10:56AM

Posts: 123
Thanked 6 times in 6 posts
The return statement

A return statement ends the processing of the current function and returns control to the caller of the function.

CODE:

>>-return--+--------------------------+--;---------------------><
           '-+---+--expression--+---+-'
             '-(-'              '-)-'
 


A value-returning function should include a return statement, containing an expression. C only If an expression is not given on a return statement in a function declared with a non-void return type, the compiler issues a warning message. C++ If an expression is not given on a return statement in a function declared with a non-void return type, the compiler issues an error message.

If the data type of the expression is different from the function return type, conversion of the return value takes place as if the value of the expression were assigned to an object with the same function return type.

For a function of return type void, a return statement is not strictly necessary. If the end of such a function is reached without encountering a return statement, control is passed to the caller as if a return statement without an expression were encountered. In other words, an implicit return takes place upon completion of the final statement, and control automatically returns to the calling function. C++ If a return statement is used, it must not contain an expression.
Examples of return statements

The following are examples of return statements:

CODE:

return;            /* Returns no value            */
return result;     /* Returns the value of result */
return 1;          /* Returns the value 1         */
return (x * x);    /* Returns the value of x * x  */
 


The following function searches through an array of integers to determine if a match exists for the variable number. If a match exists, the function match returns the value of i. If a match does not exist, the function match returns the value -1 (negative one).

CODE:

int match(int number, int array[ ], int n)
{
   int i;

   for (i = 0; i < n; i++)
      if (number == array[i])
         return (i);
   return(-1);
}
 


A function can contain multiple return statements. For example:

CODE:

void copy( int *a, int *b, int c)
{
   /* Copy array a into b, assuming both arrays are the same size */

   if (!a || !b)       /* if either pointer is 0, return */
      return;

   if (a == b)         /* if both parameters refer */
      return;          /*    to same array, return */

   if (c == 0)         /* nothing to copy */
      return;

   for (int i = 0; i < c; ++i;) /* do the copying */
      b[i] = a[1];
                       /* implicit return */
}
 


In this example, the return statement is used to cause a premature termination of the function, similar to a break statement.

An expression appearing in a return statement is converted to the return type of the function in which the statement appears. If no implicit conversion is possible, the return statement is invalid.


Source: XL C/C++ V8.0 IBM
Back to top



This post has been thanked 1 time
 nismo 
pdi33
Tue Aug 12 2008, 06:42AM

 User Offline
Registered Member #1329
Joined: Mon Jun 04 2007, 09:28AM

Posts: 770
Thanked 185 times in 180 posts
Another interesting application of the return function is when it is used in the main() function. Here the return (value) returns the value to the operating system which is used by the operating system for its own use (especially when unix operating system is used.)
but this feature is not applicable in our real time embedded system where the control never reaches the return statement.



* inspired to develop,developing to inspire *
Back to top


nismo
Tue Aug 12 2008, 09:17AM
 User Offline
Registered Member #9479
Joined: Thu Jul 31 2008, 09:03AM

Posts: 77
Thanked 1 time in 1 posts
very clear no wonder my lecturer told me India has a lot of good programmers.. Thanks

[ Edited Tue Aug 12 2008, 09:19AM ]
Back to top


nismo
Tue Aug 12 2008, 09:27AM
 User Offline
Registered Member #9479
Joined: Thu Jul 31 2008, 09:03AM

Posts: 77
Thanked 1 time in 1 posts
void copy( int *a, int *b, int c)
{
/* Copy array a into b, assuming both arrays are the same size */

if (!a || !b) /* if either pointer is 0, return */
return;

if (a == b) /* if both parameters refer */
return; /* to same array, return */

if (c == 0) /* nothing to copy */
return;

for (int i = 0; i < c; ++i;) /* do the copying */
b[i] = a[1];
/* implicit return */
}


Yoyo, Mr say2paul... what does the return does in this routine? Does it mean return to the main function?
Back to top


pdi33
Tue Aug 12 2008, 12:01PM

 User Offline
Registered Member #1329
Joined: Mon Jun 04 2007, 09:28AM

Posts: 770
Thanked 185 times in 180 posts
hi nismo,
well, if u have called the function copy ( ) from the main function, then it will return back to the main function. but if the function was called by another function say abc() then the control will return to that function.

case1: calling from main function

main()
{
.
copy (....);
. <------- control will return here
.
}


case2: calling from another function

main()
{
.
.
abc(...);
.
.
}

abc()
{
.
.
copy();
. <------ control will return here
.
}



* inspired to develop,developing to inspire *
Back to top


 

Jump:     Back to top

Syndicate this thread: rss 0.92 Syndicate this thread: rss 2.0 Syndicate this thread: RDF
Powered by e107 Forum System

8051 Microcontroller Projects 8051 AVR tutorials PIC microcontroller, 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems