sdt topic 06: functions

Post on 18-Jan-2015

837 Views

Category:

Education

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Topic 6 : FunctionsSoftware Development Techniques

Complexity

• Complexity is an enemy of good program design.

• The complexity of many algorithms comes from the flow of execution contained within its structures.

• Many nested structures can create a flow of logic that is extremely difficult for humans to understand.

Complexity

• We resolve this complexity by subdividing our pseudocode programs into multiple parts.

• Each handling only a single responsibility.

• E.g. taking input, displaying, processing, etc.

• These parts are called functions.

• Or methods, depending on who you talk to.

What is a Function?

• You can think of a function as a mini-program.• It gets some starting values• It provides an answer at the end.

• Functions exist independently of your main pseudocode program.• They only get executed when they are invoked by your

main program.

• The main program is also a function.• It causes other smaller programs to run.

Function Syntax

function <function_name>

//function statements

end function

Function Example

• The call keyword here is used to call (or invoke) a function.

Another Example

Function

• A function looks exactly like the pseudocode programs we have run so far.

• A function has a name.

• The name can be anything that we want it to be.

• It also (sometimes) has parameters.

• Information that goes into the function.

• And also (sometimes) a return value.

Function

• Functions are not part of your main program.

• So, they do not have access to any of the information that is available in your main program.

• Also the information stored in the function can not be accessed by main function.

Parameters or Arguments

• If you want your function to have some information that your main program has, you have to provide it.

• That is done using parameters.

• In our function, we say ‘we expect some information to be provided before we can work’.

• Then, when the function is called, we provide that information.

• This information then becomes available like variables in the function.

Function Expecting Parameters

Calling a Function with Parameters

Desk-Checking A Function Call

• Note that functions have their own numbering.

• They do not continue on from the main program’s numbering.

• They should be treated as separate programs.

• They should have their own separate desk-check.

• We desk-check them exactly as we desk check other programs.

• Except that parameters will have a starting value.

Desk-Checking A Function Call

• When we reach the call statement in a pseudocode program, we need to note it.

• Note what information goes in.

• If relevant, note what data comes out.

• Make sure you do the desk-check for the function on a separate table.

• Start the parameters with whatever values they got from the function call.

• If they did not get enough of these (or got too many), note an error.

Pseudo code

Desk-Checking - Main Function

Line Number a b Remark

1 0

2 0 0

3 20 0

4 20 30

5 20 30 Call AddTwoNumbers (20, 30)

Desk-Checking - Function

Line Num. num1 num2 ans Output Remark

1 20 30 0 values from function call

2 20 30 50

3 20 30 50 The answer is 50 Output

• num1 and num2 do not start as 0, they get their starting values

from the function call.

Desk-Checking - Main Function

Line Number a b Remark

1 0

2 0 0

3 20 0

4 20 30

5 20 30 Call AddTwoNumbers (20, 30)

6 20 30 Call AddTwoNumbers (4, 6)

Desk-Checking - Function

Line Num. num1 num2 ans Output Remark

1 4 6 0 values from function call

2 4 6 10

3 4 6 10 The answer is 10 Output

Getting result from Function

• Parameters let us provide information to a function.

• But they do not let us get information out.

• In order to get information out, we need to return something.

• We can return only one thing in one function.

• We can have as many parameters as we like though.

Getting result from Function

• Now, if our function should return the sum rather than display it.

• What do we need?

• We need to say here what kind of data gets returned from our function,

• and we do this by giving its type.

• We then use the return keyword to give the answer back to the calling program.

Defining a Return Type

Using the Return Value

Desk-Checking - Main Function

Line Num. a b ans Output Remark

1 0

2 0 0

3 0 0 0

4 20 0 0

5 20 30 0

6 20 30 0 Call AddTwoNumbers (20, 30)

Desk-Checking - Function

Line Num. num1 num2 ans Remark

1 20 30 0 values from function call

2 20 30 50

3 20 30 50 Returns 50

• Num1 and num2 do not start as 0, they get their starting values from

the call.

Desk-Checking - Main Function

Line Num. a b ans Output Remark

1 0

2 0 0

3 0 0 0

4 20 0 0

5 20 30 0

6 20 30 50 Call AddTwoNumbers (20, 30)

7 20 30 50 The answer is 50 Output

Desk-Checking - Main Function

Line Num. a B ans Output Remark

1 0

2 0 0

3 0 0 0

4 20 0 0

5 20 30 0

6 20 30 50 Call AddTwoNumbers (20,

30)

7 20 30 50 The answer is 50 Output

8 20 30 10 Call AddTwoNumbers (4, 6)

9 20 30 10 The result is 10 Output

Types of Function

1. Function with no argument, no return type

2. Function with arguments, no return type

3. Function with no argument, return type

4. Function with argument, return type

• Write example of all four types of function.

Functions

• Functions should be as small as possible.

• Functions should be reusable.

• Functions should be generalised.

Benefits of Functions

• Functions make it easier to change your programs.

• Functions make it easier to follow your programs.

• Functions allow for reusability.

• Functions allow for expandability.

• Functions simplify desk-checking.

Flow of Execution

• Mostly functions simplify the flow of execution.

• It is easy to understand what is happening in a single loop.

• It is mostly understandable to follow what is happening in a nested loop.

• It is difficult to follow what is happening in a doubly nested loop.

Assignment

1. Write a pesudocode & java program to find factorial using function.

2. Write a pseudocode & java program to find factorial, even/odd, prime number, fibonacci series using function in single program.

3. Write a pseudocode & java program to take two inputs in input() function, sum them in sum() function and display the result in output() function.

Scenario: Voting…

• Your program must take in the following inputs from the user:

The amount of seats to be allocated, so that the program knows how many times to loop. The amount of seats must be between 6 and 60 inclusive.

The number of different parties standing in the election. This must be a whole number between 2 and 10 inclusive

The number of votes cast for each party standing in the election. These must be whole numbers between 0 and 600,000 inclusive.

Any Questions???End of Topic 6

top related