Topics Introduction to Functions Defining and Calling a Void Function

Slides:



Advertisements
Similar presentations
Programming Logic and Design Fourth Edition, Introductory
Advertisements

C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Introduction to Methods
Chapter 6: Functions.
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Input, Output, and Processing
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
INLS 560 – F UNCTIONS Instructor: Jason Carter.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
Chapter 5 : Methods. Why Write Methods?  Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer.
Functions.
Topics Designing a Program Input, Processing, and Output
Topics Introduction to Functions Defining and Calling a Void Function
COMP 170 – Introduction to Object Oriented Programming
CS 1110 Introduction to Programming Spring 2017
Chapter 3: Using Methods, Classes, and Objects
Chapter 6 Functions.
by Tony Gaddis and Godfrey Muganda
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Functions CIS 40 – Introduction to Programming in Python
CSCI 161: Introduction to Programming Function
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Methods.
User-Defined Functions
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 2 Applications and Data.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter Topics Chapter 5 discusses the following main topics:
Starting Out with Programming Logic & Design
© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved.
Chapter 3 Simple Functions
Chapter 3 Introduction to Classes, Objects Methods and Strings
Python Primer 2: Functions and Control Flow
Starting Out with Java: From Control Structures through Objects
Chapter 4 void Functions
6 Chapter Functions.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
Introduction to Value-Returning Functions: Generating Random Numbers
Topics Introduction to Functions Defining and Calling a Function
CISC101 Reminders All assignments are now posted.
Topics Designing a Program Input, Processing, and Output
A function is a group of statements that exist within a program for the purpose of performing a specific task. We can use functions to divide and conquer.
Starting Out with Programming Logic & Design
CS 1111 Introduction to Programming Spring 2019
Topics Designing a Program Input, Processing, and Output
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Loops and Simple Functions
COMPUTER PROGRAMMING SKILLS
12th Computer Science – Unit 5
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
CPS125.
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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

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

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

Types of Returning Functions A value-returning function: Executes the statements it contains then it returns a value back to the statement that called it. The input, int, and float functions are examples of value-returning functions. A void function: Simply executes the statements it contains and then terminates.

Defining a Function Functions are given names Function naming rules 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 name should be descriptive of the task carried out by the function Often includes a verb Function definition: specifies what function does def function_name(): statement

Defining a Function Must be defined before 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

Calling a Function Call a function to execute it When a function is called Interpreter jumps to the function and executes its statements Interpreter jumps back to where the program called the function Known as function return

Definition and Calling Function Example # This program demonstrates a function. # First, we define a function named message. def message(): print('I am Arthur') print('King of the Britons') # Call the message function. message() Output I am Arthur King of the Britons

Calling a Function main function: called when the program starts Calls other functions when they are needed Defines the mainline logic of the program

Main and Two Function Example # This program has two functions. First we define the main function. def main(): print('I have a message for you.') message() print('Goodbye!') # Next we define the message function. def message(): print('I am Arthur') print('King of the Britons.') # Program starts here by calling the function labeled as main. Main() Output I have a message for you. I am Arthur King of the Britons. Goodbye!

Indentation in Python 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 IDLE automatically indents the lines in a block Blank lines that appear in a block are ignored

Designing a Program to Use Functions In a flowchart, function call shown as rectangle with vertical bars at 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

Function Flowchart

Flow Using Hierarchy Chart Depicts relationship between functions AKA structure chart Box for each function in the program, Lines connecting boxes illustrate the functions called by each function Does not show steps taken inside a function Use input function to have program wait for user to press enter

Hierarchy Chart

Detailed Program Hierarchy Chart See acme_dryer.py

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 Cannot be accessed from outside the function Scope: the part of a program that recognizes a variable For local variable – only in function that created it

Local Variables Define local variables first Cannot be accessed by statements inside its function before it is created Different functions may have local variables with the same name Each function does not see the other function’s local variables, so no confusion

Problem With Local Variable # Definition of the main function. def main(): 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 main function. main()

Local Variables Example # Demonstrates two functions that have local variables with same name. def main(): texas() # Call the texas function california() # Call the california function # texas function creates a local variable named birds. def texas(): birds = 5000 print('texas has', birds, 'birds.') # california function also creates a local variable named birds. def california(): birds = 8000 print('california has', birds, 'birds.') main() # Call the main function

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

Passing Arguments to Functions Example

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

Parameter In Use

Parameter Demonstration # pass_arg.py # This program demonstrates an argument being passed to a function. def main(): value = 5 show_double(value) # The show_double function accepts an argument # and displays double its value. def show_double(number): result = number * 2 print(result) # Call the main function. main()

Passing Multiple Arguments Python allows writing a function that accepts multiple arguments Parameter list replaces single parameter Parameter list items 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.

Passing Multiple Arguments Example

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

Making Changes to Parameters In Use (Continued) Figure 5-18 The value variable passed to the change_me function cannot be changed by it