270 likes | 274 Views
Topics. Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments to Functions Global Variables and Global Constants. Benefits of Modularizing a Program with Functions. The benefits of using functions include:
E N D
Topics • Introduction to Functions • Defining and Calling a Void Function • Designing a Program to Use Functions • Local Variables • Passing Arguments to Functions • Global Variables and Global Constants
Benefits of Modularizing a Program with Functions • The benefits of using functions include: • Simpler code • Code reuse • write the code once and call it multiple times • Better testing and debugging • Can test and debug each function individually • Faster development • Easier facilitation of teamwork • Different team members can write different functions
Introduction to Functions • Examples of built-in functions: input, float, int, etc • Group of statements within a program that perform a specific custom task • A task of a large program with multiple tasks • Functions can be executed in order to perform overall program task • Known as divide and conquer approach • Modularized program: A large task or program is broken up into smaller tasks or functions
Types of Functions • A function can just execute statements or return a value to the function that called it • A void function • Simply executes the statements it contains and then terminates • A value-returning function • Executes the statements it contains then it returns a value back to the statement that called it • Ex: input, int, and float functions
Naming a Function • It follows the same rules as variable naming • Function name should be descriptive of the task carried out by the function • Often includes a verb • Cannot use key words as a function name • Cannot contain spaces • First character must be a letter or underscore • All other characters must be a letter, number or underscore • Uppercase and lowercase characters are distinct
Defining a Function • Function must be defined before they are used • Could be in beginning of program before main program starts • Function header: first line of function • Includes keyword def and function name, followed by parentheses and colon • Block: set of statements that belong together as a group • Example: the statements included in a function • Function definition template def function_name(): statement statement
Definition and Calling Function Example # This program demonstrates a void function. # First, we define a function named message. # Notice statements in function are indented since that shows # where the function statements begin and end # Statements in main are not indented def message(): print('I am Arthur') print('King of the Britons') # Main program # Call the message function. print (“Hello”) message() Output Hello I am Arthur King of the Britons
Example Function • Notice that the function is defined before it is used • The first statement to be executed is not in the function even though it is listed first • The first statement to be executed is in the main program • Then the function is executed which executes the statements in the function
Calling a Function within Function • You can call one function within another function • You cannot call the main program • This allows you to break up the code to be more readable and repeat things with custom values
Designing a Program to Use Functions • In a flowchart, a function call is shown as a rectangle with vertical bars on each side • Function name written in the symbol • Typically draw separate flow chart for each function in the program • End terminal symbol usually reads Return • Top-down design: technique for breaking algorithm into functions
Variable Types • Global – value can be seen in all functions • Local – value can only be seen in function that created it • Parameter – used when passing a value to a function
Local Variables • 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 be generated if accessed from outside the function • Scope: the part of a program that recognizes a variable • For local variable – only in function that created it
Variables Definition and Usage • Define local variables first • Cannot be accessed by statements inside its function before it is created just like before • Different functions may have local variables with the same name • Each function does not see the other function’s local variables • Otherwise there would be confusion between functions
Problem With Local Variable # Definition of the name function. def name(): get_name() print('Hello', name) # This causes an error! # Definition of the get_name function. def get_name(): name = input('Enter your name: ') # Call the name function. name()
Arguments • Argument: value (data) that is sent into a function • Function can use argument in calculations • Argument is placed in parentheses after function name • Received by parameter in function • If variable is used, its value cannot be changed • Only value is sent to function
Parameter Variable • Assigned the value of an argument when the function is called • The parameter and the argument have same value • General format: • def function_name(parameter): • Scope of a parameter: only the function in which the parameter is used
Passing Multiple Arguments • Python allows writing a function that accepts multiple arguments • Parameter list items are separated by comma • Arguments are passed by position to corresponding parameters • First parameter receives value of first argument, second parameter receives value of second argument, etc.
Multiple Parameters In Action # multiple_args.py # This program demonstrates a function that accepts two arguments. def main(): print('The sum of 12 and 45 is') show_sum(12, 45) # The show_sum function accepts two arguments and displays their sum. def show_sum(num1, num2): result = num1 + num2 print(result) # Call the main function. main()
Making Changes to Parameters • Changes made to a parameter value within the function do not affect the argument • Known as pass by value • Provides a way for unidirectional communication between one function and another function • Calling function can communicate with called function
Making Changes to Parameters In Use • Figure 5-18 • The value variable passed to the change_me function cannot be changed by it