- Foundation
- About 13 min
- 5 practice questions
Functions, errors and ways of programming
Understand functions, syntax versus logic errors, debugging and the main programming styles.
If something looks wrong, please tell us.
As programs grow, they are split into parts and checked for mistakes. This lesson covers functions, errors and common ways of organising code.
Functions
A function (procedure or method) is a named block of code that does one job and can be used again and again. It may take parameters (inputs) and return a value. Functions avoid repetition and make programs easier to read and fix.
A function square(n) that returns n x n gives 49 for square(7) and 100 for square(10).
Types of errors
| Error | Meaning | Example |
|---|---|---|
| Syntax error | Breaking the rules of the language; the program will not run | A missing bracket |
| Runtime error | Occurs while the program runs | Dividing by zero |
| Logic error | Program runs but gives wrong results | Using + instead of x |
Debugging is finding and fixing errors. A bug is an error; a compiler or interpreter reports syntax errors, but logic errors need careful testing.
Programming styles
- Procedural: a program is a list of procedures run in order, as in C.
- Object-oriented (OOP): a program is built from objects combining data and actions; ideas include class, object, inheritance and encapsulation.
- Scripting: short programs that automate tasks, often run by an interpreter.
Worked examples
Example 1. A program to find the area of a rectangle multiplies by 2 instead of the breadth. It runs but gives wrong answers. This is a logic error.
Example 2. A program divides by a number that the user enters as 0. It stops while running. This is a runtime error.
Example 3. In OOP, a class is a blueprint and an object is one thing made from it. If Car is the class, your own car is an object.
Syntax error = grammar mistake. Runtime error = fails while running. Logic error = wrong answer.
Key takeaways
- Functions are reusable named blocks.
- Know the three error types.
- OOP uses classes and objects.
Check your understanding
-
1. A program has a missing bracket and will not run. Which error is this?
- Logic error
- Runtime error
- Syntax error
- No error
Show answer
Answer: C. Breaking the rules of the language, such as a missing bracket, is a syntax error.
-
2. A user enters 0 and the program stops while running because it divides by that number. Which error is this?
- Syntax error
- Logic error
- Runtime error
- Compiler error
Show answer
Answer: C. An error that occurs while the program is running, such as dividing by zero, is a runtime error.
-
3. A program meant to find a rectangle's area multiplies the length by 2 instead of the breadth. It runs but gives wrong answers. Which error is this?
- Logic error
- Syntax error
- Runtime error
- Hardware fault
Show answer
Answer: A. The program runs but gives wrong results, so it is a logic error.
-
4. A function square(n) returns n x n. What is square(6)?
- 36
- 12
- 66
- 6
Show answer
Answer: A. square(6) = 6 x 6 = 36.
-
5. In object-oriented programming, if Car is the class, your own car is
- A class
- A function
- A loop
- An object
Show answer
Answer: D. A class is a blueprint and an object is one thing made from it, so your own car is an object of the class Car.
Sign in to keep track of the lessons you finish.