Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

differents bt function and subroutine

Former Member
0 Likes
1,143

hi gem's

i want 2 know wether the form return's a value r not......

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,033

Hi,

Differences between subroutine and FM r as follows:

Function : Stored in the central library and has a global presence.

Subroutine : It is confined to a particular program.

Function : It can return values.

Subroutine : It cannot return values.

Function : Can handle exceptions.

Subroutine : Can't handle exceptions.

Function : The way the parameters are passed will differ from that subroutine.

Thanks,

Reward If Helpful.

8 REPLIES 8
Read only

Former Member
0 Likes
1,033

Hi,

Form can also return a value..using the CHANGING parameters...

Ex..



REPORT z1.

data: v_count type i.
data: v_output type i.

PERFORM form1 USING v_count
                          CHANGING v_output.

FORM form1 USING rv_input type i
                    CHANGING rv_output type i.


  rv_output = rv_input + 1.

ENDFORM.

Thanks

Naren

Read only

0 Likes
1,033

hi Narendran

it just change the value which locate in the address but it doesnt return any value......

Read only

Former Member
0 Likes
1,034

Hi,

Differences between subroutine and FM r as follows:

Function : Stored in the central library and has a global presence.

Subroutine : It is confined to a particular program.

Function : It can return values.

Subroutine : It cannot return values.

Function : Can handle exceptions.

Subroutine : Can't handle exceptions.

Function : The way the parameters are passed will differ from that subroutine.

Thanks,

Reward If Helpful.

Read only

0 Likes
1,033

hi viji u specify that subroutine never return value....but what abt call -by-val-and-result,

Read only

0 Likes
1,033

i want 2 know wether it return a value r not....................bcoz some 1 said it returns,,,,,,,,

Read only

Former Member
0 Likes
1,033

Hi,

Please post the FORM declaration of the parameters

Thanks

Naren

Read only

Former Member
0 Likes
1,033

hi,

1)

Functions should behave like mathematical functions. That is, they should take arguments and compute and return a result without any side effects. They should not perform I/O and they should not change the values of their actual arguments. (Fortran won't stop you from writing a function that causes side effects, but it is poor form nonetheless.)

Subroutines should take arguments and (since they can't return anything) cause side effects. That is, they should perform I/O or change the values of their actual arguments. This is the only way that a subroutine has of communicating with the outside world.

2)

Subroutines

A subroutine takes a number of input parameters but does not return a value. The input parameters are passed by-value, which means their values are copied directly onto the stack when the routine is called. There are no pointers in TEA. A subroutine may have local variables which must also be stored on the stack. The number of input parameters and local variables is limited by the amount of free stack space.

An example subroutine is listed below. It has two input parameters and one local variable. The asm block contains VM instructions .

void foo(int n, char c)

{

int var1;

asm

{

(VM code goes here)

}

}

When executing a subroutine, the input parameters go on the stack first in the order in which they are declared. The call instruction pushes the return address bytes to the stack and branches to the subroutine. Once inside the subroutine, any local variables are stored on the stack in the order in which they are declared. Uninitialized local variables are initialized to "-1" by default. Calling the example routine listed above requires seven stack bytes: three for the input parameters, two for the return address, and two for the local variable. Prior to execution of the instructions in the asm block, the top seven stack btyes will contain:n (high) n (low) c return addr. (high) return addr. (low) var1 (high) var1 (low)

If n=0x0320, c=0x14, and the return address is 0x0120, the values in the top seven stack bytes will be:0x03 0x20 0x14 0x01 0x20 0xFF 0xFF

The TEA compiler creates code that does this parameter passing and local variable allocation automatically. The user may write code within the asm block that accesses the input parameters and local variables. After the subroutine completes its task, the VM will pop the local variables off the stack, branch to the return address, and discard the input parameters. This clean-up process is also automatic.

Functions

A function takes a number of input parameters and returns a value. As with subroutines, the input parameters are passed by-value. Parameter passing and local variable allocation also works the same way. Unlike subroutines, functions require a place holder in the stack for the return value. Furthermore, the compiler requires an explicit return statement for each function. Because of this, the user must declare a local variable to hold a temporary return value and use the TEA return statement to pass the result.

An example function is listed below. It has one input parameter and two local variables. The asm block contains VM instructions .

int foo(char c)

{

int result;

int var1=0;

asm

{

(VM code goes here)

}

return result;

}

When executing a function, the first thing to go on the stack is a place holder for the return value. Calling the example routine listed above requires nine stack bytes: two for the return value place holder, one for the input parameter, two for the return address, and four for the local variables. Prior to execution of the instructions in the asm block, the top nine stack btyes will contain:int (high) int (low) c return addr. (high) return addr. (low) result (high) result (low) var1 (high) var1 (low)

If c=0x24 and the return address is 0x024A, the values in the top nine stack bytes will be:0xFF 0xFF 0x24 0x02 0x4A 0xFF 0xFF 0x00 0x00

The result variable is unitialized so it is set to "-1" by default. The return value place holder is also set to "-1" by default. The var1 variable is initialized to "0". The code in the asm block must write its output to the result variable for the function to work properly. At function completion, the return statement writes the return value into the place holder. Then the VM will pop the local variables off the stack, branch to the return address, and discard the input parameters. The return value will be on top of the stack.

Read only

Former Member
0 Likes
1,033

Hi,

By value and result will work if the subroutine ends successfully..If there is any termination in the subroutine then will not pass back the value

Thanks

Naren