?
Solved

return statement

Posted on 2000-03-14
20
Medium Priority
?
270 Views
Last Modified: 2011-09-20
I would like to know whether the "return" is
 an operator  or
 a funtion      or
 anything else ....
It would be nice if explaination is supported
0
Comment
Question by:muktar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 4
  • +3
20 Comments
 
LVL 1

Expert Comment

by:ntdragon
ID: 2618631
i think that it's part of the function
it's not an operator
0
 
LVL 2

Expert Comment

by:AndrewRodionov
ID: 2618662
Hi!

Yes, the 'return' is a part of function but you can return particular value from function. And this value is evaluated by 'return' statement...

So the 'return' is a dual matter ;-)

Andrew
0
 

Expert Comment

by:seemamrao
ID: 2618718
'return' is a part of a function. But all functions do not have to return.
Eg: functions with a return type void.

'return' is a keyword just like printf or scanf. In a C editor, you can find that when 'return' is typed, it gets highlighted( unless you haven't changed the settings).

Functions with a return type other than void usually return a value.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:AndrewRodionov
ID: 2618727
Hi, seemamrao!

Do you really think that 'printf' or 'scanf' are the keywords?! I can write my own function with name 'printf' and use it instead of the standard function. But I can not write my own 'return' function because 'return' is the keyword really.

Andrew
0
 
LVL 84

Expert Comment

by:ozo
ID: 2618753
"return" is a keyword
"return;" is a statement
0
 

Author Comment

by:muktar
ID: 2618830
Adding to the question above

I would like to one more thing, where will the return
value stored before returning


> I would like to know whether the "return" is
>           an operator  or
>           a funtion      or
>            anything else ....
> It would be nice if explaination is supported
0
 

Author Comment

by:muktar
ID: 2618837
Adjusted points from 10 to 15
0
 
LVL 2

Expert Comment

by:AndrewRodionov
ID: 2618858
It's a platform/compiler dependent.

For example, for an integer return value on x86 processors in DOS it will be AX register and in Win32 -- EAX register.

Andrew
0
 
LVL 84

Expert Comment

by:ozo
ID: 2618863
where the return value will be stored before returning is implementation defined
0
 

Expert Comment

by:seemamrao
ID: 2618971
The value returned by 'return' is stored in the stack segment pointed by pointer BP.

Consider these statements:

r=func(x,y);
return r;

r = func(x,y); will cause 'r' to be stored in the stack register pointed by bp.

return r;
In assembly, the above statement can be translated as: ret - which is an instruction. So 'ret' will obtain the value from stack pointed by bp (ss:bp)
and not from ax or eax registers.

-:) I agree we can write our own functions for printf, scanf
0
 

Expert Comment

by:seemamrao
ID: 2618975
The value returned by 'return' is stored in the stack segment pointed by pointer BP.

Consider these statements:

r=func(x,y);
return r;

r = func(x,y); will cause 'r' to be stored in the stack register pointed by bp.

return r;
In assembly, the above statement can be translated as: ret - which is an instruction. So 'ret' will obtain the value from stack pointed by bp (ss:bp)
and not from ax or eax registers.

-:) I agree we can write our own functions for printf, scanf
0
 

Expert Comment

by:seemamrao
ID: 2619019
oops! i am sorry for sending twice
0
 
LVL 2

Accepted Solution

by:
AndrewRodionov earned 45 total points
ID: 2619083
OK, seemamrao.
Look

file tst.c
-----------

int func( int x, int y );

int main()
{
  return func( 1, 2 );
}

int func( int x, int y )
{
  return x + y;
}

After executing bcc -S -O tst.c

file tst.asm
------------

      ifndef      ??version
?debug      macro
      endm
publicdll macro      name
      public      name
      endm
$comm      macro      name,dist,size,count
      comm      dist name:BYTE:count*size
      endm
      else
$comm      macro      name,dist,size,count
      comm      dist name[size]:BYTE:count
      endm
      endif
      ?debug      V 300h
      ?debug      S "tst.c"
      ?debug      C E927656F28057473742E63
_TEXT      segment byte public 'CODE'
_TEXT      ends
DGROUP      group      _DATA,_BSS
      assume      cs:_TEXT,ds:DGROUP
_DATA      segment word public 'DATA'
d@      label      byte
d@w      label      word
_DATA      ends
_BSS      segment word public 'BSS'
b@      label      byte
b@w      label      word
_BSS      ends
_TEXT      segment byte public 'CODE'
   ;      
   ;      int main()
   ;      
      assume      cs:_TEXT
_main      proc      near
      push      bp
      mov      bp,sp
   ;      
   ;      {
   ;        return func( 1, 2 );
   ;      
      mov      ax,2
      push      ax
      mov      ax,1
      push      ax
      call      near ptr _func
      pop      cx
      pop      cx
   ;      
   ;      }
   ;      
      pop      bp
      ret      
_main      endp
   ;      
   ;      int func( int x, int y )
   ;      
      assume      cs:_TEXT
_func      proc      near
      push      bp
      mov      bp,sp
   ;      
   ;      {
   ;        return x + y;
   ;      
      mov      ax,word ptr [bp+4]
      add      ax,word ptr [bp+6]
   ;      
   ;      }
   ;      
      pop      bp
      ret      
_func      endp
      ?debug      C E9
      ?debug      C FA00000000
_TEXT      ends
_DATA      segment word public 'DATA'
s@      label      byte
_DATA      ends
_TEXT      segment byte public 'CODE'
_TEXT      ends
      public      _main
      public      _func
_s@      equ      s@
      end

You can see how the expression 'return x + y;' is tranlated.

But once more, it is platform/compiler dependent. My example is for Borland C 3.x and DOS real mode.

Andrew
0
 
LVL 2

Expert Comment

by:AndrewRodionov
ID: 2619114
seemamrao, I've reread your comment and understood what you mean. You are talking about a function *return* not about a return *value*. But muktar was asking about the latter, right?..

Andrew
0
 

Author Comment

by:muktar
ID: 2619444
Thanks for allur answers
I am accepting Andrew's answer ...
0
 
LVL 2

Expert Comment

by:AndrewRodionov
ID: 2619641
OK. Reject seemamrao's proposed answer and accept mine ;-)

Andrew
0
 

Expert Comment

by:tarigopula
ID: 2633130
return is the keyword which directs the
control to the position from where the function is called.
0
 
LVL 2

Expert Comment

by:AndrewRodionov
ID: 2647717
Hi, muktar!

Do you know the rules? Your question is locked until you evaluate the answer. To accept my answer you should reject seemamrao's answer first and then accept my one.

Andrew


0
 

Author Comment

by:muktar
ID: 2652009
Thanks Andrew for informing...
Enjoy ...

Muktar
0
 
LVL 2

Expert Comment

by:AndrewRodionov
ID: 2652059
Hi Muktar,

Does you speak in a mocking tone? All I want is the regulation maintenance.
And sorry if I've mistaken...

Andrew
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question