Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Using Functions

A function is a SUB that sends an answer back to you. Programmers call the answer a return value. Functions are really helpful! A function returns one value, and only one. Let’s write a short program to show how to use a function. This program, which is called AddFunction.bas, has you enter two numbers. Then a function called Add() adds the numbers together and returns the answer.


image

A FUNCTION sends an answer back (a return value), but a SUB does not.


INPUT "Enter number 1: "; number1
INPUT "Enter number 2: "; number2
answer = Add(number1, number2)
PRINT number1; "+"; number2; "="; answer

FUNCTION Add (A, B)
sum = A + B
Add = sum
END FUNCTION

Here is the output when you run the program. I entered 12 and 16 as the numbers, and it computed the answer as 28.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint