SENG 208 PYTHON PROGRAMMING Functions Week-5
FUNCTION BASICS A function is a block of code that performs a sequence of instructions.
FUNCTION BASICS A function is a block of code that performs a sequence of instructions. Whenever the function is “called”, the sequence of instructions is executed.
FUNCTION BASICS Functions can take inputs and return outputs based on those inputs. Here are examples of built-in functions you might have seen:
FUNCTION BASICS • Some functions perform actions instead of returning outputs. • These actions are called side effects. For example, displaying text in the interactive console is a side effect of the print.
FUNCTION BASICS • The text outputs in the console are examples of a side effect. There is no value that is produced by calling print()
FUNCTION BASICS Function diagrams summarise what functions do
FUNCTION BASICS Built-in (or predefined) Functions User-Defined Functions range() type() print() eval() .... .... abc() xyz() …. …. ….
Anatomy of a User-defined Function
Parameters and Arguments • A parameter is a variable used in the definition of a function, which will be initialised with an argument value during a function call. • The particular name we use for a parameter is irrelevant as long as we use the name consistently in the function definition.
Parameters and Arguments • Syntax of creating a function:
Parameters and Arguments • Syntax of creating a function:
Unindented function body • Python is unusual among programming languages in that it uses indentation to determine what’s in the body of a function.
Python Function Call Model
Multiple parameters A function can take as many parameters as needed. They are separated via comma.
Multiple parameters Example:
Functions calling functions The function hypotenuse calls the square function we just defined.
Functions calling functions The function distanceBetweenPoints calls the hypotenuse function defined above.
Creating a function without any parameters The function hypotenuse calls the square function we just defined.
Local variables • An assignment to a variable within a function definition creates/changes a local variable. • Local variables exist only within a function’s body. They cannot be referred outside of it. • Parameters are also local variables that are assigned a value when the function is invoked. They also cannot be referred to outside the function.
RETURN VS. PRINT • return specifies the result of the function invocation • print causes characters to be displayed in the shell (side effect).
RETURN VS. PRINT • Don’t confuse return with print!
Incremental Development of Your Code • When writing your own functions or any other type of code, do not attempt to write it all at once! • Try to complete small tasks one at a time and test along the way. • Store longer expressions in variables as you go along, and continue to reference those variables.
Incremental Development of Your Code
Visualizing Code Execution with the Python Tutor • Python Tutor: http://www.pythontutor.com/visualize.html
Examples
Examples
Examples

SENG 208 Week -5 Python Functions Presentation