05
-
Topic 5: Functions & Program Design
- In this lab we finish off developing the use of functions to allow programs to be broken down into functional elements (hence the name functions) and also look at the debugging tools that can help you to fault find your programs. At the end of the lab there are some exercises which will help you to practice working with parameter passing in functions.
-
Activity 1: Move Multiple Lines
- This activity extends the previous one to call a function multiple times. It also introduces an element of problem solving as the new program doesn’t initially perform as expected. In this activity, do NOT do the Try It exercise as we will have our own exercises in the next activity.
- 🔗 Move Multiple Lines
-
Activity 2: Convert distance to CM
- This activity gets you working with floating point variables, which are critical to engineering programming. You will also perform mathematical operations in code. You do NOT need to do the “Try It” exercise.
- 🔗 Convert distance to CM
-
Activity 3: Calculate Distance Function
- In this activity you try to convert the calculation that you used repeatedly in activity 1 into a single function to make your program more efficient. At the end of the activity a problem is encountered which leads on to the concept of parameter passing which is introduced in the next activity.
- 🔗 Calculate Distance Function
-
Activity 4: Function Parameters
- This activity rounds off the work on functions by introducing the capability to pass data to a function and to get the result back from a function, thus greatly enhancing the flexibility and usefulness of functions.
- 🔗 Function Parameters
-
Activity 5: Displaying Values to LCD
- This final section covers the use of the LCD display and how to format messages to be shown on the display. After the Try It exercise there is a very useful couple of videos on the use of the debugger tools which are used to help you to track the changing values of your variables and to move through your code and inspect it on a line by line basis as it runs.
- 🔗 Displaying Values to LCD
-
Activity 6: More Exercises with Functions
- These next exercises are contained within the lab booklet in section 5.2 which you should already have downloaded and completed for Labs 3
-
Summary
- At the end of this lab you should be comfortable with writing simple functions and using parameter passing to send data to the functions and to get results back from the functions.