Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

sub... sub... sub routine

Status
Not open for further replies.

eleynika

Junior Member level 1
Joined
Jul 13, 2006
Messages
19
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Location
Nigeria
Activity points
1,388
hello guys, can i place sub routine inside another sub routine? if so, what is the limit to the number of sub routines i can place inside another
 

Generally yes. You are limited by your stack size.
Each time you call another level of subroutine, all the register data from the current subroutine is pushed onto the stack. If you do this too many times you will get the 'stack overflow' error or your system will crash.
 

eleynika said:
what is the limit to the number of sub routines i can place inside another
It is called "nesting" when you call a subroutine from another subroutine. When your top-level program calls a subroutine, the call stack is said to be "nested" one level deep.

You can call as many subroutines as you want from within a top-level subroutine. Let's say, for example, you call subroutine A. A can then call B, waits for it to return and then calls C, and so on. In this case you are never "nested" more than two levels deep, so you don't need to worry about stack overflow.

A different question is this: "how many levels deep can I nest subroutines?" For example, A calls B, and B calls C, and so on. As newelltech indicated, the maximum nesting level depends on your stack size. If you are programming in assembly language, the program counter is stacked when you call a subroutine. If you are programming in C/C++ or another high-level language, various registers plus all local non-static variables belonging to the calling subroutine are stored on the stack. In either case, stack space is consumed when you call a subroutine. The maximum nesting depth therefore depends on the stack size and the amount of stack space that is consumed by the nested calls.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top