You are on page 1of 27

Functions

Session #4
This Lecture
• Session-03 review

• Functions

• Built-in functions

• Library functions

• User defined functions

• Passing Values using parameters


File operations
• Reading
• Reading Data from a file on Hard disk to memory (RAM)

• Writing
• Writing data from RAM to a newly created file on Hard disk.
• Note: Writing to an existing file overwrites the original data in that file

• Appending
• Adding data to the end of an existing file

3
What are the other file operations?
R RB R+ RB+

W WB W+ WB+

A AB A+ AB+
File functions
• open() and close()
• open() requires two pieces of input, (called arguments) a file name
and a mode of opening, for example, open(infileName, ‘r’)
• open() returns an integer that enables a file buffer to be accessed
• Infile = open(‘students.txt’,’r’)

• close() does not require any arguments.


• close() will commonly return an integer that will indicate whether the file
closure was successful or not
• Infile.close();

6
Functions
• Useful for breaking up a large program to make it easier to read and
maintain.

• Functions consist of code that carry out a specific task or function


• This code can be used so many times (reuse of code thereby reducing the file
size ).

• Different types of functions based on inputs and output


• Read inputs (arguments) return nothing
• Read inputs return output
• Read nothing return output
• Read nothing return nothing
Using functions
• Main is the calling function.
• Other functions;
• Open
• Print
• Close
Are the called functions.

• The arguments
• Students.txt, r are inputs to open
• Line[:-1] is the input to print
• No input to close.

28 November, 2020 8
Using functions
• When a function is called, control passes to the function

• When a called function has completed its processing, it returns to the


calling function:
• the output of its processing
• control of the program

28 November, 2020 9
Different types of functions

• Built-in functions

• Library functions

• User defined functions


Familiar functions
• Built-in functions
• float(7) → 7.0

• float(“7.8”) → 7.8

• int(7.8) → 7

• round(7.8) → 8

• len(“a string”) → 8

28 November, 2020 11
Familiar functions
• Library functions
• math.sqrt(144) → 12.0
• math.sqrt() - the argument can be of type int or float, a float is returned

• string.split(“the end”) → [“the”, ”end”]


• string.split() – the argument is a string and a list of strings is returned

28 November, 2020 12
User defined functions

1. Read inputs (arguments) return nothing

2. Read inputs return output

3. Read nothing return output

4. Read nothing return nothing


Read nothing return nothing
Read nothing return output
Read inputs return output
Read inputs (arguments) return nothing

• Write this for me in the class.


A simplified problem
• A company pays a number of its employees on an hourly basis.

• The week’s wages are calculated at $20 per hour.

• Develop a function for making that calculation.

18
Logic
• If an employee works for 30 hours. We have to calculate 30*20 and
return $600 as the answer.

• 20 hours = $400

• 40 hours = $800
Pseudocode

Main function
1. Read the input from user 1. Read input from the main function
• How many hours employee has worked? 2. Calculate employee wage
2. Call the function with user input 3. Return output to main
• Lets call it calc_wage()
3. Receive input from function
4. Print output

20
Pseudocode - main

• Input how many hours worked, assign it to hours_worked

• Call the function calc_wage with input hours_worked

• Receive and store the output from calc_wage to wage_to_pay

• Print wage_to_pay
Pseudocode – function calc_wage

• Read input hours_worked from main

• Calculate wage (20* hours_worked)

• Return wage to main


Passing and returning values
main() calc_wage(hours)
control

input value passed


40 hours

calculate
salary

Salary_to_pay 800 wage


assign return

23
Program structure
# define function calc_wage to read input hours_worked.
# set hourly rate to 20
# calculte wage and return to main
# start the main function.
Def main()
#Ask user for the number of hours worked.

#call the function calc_wage with hours_worked as input


# store the output to wage_to_pay

# print the wage to pay

main()
Develop the code in the class

25
Structure of code
• Always try to write a program in which main() is used simply to call a
series of functions.

• Then the functions can be written and tested independently

• Debugging (finding and fixing errors) becomes much simpler

26
Next Weeks Lecture
• Decision structures

• Multiple decisions

• Indefinite loops

• Loop/decision combinations

• Nested loops

You might also like