Discussion in "Software" started by    nismo    Aug 11, 2008.
Mon Aug 11 2008, 09:19 pm
#1
Does anyone knows what does return 1 does?
Tue Aug 12 2008, 12:33 am
#2
suppose u have a fun say add


you can use return as follows..
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, 04:01 pm ]
Tue Aug 12 2008, 05:01 pm
#3
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.

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");
}
 nismo like this.
Tue Aug 12 2008, 05:24 pm
#4
The return statement

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

>
>
-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:

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).

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:

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
 nismo like this.
Tue Aug 12 2008, 07:12 pm
#5
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.

Tue Aug 12 2008, 09:47 pm
#6
very clear no wonder my lecturer told me India has a lot of good programmers.. Thanks


[ Edited Tue Aug 12 2008, 09:49 pm ]
Tue Aug 12 2008, 09:57 pm
#7
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?
Wed Aug 13 2008, 12:31 am
#8
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
.
}

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

AntoniaRoons
Fri Apr 19 2024, 09:59 pm
carpinteyrowrl
Fri Apr 19 2024, 02:51 pm
DonaldJAX
Fri Apr 19 2024, 01:08 pm
Lewisuhakeply
Thu Apr 18 2024, 06:00 pm
Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am
DonaldKnown
Thu Apr 18 2024, 12:24 am