fundamentals of programming (python)ce.sharif.edu/courses/96-97/1/ce153-12/resources/root... ·...
TRANSCRIPT
![Page 1: Fundamentals of Programming (Python)ce.sharif.edu/courses/96-97/1/ce153-12/resources/root... · 2020. 9. 7. · You can (and should) do this in Python as well Typically main functions](https://reader035.vdocuments.us/reader035/viewer/2022070216/611adce6a857fc3618665781/html5/thumbnails/1.jpg)
Fundamentals of Programming(Python)
Functions
Sina SajadmaneshSharif University of Technology
Fall 2017
Slides have been adapted from “CSCI 111: Fundamentals of Programming I” by Sara Sprenkle
![Page 2: Fundamentals of Programming (Python)ce.sharif.edu/courses/96-97/1/ce153-12/resources/root... · 2020. 9. 7. · You can (and should) do this in Python as well Typically main functions](https://reader035.vdocuments.us/reader035/viewer/2022070216/611adce6a857fc3618665781/html5/thumbnails/2.jpg)
Outline1. Functions and Advantages
2. Definition and Calling
3. Function Parameters
4. Function Output
5. Flow of Control
6. Program Organization
2SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
![Page 3: Fundamentals of Programming (Python)ce.sharif.edu/courses/96-97/1/ce153-12/resources/root... · 2020. 9. 7. · You can (and should) do this in Python as well Typically main functions](https://reader035.vdocuments.us/reader035/viewer/2022070216/611adce6a857fc3618665781/html5/thumbnails/3.jpg)
FunctionsFunctions perform some task◦ May take arguments/parameters
◦ May return a value that can be later used
Function is a black box◦ Implementation doesn't matter
◦ Only care that function generates appropriate output
3SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
![Page 4: Fundamentals of Programming (Python)ce.sharif.edu/courses/96-97/1/ce153-12/resources/root... · 2020. 9. 7. · You can (and should) do this in Python as well Typically main functions](https://reader035.vdocuments.us/reader035/viewer/2022070216/611adce6a857fc3618665781/html5/thumbnails/4.jpg)
AdvantagesAllows you to break up a hard problem into smaller, more manageable parts
Makes your code easier to understand
Hides implementation details (abstraction)
Makes part of the code reusable so that you: ◦ Only have to write function code once
◦ Can debug it all at once
◦ Can make changes in one function (maintainability)
4SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
![Page 5: Fundamentals of Programming (Python)ce.sharif.edu/courses/96-97/1/ce153-12/resources/root... · 2020. 9. 7. · You can (and should) do this in Python as well Typically main functions](https://reader035.vdocuments.us/reader035/viewer/2022070216/611adce6a857fc3618665781/html5/thumbnails/5.jpg)
Definition
5SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
![Page 6: Fundamentals of Programming (Python)ce.sharif.edu/courses/96-97/1/ce153-12/resources/root... · 2020. 9. 7. · You can (and should) do this in Python as well Typically main functions](https://reader035.vdocuments.us/reader035/viewer/2022070216/611adce6a857fc3618665781/html5/thumbnails/6.jpg)
Calling a Function
6SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
![Page 7: Fundamentals of Programming (Python)ce.sharif.edu/courses/96-97/1/ce153-12/resources/root... · 2020. 9. 7. · You can (and should) do this in Python as well Typically main functions](https://reader035.vdocuments.us/reader035/viewer/2022070216/611adce6a857fc3618665781/html5/thumbnails/7.jpg)
ParametersThe inputs to a function is called parameters◦ Parameters are local to the function and cannot be
references outside the function body
When calling a function: ◦ Must appear in the same order as function header:
◦ Or, must used in name=value way:
◦ Or both:
7SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
average = average2(100, 50)
average = average2(num1=100, num2=50)
average = average2(100, num2=50)
![Page 8: Fundamentals of Programming (Python)ce.sharif.edu/courses/96-97/1/ce153-12/resources/root... · 2020. 9. 7. · You can (and should) do this in Python as well Typically main functions](https://reader035.vdocuments.us/reader035/viewer/2022070216/611adce6a857fc3618665781/html5/thumbnails/8.jpg)
Passing ParametersOnly copies of the actual parameters are given to the function for immutable data types◦ Most of the data types we have talked about, such as
integer, float, string, and boolean are immutable
The actual parameters in the calling code do not change
8SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
![Page 9: Fundamentals of Programming (Python)ce.sharif.edu/courses/96-97/1/ce153-12/resources/root... · 2020. 9. 7. · You can (and should) do this in Python as well Typically main functions](https://reader035.vdocuments.us/reader035/viewer/2022070216/611adce6a857fc3618665781/html5/thumbnails/9.jpg)
Function OutputWhen the code reaches a statement like:
◦ The function stops executing
◦ x is the output returned to the place where the function was called
For functions that don't have explicit output, return does not have a value with it
Optional: don't need to have return◦ Function automatically returns at the end
9SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
return x
![Page 10: Fundamentals of Programming (Python)ce.sharif.edu/courses/96-97/1/ce153-12/resources/root... · 2020. 9. 7. · You can (and should) do this in Python as well Typically main functions](https://reader035.vdocuments.us/reader035/viewer/2022070216/611adce6a857fc3618665781/html5/thumbnails/10.jpg)
Flow of ControlWhen program calls a function, the program jumps to the function and executes it
After executing the function, the program returns to the same place in the calling code where it left of
10SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
![Page 11: Fundamentals of Programming (Python)ce.sharif.edu/courses/96-97/1/ce153-12/resources/root... · 2020. 9. 7. · You can (and should) do this in Python as well Typically main functions](https://reader035.vdocuments.us/reader035/viewer/2022070216/611adce6a857fc3618665781/html5/thumbnails/11.jpg)
Program OrganizationThe main function◦ In many languages, you put the “driver” for your
program in a main function◦ You can (and should) do this in Python as well
◦ Typically main functions are defined at the top of your program◦ Readers can quickly see an overview of what program does
◦ main usually takes no arguments
11SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
def main():
![Page 12: Fundamentals of Programming (Python)ce.sharif.edu/courses/96-97/1/ce153-12/resources/root... · 2020. 9. 7. · You can (and should) do this in Python as well Typically main functions](https://reader035.vdocuments.us/reader035/viewer/2022070216/611adce6a857fc3618665781/html5/thumbnails/12.jpg)
Program OrganizationThe main function◦ Call main() at the bottom of your program
◦ You can (and should) do this in Python as well
◦ Side effects:◦ Do not need to define functions before main function
◦ main can “see” all other functions
12SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017