Solved

return statement

Posted on 2000-03-14
20
224 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
  • 8
  • 4
  • 4
  • +3
20 Comments
 
LVL 1

Expert Comment

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

Expert Comment

by:AndrewRodionov
Comment Utility
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
Comment Utility
'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
 
LVL 2

Expert Comment

by:AndrewRodionov
Comment Utility
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
Comment Utility
"return" is a keyword
"return;" is a statement
0
 

Author Comment

by:muktar
Comment Utility
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
Comment Utility
Adjusted points from 10 to 15
0
 
LVL 2

Expert Comment

by:AndrewRodionov
Comment Utility
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
Comment Utility
where the return value will be stored before returning is implementation defined
0
 

Expert Comment

by:seemamrao
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:seemamrao
Comment Utility
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
Comment Utility
oops! i am sorry for sending twice
0
 
LVL 2

Accepted Solution

by:
AndrewRodionov earned 15 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for allur answers
I am accepting Andrew's answer ...
0
 
LVL 2

Expert Comment

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

Andrew
0
 

Expert Comment

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

Expert Comment

by:AndrewRodionov
Comment Utility
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
Comment Utility
Thanks Andrew for informing...
Enjoy ...

Muktar
0
 
LVL 2

Expert Comment

by:AndrewRodionov
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now