SDT Topic 06: Functions
-
Upload
pradip-kharbuja -
Category
Education
-
view
831 -
download
4
description
Transcript of SDT Topic 06: Functions
![Page 1: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/1.jpg)
Topic 6 : FunctionsSoftware Development Techniques
![Page 2: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/2.jpg)
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.
![Page 3: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/3.jpg)
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.
![Page 4: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/4.jpg)
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.
![Page 5: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/5.jpg)
Function Syntax
function <function_name>
//function statements
end function
![Page 6: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/6.jpg)
Function Example
• The call keyword here is used to call (or invoke) a function.
![Page 7: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/7.jpg)
Another Example
![Page 8: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/8.jpg)
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.
![Page 9: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/9.jpg)
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.
![Page 10: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/10.jpg)
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.
![Page 11: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/11.jpg)
Function Expecting Parameters
![Page 12: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/12.jpg)
Calling a Function with Parameters
![Page 13: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/13.jpg)
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.
![Page 14: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/14.jpg)
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.
![Page 15: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/15.jpg)
Pseudo code
![Page 16: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/16.jpg)
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)
![Page 17: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/17.jpg)
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.
![Page 18: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/18.jpg)
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)
![Page 19: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/19.jpg)
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
![Page 20: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/20.jpg)
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.
![Page 21: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/21.jpg)
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.
![Page 22: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/22.jpg)
Defining a Return Type
![Page 23: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/23.jpg)
Using the Return Value
![Page 24: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/24.jpg)
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)
![Page 25: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/25.jpg)
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.
![Page 26: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/26.jpg)
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
![Page 27: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/27.jpg)
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
![Page 28: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/28.jpg)
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.
![Page 29: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/29.jpg)
Functions
• Functions should be as small as possible.
• Functions should be reusable.
• Functions should be generalised.
![Page 30: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/30.jpg)
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.
![Page 31: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/31.jpg)
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.
![Page 32: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/32.jpg)
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.
![Page 33: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/33.jpg)
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.
![Page 34: SDT Topic 06: Functions](https://reader034.fdocuments.in/reader034/viewer/2022042510/54bb63d04a79595e238b4572/html5/thumbnails/34.jpg)
Any Questions???End of Topic 6