Download - ITEC 109
![Page 1: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/1.jpg)
ITEC 109
Lecture 15Advanced functions
![Page 2: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/2.jpg)
Functions (2)
Review
• Functions– Parameters– Return statements– Variable scope
![Page 3: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/3.jpg)
Functions (2)
Objectives
• Functions calling other functions• Patterns• Examples
![Page 4: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/4.jpg)
Functions (2)
Easy
• Main to function and backdef FunctionName():
printNow(“I am a function”)def FunctionTwo():
printNow(“I am the second”)
FunctionName()FunctionTwo()
![Page 5: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/5.jpg)
Functions (2)
Driver
Driver
Curve1 Curve2 Curve3
![Page 6: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/6.jpg)
Functions (2)
Pro/Con
• Pro– Breaks a problem into steps– Easy to re-order operations in the code
• Con– Initial setup cost is high– Requires design, can’t just code it ™
![Page 7: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/7.jpg)
Functions (2)
Application
• Standard cycle pattern
Driver
IO Compute Output
def getValue1():return 3;
def getValue2():return 5;
def compute(a, b)return a+b;
def ouput(value)printNow(value)
x =getValue1();y= getValue2();result = compute(x,y);output(result);
![Page 8: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/8.jpg)
Functions (2)
Pro/Con
• Pro– Separates parts of implementation
(keyboard vs file)– Scaling of project
• Con– Data management– Questions of what happens when you need
to mix input / computation
![Page 9: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/9.jpg)
Functions (2)
Chooser
Driver
Input
Function2 Function3
Choice
def getBalance():return 55;
def badCredit():printNow(“Work harder”)
def goodCredit():printNow(“Keep it up”)
amount = getBalance();if (amount > 50):
goodCredit(); else:
badCredit();
![Page 10: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/10.jpg)
Functions (2)
Stair step
Driver
Function2
Function3
Function 1
def function1():function2();
def function2():function3();
def function3():printNow(“Complicated”)
function1()
![Page 11: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/11.jpg)
Functions (2)
Pro/Con
• Pro–Models more complex problems
• Con–What happens when something goes
wrong?– If A calls B, which calls C, which calls A,
what happens?
![Page 12: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/12.jpg)
Functions (2)
Stair climbing
Driver
Function2
Function3
Function 1
Start
Go back up
*Warning*
![Page 13: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/13.jpg)
Functions (2)
Example
def sum4():return 4 + sum3();
def sum3():return 3 +sum2();
def sum2():return 2 + sum1();
def sum1():return 1;
answer =sum4();printNow(answer)
//Prints out sum of 4+3+2+1
![Page 14: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/14.jpg)
Functions (2)
Pro/Con
• Pro– Crazy and semi-useful tool– Brain bending mental exercise
• Con– Algorithm design complexity– Debugging (100% CPU usage)– Recursion
![Page 15: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/15.jpg)
Functions (2)
Combination
Driver
Function 1
Function2
Choice
Function3
What kind of code would we need for this?
![Page 16: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/16.jpg)
Functions (2)
Pro/Con
• Pro– Divide and conqueror – Flexibility
• Con– You have to figure it out– Requirements– Design– Implementation
![Page 17: ITEC 109](https://reader037.vdocument.in/reader037/viewer/2022110212/56813b00550346895da39abc/html5/thumbnails/17.jpg)
Functions (2)
Review
• Function review• Functions calling functions• Pattern review