Functions. Introduction to Functions. Chapter 6 CSUS, Fall Chapter 6.1

Functions Chapter 6 CSUS, Fall 2016 Introduction to Functions Chapter 6.1 1 Introduction to Functions  A function is a module that returns a v...
10 downloads 2 Views 400KB Size
Functions

Chapter 6 CSUS, Fall 2016

Introduction to Functions

Chapter 6.1

1

Introduction to Functions

 A function is a module that returns a value back to the part of the program that called it  Found in practically all programming languages 10/28/2016

Sacramento State - CSc 10A

1-3

Library Functions

 Many programming languages come with a collection of pre-defined functions  They perform common tasks and save time for the programmer 10/28/2016

Sacramento State - CSc 10A

1-4

2

Library Functions

 These are stored in are called "library" functions  They are included from a separate file

10/28/2016

Sacramento State - CSc 10A

1-5

Intrinsic Functions

 Some languages have built-in "intrinsic" functions  These are a core part of the language  They are handled directly by the compiler or interpreter 10/28/2016

Sacramento State - CSc 10A

1-6

3

Example: Random Numbers  A common library function is a Random Number Generator  Random numbers are useful for: • • • • 10/28/2016

game programs simulations statistical programs encryption Sacramento State - CSc 10A

1-7

Example: Random Numbers

 The function returns a random number either between two integers  Generally accepts 1 or 2 arguments: • Random(n) – random between 0 and n-1 • Random(n, m) – random between n and m

10/28/2016

Sacramento State - CSc 10A

1-8

4

Random Example

10/28/2016

Sacramento State - CSc 10A

9

Random Example Output

10/28/2016

Sacramento State - CSc 10A

10

5

Writing Your Own Functions Chapter 6.2

Writing Your Own Functions  Most languages allow programmers to write their own functions  They can be called and used just like any library function  Most programmers create their own library 10/28/2016

Sacramento State - CSc 10A

1-12

6

Function Headers

 The function header specifies information about the function itself  It contains: • name of the function • any parameter variables • data type of the value that is returned

10/28/2016

Sacramento State - CSc 10A

1-13

Function Body

 The function body contains the statements that execute when the function calls  At the end of the body, a return statement specifies the value that is returned when the function ends

10/28/2016

Sacramento State - CSc 10A

1-14

7

Function Body

 Some program languages allow a return statement to be located anywhere  However, it is considered good style to have a one-way-in-one-way-out design  So, ideally, there is only one return statement – and it is the last statement 10/28/2016

Sacramento State - CSc 10A

1-15

Additional Concerns  Each function should be flowcharted separately  To graphically represent a function's treatment of data, we can use an IPO (input, processing, and output) chart  The IPO contains the arguments, output type, and a description of the behavior 10/28/2016

Sacramento State - CSc 10A

1-16

8

IPO Chart Example getPrice Function Input None

10/28/2016

Processing Prompts the user to enter an item’s regular price

Output The item’s regular price, as a Real

Sacramento State - CSc 10A

1-17

IPO Chart Example Sum Function Input Integer num1 Integer num2

10/28/2016

Processing Adds the num1 and num2 to together

Sacramento State - CSc 10A

Output sum of num1, num2

1-18

9

Example: Sum Function returns an integer

Function Integer sum(Integer num1, Integer num2) Declare Integer result Set result = num1 + num2 Return result End Function Return the result 10/28/2016

Sacramento State - CSc 10A

1-19

Sacramento State - CSc 10A

20

Example: Sum

10/28/2016

10

Example: Calling Sum

Module Main Declare Integer x Declare Integer y Input x Input y

Output the result of the function

Display sum(x, y) End Module

10/28/2016

Sacramento State - CSc 10A

1-21

Example: Calling Sum

10/28/2016

Sacramento State - CSc 10A

22

11

Example: Sum Output

10/28/2016

Sacramento State - CSc 10A

23

More Library Functions

Chapter 6.3

12

More Library Functions  The library functions found in each programming language varies  The book uses a number of functions in its pseudocode  Flowgorithm also contains library functions 10/28/2016

Sacramento State - CSc 10A

25

Flowgorithm Math Functions Function

Description

abs(n)

Absolute Value

arctan(n)

Trigonometric Arctangent

cos(n)

Trigonometric Cosine

int(n)

Integer of a real number

log(n)

Natural Log

10/28/2016

Sacramento State - CSc 10A

26

13

Flowgorithm Math Functions Function

Description

log10(n)

Log Base 10

sgn(n)

Mathematical sign

sin(n)

Trigonometric Sine

sqrt(n)

Square Root

tan(n)

Trigonometric Tangent

10/28/2016

Sacramento State - CSc 10A

27

Flowgorithm String Functions Function

Description

len(s)

Length of a string

char(s, n)

Returns a single character from the existing string s. Characters are indexed starting at 0.

10/28/2016

Sacramento State - CSc 10A

28

14

Flowgorithm Type Conversion Function

Description

ToInteger(n)

Convert a string to an integer

ToReal(n)

Convert a string to an real

ToString(n)

Convert a number to a string

ToChar(n)

Convert the character code n to a character

ToCode(n)

Convert a character into a character code

10/28/2016

Sacramento State - CSc 10A

29

15