Monday, March 15, 2010

Introduction To Programming: Starting Out With Programming Logic And Design: Chapter 6: Functions

Starting Out with Programming Logic and Design (2nd Edition)

Chapter 6 Functions

1. Introduction to Functions: Generating Random Numbers

A function is a specially designed module. It will return a value to the part of the program that called it.

Library Function

Most programming languages have built in functions called library functions. They simplify things by doing many tasks that a programmer would have to write himself. Since a programmer will not see the inner workings of them they are sometimes referred to as a 'black box'.

Using the random Function

Random numbers are used in many ways so most programming languages provide for a way to generate them. A common way to code it would be

Set number = (1,100)

With random being the function, number the variable and 1, 100beng the arguments to define the range of numbers to work with. Number would now take a value from 1 to 100. A random number function will return an integer so you want to make sure that the variable is an integer or an error will result.

2. Writing Your Own Functions

When you write your own functions they must have three parts:

  1. Function header - gives the type of data returned and the name of the function; optionally has a parameter list of variables passed to it
  2. Function body - one or more statements that will be done
  3. Return statement - usually the word return with a value that will go back to the calling program line

The type of data would define what it is: real, integer, string. The function name should be descriptive of what it does and will usually have to follow some naming rules.

Usually after the return will be something that indicates the function has ended. We will use end function.

Flowcharting a Function

Draw it like any other program but functions get their own separate chart.

Making the Most of the return statement

Since the return statement will return a variable, it can be used in place of a variable to eliminate a step and make code more efficient.

How To Use Functions

In an effort to make code easier to read it is possible to take confusing code and split it out as a function.

Using IPO Charts

An IPO chart takes the input (I), processing (P), and output (O), and displays them as boxes to define what a function does.

Returning Strings

Most programming languages allow for strings to be returned.

Returning Boolean Values

Most programming languages allow for Boolean values to be returned.

3. More Library Functions

Mathematical Functions

The sqrt Function

Accepts an argument of a number and returns the square root.

The pow Function

Same as ^ since some programming languages do not have raise to power symbol.

abs returns absolute value of a number

Cos returns the cosine of argument

Round accepts a real number and returns it closest value after rounding it.

sin returns sine of an argument

tan returns tangent of an argument

Data Type Conversion Functions

toInteger converts a real number to an integer

toReal takes an integer and converts it to a real

Formatting Functions

Most languages offer a way to format numbers so that it will display as currency.

String Functions

The length Functions

Returns an integer value indicating the length of a string.

The append Function

Takes two strings and ties the first one to the second one and returns a third string.

The toUpper and toLower function

Converts a string of text to all upper or all lower text.

The substring Function

Takes as parameters a string, a starting integer and ending integer and returns a string from the original string starting and ending at the marked locations.

The contains Function

Accepts two strings and checks to see if the second string is in the first.

The stringToInteger and stringToReal Function

Converts a number stored as a string to either a real or an integer.

The isInteger and isReal Functions

Checks to see if a string can be converted.

 

Beginning Programming (Wrox Beginning Guides)

No comments:

Post a Comment