Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

return statement

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
muktar
Asked:
muktar
  • 8
  • 4
  • 4
  • +3
1 Solution
 
ntdragonCommented:
i think that it's part of the function
it's not an operator
0
 
AndrewRodionovCommented:
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
 
seemamraoCommented:
'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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
AndrewRodionovCommented:
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
 
ozoCommented:
"return" is a keyword
"return;" is a statement
0
 
muktarAuthor Commented:
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
 
muktarAuthor Commented:
Adjusted points from 10 to 15
0
 
AndrewRodionovCommented:
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
 
ozoCommented:
where the return value will be stored before returning is implementation defined
0
 
seemamraoCommented:
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
 
seemamraoCommented:
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
 
seemamraoCommented:
oops! i am sorry for sending twice
0
 
AndrewRodionovCommented:
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
 
AndrewRodionovCommented:
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
 
muktarAuthor Commented:
Thanks for allur answers
I am accepting Andrew's answer ...
0
 
AndrewRodionovCommented:
OK. Reject seemamrao's proposed answer and accept mine ;-)

Andrew
0
 
tarigopulaCommented:
return is the keyword which directs the
control to the position from where the function is called.
0
 
AndrewRodionovCommented:
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
 
muktarAuthor Commented:
Thanks Andrew for informing...
Enjoy ...

Muktar
0
 
AndrewRodionovCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 4
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now