Difference between revisions of "Subroutines - Functions"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
m
Line 1: Line 1:
=Subroutines=
+
A set of instructions designed to perform a frequently used operation within a program. This makes it easy to repeat code. A subroutine must be contained in a class, and should the class not be the same as the one calling it, it should be referred to directly or by instantiation.
A set of instructions designed to perform a frequently used operation within a program. This makes it easy to repeat code.
 
  
=== Example ===
+
<h3>Example</h3>
 
[[File:Sub.PNG]]
 
[[File:Sub.PNG]]
  
 
Every time greeting() is written the program runs the greeting subroutine.
 
Every time greeting() is written the program runs the greeting subroutine.
 +
 +
<h2>Passing Variables</h2>
 +
Variables may need to be passed across subroutines in order for them to execute correctly.<br />
 +
<h3>Example</h3>
 +
[[File:passing variables.png]]
 +
 +
<h2>Data Type Subroutines</h2>
 +
A subroutine may need to return a variable type to assign to a variable when called. This can be done using any data type.<br />
 +
<h3>Example</h3>
 +
[[File:data passing.png]]

Revision as of 23:11, 15 December 2016

A set of instructions designed to perform a frequently used operation within a program. This makes it easy to repeat code. A subroutine must be contained in a class, and should the class not be the same as the one calling it, it should be referred to directly or by instantiation.

Example

File:Sub.PNG

Every time greeting() is written the program runs the greeting subroutine.

Passing Variables

Variables may need to be passed across subroutines in order for them to execute correctly.

Example

File:Passing variables.png

Data Type Subroutines

A subroutine may need to return a variable type to assign to a variable when called. This can be done using any data type.

Example

File:Data passing.png