Unformatted text preview:

COMP 116 FUNCTIONS Instructor Jason Carter CHANGE THE PROGRAM Change the lines that say The first name you entered has no characters The last name you entered has no characters The birthday you entered has no characters TO Lines that say The first name you entered is empty The last name you entered is empty The birthday you entered is empty Make changes 3 places Make changes 3 places WHY FUNCTIONS Makes your program easier to read and debug Groups statements If changes have to be made only one place to make them in Reduce repetitive code Makes programs smaller Reuse programs Debug once Well designed functions are often useful for many FUNCTION A named sequence of statements that performs a computation Pick a name for a function that describes what it does just like variable names Usually one task of a large program Functions can be executed in order to perform overall program task Known as divide and conquer approach Modularized program program wherein each task within the program is in its own function Input Function Output Function DIVIDE AND CONQUER Program to make dinner make appetizier make salad make entr e make dessert TWO TYPES OF FUNCTIONS Built in User defined BUILT IN FUNCTIONS https docs python org 2 library functions html BUILT IN FUNCTIONS To use functions we call them print Hello World print Hello World Input print Output What is the input to the print function Hello World What is the output of the print function Printing the words Hello World to the console print BUILT IN FUNCTIONS len Return the length the number of items of an object The argument may be a sequence such as a string bytes tuple list or range or a collection such as a dictionary set or frozen set len Hello World Input print Output What is the input to the len function Hello World What is the output of the len function 11 print BUILT IN FUNCTIONS abs abs 4 5 Return the absolute value of a number The argument may be a plain or long integer or a floating point number If the argument is a complex number its magnitude is returned Input abs Output What is the input to the abs function What is the output of the len function 4 5 4 5 abs BUILT IN FUNCTIONS Print the length of the string Hello World to the console print len Hello World length len Hello World print length print abs 4 5 abs value abs 4 5 print abs value Print the absolute value of 4 5 to the console TWO TYPES OF FUNCTIONS Built in User defined USER DEFINED FUNCTIONS Function Header def name statement statement statement Notice how the statements are indented This is how python knows where a function begins and ends def add two numbers number one int input enter a number number two int input enter a number print number one number two print This program adds two numbers add two numbers Function Header INDENTATION Each block must be indented Lines in block must begin with the same number of spaces Use tabs or spaces to indent lines in a block but not both as this can confuse the Python interpreter Pycharm automatically indents the lines in a block Blank lines that appear in a block are ignored CALLING USER DEFINED FUNCTIONS After defining a function to run the code inside you call the function When a function is called Interpreter jumps to the function and executes statements in the block Interpreter jumps back to part of program that called the function FLOW OF EXECUTION The order in which statements are executed Execution always begins at the first statement of the Statements are executed one at a time in order from program top to bottom name statement statement statement name Execution starts here Execution starts here CALLING USER DEFINED FUNCTIONS name statement statement statement name name After defining a function you can call it any number of times Each time it is called Python acts as if you had typed in all the lines of the function definition A function call must come after the function definition Functions are case sensitive MAIN FUNCTION From this point on always define a main function in your programs Always call the main function as the last line in your program main function called when the program starts The main function works the same as all other functions but just called first when the program starts Calls other functions when they are needed Defines the mainline logic of the program EXAMPLE USER DEFINED FUNCTIONS 9 4 5 1 2 3 5 ARGUMENTS AND PARAMETERS User Defined functions can take input just like built in functions EXAMPLE ARGUMENTS AND PARAMETERS LOCAL VARIABLE Local variable variable that is assigned a value inside a function Belongs to the function in which it was created Only statements inside that function can access it error will occur if another function tries to access the variable Scope the part of a program in which a variable may be accessed For local variable function in which created LOCAL VARIABLE A local variable cannot be accessed by statements inside its function which precede its creation Different functions may have local variables with the same name Each function does not see the other function s local variables so no confusion EXAMPLE EXAMPLE EXAMPLE PARAMETERS ARE LOCAL VARIABLES That sounds like local variables Just as local variables are invisible outside of the function that owns them variables used as parameters inside a function definition are local to that function Parameters in a function definition are really local variables that are created and assigned values automatically when the function is called TYPES OF FUNCTIONS User defined functions has output like built in functions Output can be functions functions printed something to the console or screen Functions that return a value are called fruitful Functions that do not return a value are called void FRUITFUL FUNCTIONS Return keyword Return keyword VOID FUNCTIONS IN CLASS EXAMPLE Using functions write a program that prompts the user for 3 numbers and outputs the average of those numbers GLOBAL VARIABLES Global variable created by assignment statement written outside all the functions Can be accessed by any statement in the program file including from within a function DO NOT USE GLOBAL VARIABLES Global variables making debugging difficult Many locations in the code could be causing a wrong variable value Functions that use global variables are usually dependent on those variables Makes function hard to transfer to another program Global variables make a program hard to understand GLOBAL CONSTANTS Global constant global name that


View Full Document

UNC-Chapel Hill COMP 116 - Functions

Download Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functions and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?