Year 1 Computer Programming - Lecture 3
Year 1 Computer Programming - Lecture 3
(Based on Gaddis, T., Starting out with Python 3e, Pearson Education)
Functions
– Introduction
– Benefits of Functions
– Different Types of Functions
– Defining and Calling Function
– Local Variables Vs Global Variables
– Function Arguments
– Functions and Modules
A void function:
– Simply executes the statements it contains and then terminates.
A value-returning function:
– Executes the statements it contains, and then it returns a value back to
the statement that called it.
- The input, int, and float functions are examples of value-
returning functions.
We need to write a program that inputs two numbers from keyboard and
displays the sum of the numbers.
– Apply divide and conquer technique to identify subtasks
- Input number(s)
- Compute Sum
- Display sum
- Write a main function to coordinate the functions to achieve the overall task.
Different functions may have local variables with the same name
– Each function does not see the other function’s local variables, so no
confusion
Arguments
print(volume(2, 3, 3))
print(volume(2, 3))
– Output
18
12
a, b, c = myfun(1, 2, 3)
print(a, b, c)
result = myfun(1,2,3)
print(result[0], result[1], result[2])
>>> >>>
246 246
>>> >>>