• 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

Errors
ErrorMeaningExample
Syntax errorBreaking the rules of the language; the program will not runA missing bracket
Runtime errorOccurs while the program runsDividing by zero
Logic errorProgram runs but gives wrong resultsUsing + 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.

— Remember

Key takeaways

  • Functions are reusable named blocks.
  • Know the three error types.
  • OOP uses classes and objects.

Check your understanding

  1. 1. A program has a missing bracket and will not run. Which error is this?

    1. Logic error
    2. Runtime error
    3. Syntax error
    4. No error
    Show answer

    Answer: C. Breaking the rules of the language, such as a missing bracket, is a syntax error.

  2. 2. A user enters 0 and the program stops while running because it divides by that number. Which error is this?

    1. Syntax error
    2. Logic error
    3. Runtime error
    4. 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. 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?

    1. Logic error
    2. Syntax error
    3. Runtime error
    4. Hardware fault
    Show answer

    Answer: A. The program runs but gives wrong results, so it is a logic error.

  4. 4. A function square(n) returns n x n. What is square(6)?

    1. 36
    2. 12
    3. 66
    4. 6
    Show answer

    Answer: A. square(6) = 6 x 6 = 36.

  5. 5. In object-oriented programming, if Car is the class, your own car is

    1. A class
    2. A function
    3. A loop
    4. 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.

Privacy · Terms · Refunds · Delivery