[programming techniques] subroutines

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/16

flashcard set

Earn XP

Description and Tags

[ BBC Bitesize 'Computational thinking, algorithms and programming ⇢ Programming techniques page 6-7 ]

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

17 Terms

1
New cards

Program

Sequences of instructions for a computer.

2
New cards

Subroutines

A small program that is written within a main program to carry out a specific function.

3
New cards

Code for Defining a Subroutine:

def subroutineName(parameterOne, parameterTwo)

4
New cards

Types of Subroutines:

  • procedures

  • functions

5
New cards

Procedures

A section of computer code that performs a specific task by taking in parameters.

6
New cards

Functions

A section of computer code that performs a specific task and returns a value by taking in parameters.

7
New cards

Benefits of Subroutines:

  • usually small in size → easier to write, test, debug and understand

  • can be saved separately as modules and used again in other programs → this saves time

  • used repeatedly at various points → the code only has to be written once, resulting in shorter programs

8
New cards

Built-in Functions:

  • int - converts strings or floats into integers

  • str - converts a number into a string

  • asc - finds the ASCII number of a character

9
New cards

Libraries

A collection of pre-written, tested functions that extend the functionality of a language. stored in a separate file.

10
New cards

Function Example:

def add(a, b)

result = a + b

return result

sum = add(5, 3)

print(sum)

11
New cards

Local Variables

Variables that are defined within a subroutine and can only be used in said subroutine.

12
New cards

Global Variables

Variables that are defined outside a subroutine but are valid both in and out of subroutines.

13
New cards

Parameters

Variables listed inside the parentheses when defining a subroutine. They act as placeholders for the values that will be passed to the subroutine when it is called.

14
New cards

Arguments

The actual values passed to the subroutine when it is called. They provide the subroutine with the data it needs to operate on.

15
New cards

Code for Random Number Generator (Integer):

import random

randomNumber = random.randint(1,100)

print(randomNumber)

16
New cards

Code for Random Number Generator (Float):

import random

#generates a random float between 0 and 1 (inclusive)

randomFloat = random.uniform(0, 1)

print(randomFloat)

17
New cards

Code for Random Number Generator (Arrays):

import random

items = [1, 2, 3, 4, 5]

#generates a random item from list

randomItem = random.choice(items)

print(randomItem)