Presentation is loading. Please wait.

Presentation is loading. Please wait.

National Diploma Unit 4 Introduction to Software Development Procedures and Functions.

Similar presentations


Presentation on theme: "National Diploma Unit 4 Introduction to Software Development Procedures and Functions."— Presentation transcript:

1 National Diploma Unit 4 Introduction to Software Development Procedures and Functions

2 Procedural programming These programs are executed line by line in a stepwise fashion Commands are interpreted line by line or the whole program is compiled first Complex programs might be broken down into a series of sub procedures In event driven programming (like VB), sub procedures usually run in response to an event such as a mouse clicking a button

3 Problem What if I want the same piece of code to run when a user clicks several controls? I could write the code behind each control by copying and pasting it This is not good practice – the more code you write, the more has to be stored, the more resources this uses up Better to write one general procedure that can be used many times

4 General procedures Are written in the general area of the form So far we have only used this to declare variables available to the whole form and to use the option explicit statement to make sure that all variables have to be declared before use

5 Creating a general procedure Click on the Tools menu item and then select Add Procedure

6 Creating a general procedure Select Sub, give the procedure a name. Note that the default scope is Public - this can be accessed throughout the program Your procedure will appear in the General area of the form.

7 Complex programs It might be that a really big program consists of many forms In this case, you want the procedure to be visible everywhere The best move is to create a Module to hold all of your general code that is not associated with a particular form or any controls on that form

8 Creating a module Select the Add Module option from the Project menu item. A module appears as a new project item and you can view all of the code but remember that there isn’t a form or any controls associated with a module.

9 Why use modules? The really great thing about modules is that if they are general purpose ones, they can be imported and reused in any other VB project This is good programming practice – modular code that permits reuse

10 Functions A Function is still a procedure but a special one – it returns a value to the procedure that called it. Adding a function is similar to adding a general procedure but select the function option

11 Functions Very often, rather than the program just running a bit of code, we want it to do something to a value and then return that new value to the program You all know what Add does If I said “you have two numbers: 1 and 6. Now Add them……… This is how functions work and the function would return 7

12 Calling a function Since functions are either in the general area of a form or in a module, we have to Call them A function will expect something to be sent to it (numbers 1 and 6 in our previous example) These are the Arguments

13 Calling a function Here is the example function from the practical handout Here it is being called in code and the argument being passed to it

14 Passing values Any sub procedure or function could possibly change a variable value In the first example, two arguments were passed that had the values of 1 and 6 I would have declared these are intNumber1 and intNumber2

15 Passing values by reference The function, by default will have the actual values passed to it – this is passing by reference In our example, this is fine, I store the total in another slot in memory, intTotal If I didn’t, I could overwrite the original value which might be needed elsewhere in the program

16 Passing values by value This is a safer bet when it is vital that a variable value is not overwritten You need to add the ByVal keyword to the argument: Public function doubleIt (ByVal intNumber) doubleIt will now make a copy of the value of intNumber and use that in the calculation

17 Using built in functions VB has loads of useful built in functions that you can call by name in your code: Date – returns the date Time – returns the time InputBox – returns whatever the user entered in the textbox MsgBox – returns a value depending on which button the user pressed

18 Built in functions Int – returns the integer part of a number IsNumeric – tests whether a value is a number or not. Returns true or false. Now – returns current system date and time Len – returns the number of characters in a string Left$- returns the leftmost character in a string Mid$ - returns a part of a string Right$ - guess what this does?

19 Return to Main menu


Download ppt "National Diploma Unit 4 Introduction to Software Development Procedures and Functions."

Similar presentations


Ads by Google