Week 2 - Structured Programs Flashcards
What are procedures and functions?
They are ways of grouping lines of code (blocks) and giving the block a name.
What can functions and procedures take?
They both take arguments.
How are functions and procedures different?
Functions have one return value of a single type & side-effects, whereas procedures only produce side-effects.
Arguments
Data that is passed from one block of code to another (the thing that goes into the brackets of a procedure/function).
The argument is given a name (like a variable name) in the receiving procedure.
Why are procedures called?
Because they produce side-effects.
What are examples of side-effects?
A side-effect to a procedure can include calling it inside another function, or outputting data to the screen or terminal
How are global variables marked?
With a $ sign.
Eg: $a
Methods
An OOP term that means both functions and procedures
method()
Attributes
Variables and arguments, but also functions or methods that might be used to give values as though you were accessing a variable.
These are used to define a class, where a class is a new data type that we make. Eg: Attribute like id in HTML to later call onto it.
What are classes used for?
Used for creating custom data types.
What are the possible scopes in Ruby?
Global, Local & Instance
Global Scope
A variable/constant that can be accessed anywhere in the program.
Local Scope
A variable/constant that’s only accessible in the block of code where declared
Instance Scope (@Shared)
A variable accessible to all procedures and functions under a class.
What is debugging code?
Running code and if there are errors, you’re finding them and resolving them.
What data type is on the output screen?
String
What’s the difference between a parameter and an argument?
Parameter is the code in the brackets when defining a method.
Eg: method(n)
The argument is the value that we pass in place of the parameter/placeholder.
Eg: method(12) will substitute n = 12 and put that value into the function called method.
What’s a splat argument? How do we mark it?
Splat arguments tell the program that the method can receive one or more arguments.
They are arguments followed a *.
How do you write if a number (m) is divisible by another number (n) in Ruby?
m % n == 0
How are blocks different from methods?
Blocks are like nameless methods. They often start with do.