Upcoming SlideShare
×

# SDT Topic 06: Functions

1,910 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,910
On SlideShare
0
From Embeds
0
Number of Embeds
982
Actions
Shares
0
123
0
Likes
0
Embeds 0
No embeds

No notes for slide

### SDT Topic 06: Functions

1. 1. Topic 6 : Functions Software Development Techniques
2. 2. 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.
3. 3. 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.
4. 4. 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.
5. 5. Function Syntax function <function_name> //function statements end function
6. 6. Function Example • The call keyword here is used to call (or invoke) a function.
7. 7. Another Example
8. 8. 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.
9. 9. 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.
10. 10. 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.
11. 11. Function Expecting Parameters
12. 12. Calling a Function with Parameters
13. 13. 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.
14. 14. 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.
15. 15. Pseudo code
16. 16. 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)
17. 17. 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.
18. 18. 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)
19. 19. 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
20. 20. 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.
21. 21. 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.
22. 22. Defining a Return Type
23. 23. Using the Return Value
24. 24. 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)
25. 25. 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.
26. 26. 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
27. 27. 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
28. 28. 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.
29. 29. Functions • Functions should be as small as possible. • Functions should be reusable. • Functions should be generalised.
30. 30. 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.
31. 31. 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.
32. 32. 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.
33. 33. 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.
34. 34. Any Questions??? End of Topic 6