Difference between revisions of "Subroutine"

From SEGGER Wiki
Jump to: navigation, search
m (Rolf moved page subroutine to Subroutine)
m
 
Line 1: Line 1:
  +
[[Category:Knowledge Base]]
 
A subroutine is a piece of program code that is designed to be called by a program. It is not a stand-alone application, so it is not usable in itself.
 
A subroutine is a piece of program code that is designed to be called by a program. It is not a stand-alone application, so it is not usable in itself.
 
I an real world application, the bulk of the program consists of subroutines.
 
I an real world application, the bulk of the program consists of subroutines.

Latest revision as of 15:20, 24 June 2019

A subroutine is a piece of program code that is designed to be called by a program. It is not a stand-alone application, so it is not usable in itself. I an real world application, the bulk of the program consists of subroutines. A subroutine can take parameters and return a value.

Let's look at a simple example of a subroutine:

int Max(int a, int b)
  if (a > b) {
    return a;
  }
  return b;
}

How does it work in practice?

First of all, most architectures have a call instruction. "Call" is the logical name. Unfortunately, it is not always called that way. Some other names are

  • JSR (Jump SubRoutine, on 6202 and related architectures)
  • BL or BLX (Branch linked or Branch Linked with Mode switch) on ARM architectures

A call instruction transfers control to an other subroutine by loading the address of the first instruction of the subroutine into the PC memorizing the current PC (the address of the next instruction to execute). Memorizing the return address can happen in 2 ways basically: By saving it on the stack or in a dedicated register, typically called RA (Return Address register) or LR (Link register, ARM architectures) In most architectures, there is also a special instruction to return from the sub routine call to the caller. This instruction is typically called

  • RET (Return)
  • RTS (Return from subroutine)
  • BX LR (Branch with mode change to Link register) on ARM architectures

Inlining

Inlining is a technique used by compilers. A known subroutine can be made part of the calling function (the caller). This is always a speed optimization, as the overhead of the call can be eliminated. If the subroutine is called only in one place, it is usually also a size optimization. If the routine is big and called multiple times, the cost of inlining in terms of addition program size is usually too high. The compiler strategy can be selected for most compilers via command line switch. Typically, if a routine is static, the compiler knows that it is aware of all uses (calls) of the subroutine, which encourages inlining. There are also different ways to force inlining of a routine.

FAQs

What is the difference between a function and a subroutine?

The simple answer is: It is the same. Some people will disagree to that, saying that a function operates on local values (the parameters) only, that a function has to return a value. That would mean every function is a subroutine, but not every subroutine is a function. In any case, let's just assume they are the same and it is not worth arguing about.