Matthieu Choplin
A function is a collection of statements that are grouped together to perform an operation.
def printGrade(score):
# Print grade for the score
if score >= 90.0:
print('A')
elif score >= 80.0:
print('B')
elif score >= 70.0:
print('C')
elif score >= 60.0:
print('D')
else:
print('F')
def main():
score = eval(input("Enter a score: "))
print("The grade is ", end = "")
printGrade(score)
main() # Call the main function
def getGrade(score):
# Return the grade for the score
if score >= 90.0:
return 'A'
elif score >= 80.0:
return 'B'
elif score >= 70.0:
return 'C'
elif score >= 60.0:
return 'D'
else:
return 'F'
def main():
score = eval(input("Enter a score: "))
print("The grade is", getGrade(score))
main() # Call the main function
A function that does not return a value is known as a void function. In Python, such function returns a special None.
Suppose you have the following function:
def nPrintln(message, n):
for i in range(0, n):
print(message)
What is the ouput of nPrintln("Welcome to Python", 5)?
What is the ouput of nPrintln(15, "Computer Science")?
What is wrong? How to fix?
With the same function:
def nPrintln(message, n):
for i in range(0, n):
print(message)
What is the ouput of nPrintln(message="Welcome to Python", n=5)
What is the ouput of nPrintln(n = 4, message = "Computer Science")
What is wrong? How to fix?
In Python, all data are objects. A variable for an object is actually a reference to the object. When you invoke a function with a parameter, the reference value of the argument is passed to the parameter. This is referred to as pass-by-value. For simplicity, we say that the value of an argument is passed to a parameter when invoking a function. Precisely, the value is actually a reference value to the object.
If the argument is a number or a string, the argument is not affected, regardless of the changes made to the parameter inside the function.
Functions can be used to reduce redundant coding and enable code reuse. Functions can also be used to modularize code and improve the quality of the program.
Example, download the following files and put them in the current Pycharm project (right click and save as):
The program PrimeNumberFunction.py (right click and save as) provides the isPrime(number) function for testing whether a number is prime.
Use this function to find the number of prime numbers less than 10,000.
Using the function in the same file
Solution
Hide solution
def main():
count = 0
N = 10000
for number in range(2, N):
if isPrime(number):
count += 1
print("The number of prime number <", 10000, "is", count)
# Check whether number is prime
def isPrime(number):
for divisor in range(2, number // 2 + 1):
if number % divisor == 0: # If true, number is not prime
return False # number is not a prime
return True # number is prime
main()
Import the function from an other file
Solution
Hide solution
from PrimeNumberFunction import isPrime
def main():
count = 0
N = 10000
for number in range(2, N):
if isPrime(number):
count += 1
print("The number of prime number <", 10000, "is", count)
main()
Scope: the part of the program where the variable can be referenced.
A variable created inside a function is referred to as a local variable. Local variables can only be accessed inside a function. The scope of a local variable starts from its creation and continues to the end of the function that contains the variable.
In Python, you can also use global variables. They are created outside all functions and are accessible to all functions in their scope.
Python allows you to define functions with default argument values. The default values are passed to the parameters when a function is invoked without the arguments.
Python allows a function to return multiple values. The following program defines a function that takes two numbers and returns them in non-descending order.
You can think of the function body as a black box that contains the detailed implementation for the function.
The concept of function abstraction can be applied to the process of developing programs. When writing a large program, you can use the "divide and conquer" strategy, also known as stepwise refinement, to decompose it into subproblems. The subproblems can be further decomposed into smaller, more manageable problems.
Let us use the PrintCalendar example to demonstrate the stepwise (or divide-and-conquer) refinement approach. Suppose you write a program that displays the calendar for a given month of the year. The program prompts the user to enter the year and the month, and then it displays the entire calendar for the month, as shown in the following sample run:
Enter full year (e.g., 2001): 2011
Enter month as number between 1 and 12: 9
September 2011
———————————————————————————
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
Top-down approach is to implement one function in the structure chart at a time from the top to the bottom.
Stubs can be used for the functions waiting to be implemented. A stub is a simple but incomplete version of a function. The use of stubs enables you to test invoking the function from a caller. Implement the main function first and then use a stub for the printMonth function. For example, let printMonth display the year and the month in the stub. Thus, your program may begin like this:
Bottom-up approach is to implement one function in the structure chart at a time from the bottom to the top. For each function implemented, write a test program to test it. Both top-down and bottom-up functions are fine. Both approaches implement the functions incrementally and help to isolate programming errors and makes debugging easy. Sometimes, they can be used together.
The isLeapYear(year) function can be implemented using the following code:
return year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)
Use the following facts to implement getTotalNumberOfDaysInMonth(year, month):
This approach makes the program easier to write, reuse, debug, test, modify, and maintain.
Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with:
In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly.
Credit card numbers are generated following this validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustration, consider the card number 4388576018402626):
Write a program that prompts the user to enter a credit card number as an integer. Display whether the number is valid or invalid. Design your program to use the following functions:
#Return true if the card number is valid
def isValid(number):
#Get the result from Step2
def sumOfDoubleEvenPlace(number):
#Return this number if it is a single digit, otherwise,return
#the sum of the two digits
def getDigit(number):
#Return sum of odd place digits in number
def sumOfOddPlace(number):
#Return true if the digit d is a prefix for number
def prefixMatched(number,d):
#Return the number of digits in d
def getSize(d):
#Return the first k number of digits from number.If the
#number of digits in number is less than k, return number.
def getPrefix(number,k):