3 Replies - 545 Views - Last Post: 17 October 2012 - 03:32 PM Rate Topic: -----

#1 fsdfsdfsd1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 04-October 12

diff b/n sub and function

Posted 09 October 2012 - 03:43 AM


i know sub cannot return a value but a function can return a value
//trying to convert vb to c++
my doubt is in my vb code there is a sub program,from that subprogram it calls a function....
how these two are related....

Is This A Good Question/Topic? 0
  • +

Replies To: diff b/n sub and function

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: diff b/n sub and function

Posted 09 October 2012 - 03:57 AM

If you are talking C++ the only thing that you can get that doesn't return anything is a void procedure:
void dostuff(char p){
}


Otherwise all the others return values (to my knowledge)
int dostuff(char p){
return <an integer>
}


That is a bit like:
Function doStuff (ByVal p As Char) As Integer
Return <an integer>
End Function


A function can be used so that a large sub procedure can be shortened, with the added advantage of being able to use the function anywhere within its declared scope. A sub can return but only return execution to the calling procedure, without actually returning anything.
Hope this is clearer for you now.
Was This Post Helpful? 1
  • +
  • -

#3 Neku  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 268
  • Joined: 21-May 09

Re: diff b/n sub and function

Posted 10 October 2012 - 04:00 PM

View PostDimitriV, on 09 October 2012 - 01:57 PM, said:

If you are talking C++ the only thing that you can get that doesn't return anything is a void procedure:
void dostuff(char p){
}


Otherwise all the others return values (to my knowledge)
int dostuff(char p){
return <an integer>
}


That is a bit like:
Function doStuff (ByVal p As Char) As Integer
Return <an integer>
End Function


A function can be used so that a large sub procedure can be shortened, with the added advantage of being able to use the function anywhere within its declared scope. A sub can return but only return execution to the calling procedure, without actually returning anything.
Hope this is clearer for you now.



in C function return data unless you state they are void.

in vb6 to get something back from a function you dont use the Return command, you equal the function name to the result you try to give

example:

Public Function Sum(A As Integer, B As Integer) As integer
Dim Result As Integer

Result=A+B
'now the part that actually return the result
Sum=Result
End Function



how to call this fuction:
Note: since its public you can call it from every form you have.

'for this example this code is inside a command button click event
Dim Total As Integer
Total=Sum(5,6)
MsgBox Total 'will pup up a message with the value of vriable Total, in this case 5+6=11


Was This Post Helpful? 0
  • +
  • -

#4 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: diff b/n sub and function

Posted 17 October 2012 - 03:32 PM

I believe you are asking what happens when you have a sub that calls a function. The function returns a value to the sub that calls it. The sub itself still can't return a value to its caller.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1