261x Filetype PDF File size 0.17 MB Source: etut.edu.tm
MODERNISATION OF HIGHER EDUCATION
IN CENTRAL ASIA
THROUGH NEW TECHNOLOGIES
( HiEdTec )
Objected Oriented Programming
Lecture12: Procedure
Procedure
A procedure allows us to group a block of code under a name, known as a pr
ocedure name. We can call the block of code from anywhere in the program to exe
cute the instructions it contains. We can also pass values to the procedure to chang
e how it works.
In this example I want to print the squares of the numbers from 1 to 20:
#This is a procedure called output
#It will print a given number and its square
def output(number):
print(number,"squared =",number*number)
#This is the start of the main program
number = 1
while number < 21:
output(number)
number = number + 1
Multiple Parameters
A parameter is the variable (or variables) that get passed to the procedure. You can
require more than one parameter:
def output(number1,number2):
print(number1,"+",number2,"=",number1+number2)
for counter in range(20):
number = counter**2
output(counter,number)
MODERNISATION OF HIGHER EDUCATION
IN CENTRAL ASIA
THROUGH NEW TECHNOLOGIES
( HiEdTec )
You can pass as many parameters as you like to a procedure as long as you set it
up correctly.
Optional Parameters
Once you have defined a procedure, you can call it in a number of ways:
def output(grade,score=50,feedback="Well done!"):
print("You scored",score,"which is a grade",grade,feedback)
output("C")
output("A",87)
output("E",12,"Rubbish!")
You can pass as many (or few) of the parameters as you like - as long as they’re in
the right order. This means you have to plan the procedure quite carefully.
MODERNISATION OF HIGHER EDUCATION
IN CENTRAL ASIA
THROUGH NEW TECHNOLOGIES
( HiEdTec )
REFERENCES:
1.”Turkmenistanyn Konstitusiýasy”. Aşgabat, TDNG.2008.
2. ”Turkmenistanyn Prezidenti Gurbanguly Malikgulyyewiç Berdimuhamedowyň gysgaça
terjimehaly”.- Asgabat, TDNG.2007.
3. Gurbanguly Berdimuhamedow. ”Garaşsyzlyga guwanmak, Watany, halky söýmek bagtdyr”-
Asgabat, TDNG.2007.
4. Gurbanguly Berdimuhamedow.” Parahatçylyk, döredijilik, progres syýasatynyň
dabaralanmagy”- Asgabat, TDNG.2007.
5. Gurbanguly Berdimuhamedow. “Halkyň saýlany we ynam bildireni”- Asgabat,TDNG.2007.
6. John Shovic and Alan Simpson, “Python All-in-One For Dummies”, New Jersey, 2019
7. Michel Anders, “Python 3 Web Development Beginner's Guide”, UK, 2011 Packt
Publishing
8. Python Crash Course, 2nd Edition: A Hands-On, Project-Based Introduction. 3pnK
MaTTec. 20 Hoadpa 2015 r.
9. Automate the Boring Stuff with Python: Practical Programming for Total. Ant
CBenrapT. 14 anpena 2015 r.
10. Fluent Python. Knnra, Jlycnano Pamanbo. 2015 r.
11. David Beazley and Brian Jones , “Python Cookbook, Third Edition” United States of
America, 2013
12. www.w3schools.com
13. Problem book on programming. Scalar types, control statements, procedures and
functions, arrays, strings, files, recursion, dynamic data structures. A. V. Abramyan, M. E.
Abramyan. The Ministry of Education and Science of the Russian Federation Southern
Federal University. Rostov-on-Don 2014
14. Python Programming for Beginners: An Introduction to the Python Computer
Programming. Kunra, Alencon KOHHOH. 2014 r
15. Fluent Python. Knnra, Jlycnano Pamanbo. 2015 r.
16. JerKHH cnocod BbiyunTb Python. Kunra, 3eg ffloy. 19 ceHTadpa 2013 r.
17. www.tutorialspoint.com
MODERNISATION OF HIGHER EDUCATION
IN CENTRAL ASIA
THROUGH NEW TECHNOLOGIES
( HiEdTec )
Problem set #12
Choose correct answer
1. Does the function call in the following function cause syntax errors?
import math
def main():
math.sin(math.pi)
main()
a)yes
b)no
2. Which of the following should be defined as a None function?
a) Write a function that prints integers from 1 to 100.
b) Write a function that returns a random integer from 1 to 100.
c) Write a function that checks whether a number is from 1 to 100.
d) Write a function that converts an uppercase letter to lowercase.
3. Consider the following incomplete code:
def f(number):
# Missing function body
print(f(5))
The missing function body should be ________.
a) return "number"
b) print(number)
c) print("number")
d) return number
no reviews yet
Please Login to review.