Global Variables Created by assignment statement placed at beginning of program and outside all functions Can be accessed by any statement in the program.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Python quick start guide
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: Functions Starting Out with C++ Early Objects
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Functions CMSC 201. Motivation Using the tools we have so far, we can easily write code to find the largest number in a list, right?
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
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.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
Python Let’s get started!.
COP 2510 Chapter 6 - Functions. Random function (randint) #import the desired function import random #integer random number in the range of 1 through.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Programming Logic and Design Seventh Edition
Topics Introduction to Functions Defining and Calling a Void Function
REPETITION CONTROL STRUCTURE
Python Let’s get started!.
Python: Control Structures
Value-Returning Functions
The Selection Structure
Chapter 5: Control Structure
3rd prep. – 2nd Term MOE Book Questions.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Agenda Control Flow Statements Purpose test statement
User-Defined Functions
An Introduction to Programming with C++ Fifth Edition
Chapter Topics Chapter 5 discusses the following main topics:
Value returning Functions
Functions and Procedures
Functions.
6 Chapter Functions.
Topics Introduction to Repetition Structures
Chapter 6 Control Statements: Part 2
Reading Input from the Keyboard
3. Decision Structures Rocky K. C. Chang 19 September 2018
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
Topics Introduction to Functions Defining and Calling a Void Function
Topics The if Statement The if-else Statement Comparing Strings
Basic String Operations
Introduction to Repetition Structures
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Topics Introduction to Functions Defining and Calling a Function
Python Basics with Jupyter Notebook
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Fundamental Programming
Another Example Problem
Standard Version of Starting Out with C++, 4th Edition
Lecture 1 Review of 1301/1321 CSE /26/2018.
Introducing Modularity
Flow Control I Branching and Looping.
Presentation transcript:

Global Variables Created by assignment statement placed at beginning of program and outside all functions Can be accessed by any statement in the program If a function needs to assign a value to the global variable, the global variable must be redeclared within the function General format: global variable_name

Global Variables Example # Create a global variable. number = 0 def main(): global number number = int(input('Enter a number: ')) show_number() def show_number(): #Notice no argument was passed print('The number you entered is', number) # Call the main function. main()

Global Variables Reasons to avoid using global variables- Global variables making debugging difficult Many locations in the code could be causing An unexpected 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 name that references a value that cannot be changed Permissible to use global constants in a program To simulate global constant in Python, create global variable and do not re-declare it within functions Like is done with variables

Value-Returning Functions void function: group of statements within a function that perform a specific task Value-returning function: returns a value Can be stored in a variable As a value in a program statement

return Statement Used in value-returning function to send a value back to where the function was called Format: return expression The value for expression will be returned Expression can be simple or a complex expression

Writing Your Own Value-Returning Functions

How to Use Value-Returning Functions Value-returning function can be useful in specific situations Prompt user for input and return the user’s input Simplify mathematical expressions Complex calculations that need to be repeated throughout the program Use the returned value Assign it to a variable or use as an argument in another function

Return Value Example def main(): first_age = int(input('Enter your age: ')) # Get the user's age second_age = int(input("Enter your best friend's age: ")) total = sum(first_age, second_age) # Get the sum of both ages print('Together you are', total, 'years old.') # The sum function accepts two numeric arguments and # returns the sum of those arguments. def sum(num1, num2): result = num1 + num2 return result # Call the main function. main()

Return Value Example Part 2 def main(): first_age = int(input('Enter your age: ')) # Get the user's age second_age = int(input("Enter your best friend's age: ")) total = sum(first_age, second_age) # Get the sum of both ages print('Together you are', total, 'years old.') # The sum function accepts two numeric arguments and # returns the sum of those arguments. def sum(num1, num2): return num1 + num2 # returns value of expression # Call the main function. main()

Modularizing With Functions Breaks large program into smaller functions Easier to troubleshoot and understand

Commission Rate Example # This program calculates a salesperson's pay # at Make Your Own Music. def main(): sales = get_sales() advanced_pay = get_advanced_pay() comm_rate = determine_comm_rate(sales) pay = sales * comm_rate - advanced_pay # Display the amount of pay. print('The pay is $', format(pay, ',.2f'), sep='') # Determine whether the pay is negative. if pay < 0: print('The salesperson must reimburse the company.')

Commission Rate Example # The get_sales function gets a salesperson's # monthly sales from the user and returns that value. def get_sales(): monthly_sales = float(input('Enter the monthly sales: ')) return monthly_sales # The get_advanced_pay function gets the amount of # advanced pay given to the salesperson and returns that amount. def get_advanced_pay(): print('Enter the amount of advanced pay, or') print('enter 0 if no advanced pay was given.') advanced = float(input('Advanced pay: ')) return advanced

Commission Rate Example def determine_comm_rate(sales): # Determine the commission rate. if sales < 10000.00: rate = 0.10 elif sales >= 10000 and sales <= 14999.99: rate = 0.12 elif sales >= 15000 and sales <= 17999.99: rate = 0.14 elif sales >= 18000 and sales <= 21999.99: rate = 0.16 else: rate = 0.18 return rate # Call the main function main()

Returning Strings Example # This program demonstrates passing two string arguments to a function. def main(): first_name = input('Enter your first name: ') last_name = input('Enter your last name: ') print('Your name reversed is') reverse_name(first_name, last_name) def reverse_name(first, last): print(last, first) # Call the main function. main()

Returning Boolean Values Boolean function: returns either True or False Use to test a condition such as for decision and repetition structures Common calculations, such as whether a number is even, can be easily repeated by calling a function Use to simplify complex input validation code

Returning Boolean Example def is_even(number): if (number % 2) == 0: status = True else: status = False return status #Program starts number = int(input(‘Enter a number: ‘)) if is_even(number): print(‘The number is even.’) print (‘The number is odd’)