A-level Computing/AQA/Paper 1/Fundamentals of programming/Subroutines (functions and procedures)

To save you rewriting lots of code again and again you might use a sub routine, there are two types: Procedures and Functions. For example in a program you wanted to know today's date, instead of having to write a separate sub routine to calculate the date each time you wanted to work it out, you would probably use codice_1. This is a function, when you call it, it returns a value. It was written by someone else and you can keep reusing it as many times as you want. Any program written in industry will use sub routines calling things like: codice_2 . Procedures and Functions allow for you to:
An easy way to tell the difference between a Procedure and a Function is to look at the names:
The difference between functions and procedures is a little more complex than this, but the above works as a rule of thumb
Declarations.
Declarations are where you state the name of your procedure/function and the code that you want to execute. Even if you declare a procedure/function, it doesn't mean that the code will run, you need a "Call" to actually get the code to execute.
Sub printNumber()
 console.writeline(number1)
End Sub
Functions are slightly different, as they return values you must include a return function in their declaration. And you must specify the datatype of the value being returned, in the case below that is an Inteteger specified by: codice_4
Function printNumber() as Integer
 return number1
End Function
Calls.
Calls allow you to run the code declared in a procedure/function. You can build up all sorts of programming structures by making Calls part of the code. Remember that Functions are fun, so you should be doing something with the returned value.
printNumber() ' a procedure call
console.writeline(printNumber()) ' a function call
dim x = MaximumSpeed() ' another function call
Parameters (and Arguments).
Parameters allow you to pass values to the procedures and functions that you declare, you can pass all sorts of data as parameters and you can pass as many as you like.
 Parameters are often called Arguments. Technically, they are very similar but not the same thing. When you describe a procedure or a function, and identify the data it can accept, each thing is a Parameter. When you later use the procedure or function within a program, and provide it with an actual piece of data (or multiple pieces), those things are called Arguments. In practice, the terms are often used interchangeably (c.f. StackOverflow: What's the difference between an argument and a parameter?)
'declaration
Sub printNumber(number1 as integer) 'one parameter
 console.writeline(number1)
End Sub
'call
printNumber(4)
The output would be:
4
'declaration
Sub printNameAge(name as string, age as integer) 'two parameters
 console.writeline(name &amp; " is " &amp; age &amp; " years old")
End Sub
'call
printNameAge("Mounir", 17)
The output would be:
Mounir is 17 years old
'declaration
function squareNumber(number1 as integer) as integer 'one parameter
 return (number1 * number1)
End Function
'call
console.writeline(squareNumber(4))
Note that as it's a function, we had to include the call in an equation. It returns a value, it can't sit on its own. The output would be:
16
What is the difference between a function and procedure?
Functions return values, Procedures may or may not return a value.
Why would you use subroutines (functions and procedures) in your code?
Write a function declaration with the identifier of Avg that will accept 3 numbers (num1, num2, num3) and return the average:
Function Avg(num1, num2, num3)
 return (num1 + num2 + num3) / 3
End Function
For the above function, write code with a function call to work out the average of three numbers input by a user:
num1 = 4
num2 = 7
num3 = 10
Average = 7
dim a, b, c as integer
console.write("input num1 = ")
a = console.readline()
console.write("input num2 = ")
b = console.readline()
console.write("input num3 = ")
c = console.readline()
console.write("Average = " &amp; Avg(a,b,c))
Sub nameTimes(name, num)
 for x = 1 to num
 console.writeline(name)
 next
End Sub
For the procedure above list:
ByRef.
The parameter that you are passing to a procedure or function is referred to. That means you are pointing at it, you are going to directly change its value and anything that happens to it within the procedure or function will change the original value.
Dim number1 as integer = 123
Sub Main()
 console.writeline(number1)
 IncPrintNumber(number1)
 console.writeline(number1)
End Sub
Sub IncPrintNumber(ByRef num as integer)
 num = num + 1
 console.writeline(num)
End Sub
The output would be:
123
124
124
ByVal.
The parameter that you are passing to a procedure or function is copied. That means you are taking a copy of the original value put into the procedure or function call. Anything that happens to it within the procedure or function will NOT change the original value.
Dim number1 as integer = 123
Sub Main()
 console.writeline(number1)
 IncPrintNumber(number1)
 console.writeline(number1)
End Sub
Sub IncPrintNumber(ByVal num as integer)
 num = num + 1
 console.writeline(num)
End Sub
This saves a local variable of the number1 value, storing it in num, it is only valid inside the IncPrintNumber sub routine
The output would be:
123
124
123
Sub Main()
 dim a as integer = 7
 dim b as integer = 8
 Add(a,b)
 console.writeline(a)
End Sub
Sub Add(ByXXX num1 as integer, ByXXX num2 as integer)
 num1 = num1 + num2
End Sub
What is the output of the above code when codice_5?
7
What is the output of the above code when codice_6?
15
Sub Swap(ByRef p as string, ByVal q as string)
 dim temp as string
 temp = p
 p = q
 q = temp
 console.writeline(p &amp; " - " &amp; q)
End Sub
Sub Main()
 dim s1 as string = "hello"
 dim s2 as string = "goodbye"
 Swap(s2, s1)
 console.writeline(s2 &amp; " - " &amp; s1)
End Sub
What is the output of the above code?
hello - goodbye
hello - hello
What is the difference between a parameter passed by Value and a parameter passed by Reference?
A parameter passed by value copies the value of the parameter passed into the sub routine. Any changes made to this value do not impact on the original value.
A parameter passed by Reference passes a link to a variable. Any changes made to the parameter in the sub routine change the original variable.