Chapter 7

Writing functions

CS1302 Introduction to Computer Programming

7.1 Function Basics

7.2 Parameter Passing

def name(parameter list):

           block

Example: pass user's input into function as parameter

7.3 Documenting Functions

7.3 Documenting Functions

The essential information includes: 

  • The purpose of the function.

  • The role of each parameter.

  • The nature of the return value.

7.4 Function Examples

Question: what is the order of functions being called and what content will be printed ?

7.4 Function Examples

Output:

I am method 1.
I am method 2.
I am method 1.
I am method 1.
I am method 2.
I am method 3.
I am method 1.
I am method 1.
I am method 2.

7.5 Refactoring to Eliminate Code Duplication

7.6 Custom Functions vs. Standard Functions

Generally speaking, if you have the choice of using a standard library function or writing your own custom function that provides the same functionality,

choose to use the standard library routine.

CS1302 Chapter 7

By Sally ZENG

CS1302 Chapter 7

Lecture 4 part 2

  • 107