Difference between revisions of "Subroutines - Python"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Default Parameters)
(Declaring a subroutine)
 
(One intermediate revision by the same user not shown)
Line 17: Line 17:
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 
def answer(person):
 
def answer(person):
     if person="Wayne":
+
     if person=="Wayne":
 
           print("yes")
 
           print("yes")
 
     else:
 
     else:
Line 60: Line 60:
  
 
the function is called by my_function(3) this will be replaced with the value that is returned by the function. so this line will essentially be print(15). The function is passed a 3, this is multiplied by 5 to get 15 and then returned. A function can return any type of data, but it can only return a single value.
 
the function is called by my_function(3) this will be replaced with the value that is returned by the function. so this line will essentially be print(15). The function is passed a 3, this is multiplied by 5 to get 15 and then returned. A function can return any type of data, but it can only return a single value.
 
=Default Parameters=
 
You can specify a default value for a parameter, this will be used if no parameter is passed. For example:
 
 
<syntaxhighlight lang=python>
 
def my_function(country = "Norway"):
 
  print("I am from " + country)
 
 
my_function("Sweden")
 
my_function("India")
 
my_function()
 
my_function("Brazil")
 
</syntaxhighlight>
 

Latest revision as of 13:29, 6 April 2019

What is a subroutine & Why do we have them

A subroutine is essentially defining a name for a block of code. This code can then be used elsewhere just by calling it. For example, if you think of one of your favourite songs it is likely to have several verses and a repeating chorus. A subroutine could be used to define what the chorus is, so we could now replace the lines of the actual chorus with just chorus.

Declaring a subroutine

Python uses the def command to declare a function, you must supply the name of the routine and any parameters accepted by the routine ie the brackets () . So for example:

def chorus():
     print("chorus line 1")
     print("chorus line 2")
     print("chorus line 3")
     print("chorus line 4")

you can use parameters, for example:

def answer(person):
     if person=="Wayne":
          print("yes")
     else:
          print("no")

Calling a subroutine

With your subroutine declared you can then call it anywhere within your program, this call must also provide the parameters required for the routine ie inside the brackets () . For example using the chorus subroutine above:

print(verse 1 line 1)
print(verse 1 line 2)
print(verse 1 line 3)
print(verse 1 line 4)
chorus()
print(verse 2 line 1)
print(verse 2 line 2)
print(verse 2 line 3)
print(verse 2 line 4)

alternatively if you are using parameters:

name = input("please enter your name")
print("do I like you???")
answer(name)

Procedure vs Function

There are 2 types of subroutine. A procedure is essentially what we have looked at above, you can pass parameters into a procedure and a procedure is a name for a block of code. A function however allows all of this but can also return a value.

Example Function

def my_function(x):
  return 5 * x

print(my_function(3))

the function is called by my_function(3) this will be replaced with the value that is returned by the function. so this line will essentially be print(15). The function is passed a 3, this is multiplied by 5 to get 15 and then returned. A function can return any type of data, but it can only return a single value.