Positional Parameters and Global Variables

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Course A201: Introduction to Programming 11/11/2010.
Guide to Programming with Python
Chapter 5 Functions.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
24 Hour Clock Grade 6 Math.
4.8 Quadratic Formula and Discriminant
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
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.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Notes Over 5.6 Quadratic Formula
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
Warm-Up How can you determine whether two lines are parallel or perpendicular or neither? 4.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
Intro to CS Nov 29, 2016.
OOP: Encapsulation &Abstraction
Excel IF Function.
Course A201: Introduction to Programming
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Object-Oriented Programming: Classes and Objects
Week 3 - Friday CS222.
CIS 200 Test 01 Review.
Encapsulation, Data Hiding and Static Data Members
Some Eclipse shortcuts
Spreadsheet-Based Decision Support Systems
Chapter 6 Functions.
Functions.
Object-Oriented Programming: Classes and Objects
Reference parameters (6.2.3)
User Defined Functions
Class Notes 11.2 The Quadratic Formula.
Warm-Up Respiratory Therapist Carol has two patients scheduled to get albuterol treatments. Patient 1 has treatments at 12:30pm, 8:30pm and 4:30am Patient.
6 Chapter Functions.
Find all solutions of the polynomial equation by factoring and using the quadratic formula. x = 0 {image}
MILITARY TIME.
Introduction To The 24-Hour Clock
Coding Concepts (Sub- Programs)
CISC101 Reminders Assn 3 due tomorrow, 7pm.

We’re slowly working our way towards classes and objects
Dr. Bhargavi Dept of CS CHRIST
Quadratic Formula & the Discriminant
Chapter 4 Topics: class declarations method declarations
Java Inheritance.
Recap Week 2 and 3.
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 10: Method Overriding and method Overloading
Tonga Institute of Higher Education
Week 4 Lecture-2 Chapter 6 (Methods).
Topics Introduction to Functions Defining and Calling a Function
COP 3330 Object-oriented Programming in C++
15-110: Principles of Computing
Warm-Up Respiratory Therapist Carol has two patients scheduled to get albuterol treatments. Patient 1 has treatments at 12:30pm, 8:30pm and 4:30am Patient.
Introduction to Computer Science
Using the Quadratic Formula to Solve Quadratic Equations
CISC101 Reminders Assignment 3 due today.
Object-Oriented Design AND CLASS PROPERTIES
ITM 352 Functions.
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Variables.
Parameters and Arguments
Presentation transcript:

Positional Parameters and Global Variables Chapter 6 Part 2 Positional Parameters and Global Variables

Positional Parameters vs. Default Parameters Positional Parameters and positional arguments mean that parameters get their values based solely on the position of the values sent. Ex. def birthday1(name, age) Name and age are positional parameters Ex. birthday1(“Jackson”, 1) “Jackson” and 1 are positional arguments The first parameter gets the first value sent, the second parameter gets the second value sent. If you switch the arguments, birthday1(1, “Jackson”) then name gets 1 and age gets Jackson, that is unless you specify.

Keyword Arguments Keyword arguments let you assign certain values to specific parameters regardless of order. If you say: birthday1(name = “Jackson”, age = 1) Or Birthday1(age = 1, name = “Jackson”) Name gets Jackson and age gets 1 in both scenarios. Keyword arguments let you pass values in any order. But their biggest benefit is clarity. Using them gives you a better idea of what the values represent.

Default Parameter Values You have the option to assign default values to your parameters – values that get assigned to the parameters if no value is passed to them. Ex. def birthday2(name = “Jackson”, age = 1): This means if no value is given for name, it gets Jackson, and if no value is given for age it gets 1. So birthday2( ) won’t generate an error.

Default Values You can override default values: birthday2(name = “Katherine”) name is overridden birthday2(age = 12) age is overridden birthday2(name = “Katherine”, age = 12) both are overridden birthday2(“Katherine”, 12) gets the same results as previous. *Note: Once you assign a default value to a parameter in the list, you have to assign default values to all the parameters listed after it. So this function is fine: def food(fave = “pizza”, second = “spaghetti”, third = “chocolate”) But this one is not: def food(fave = “pizza”, second, third)

Global Variables and Constants Through encapsulation, functions are totally sealed off and independent from each other and the main part of the program. The only way to get information to them is through their parameters, and the only way to get information out of them is through their return values. That is unless you use global variables Scopes – different areas of your program that are separate from each other.

Scopes variable 1 and 2 are local Variable 0 is global def func1() variiable 0 variable 1 and 2 are local Variable 0 is global You could have two variables In function 1 and function 2 with the same name and they wouldn’t affect one another. It is different for global variables and local variables that have the same name def func1() variable1 def func2() variable 2

Global Reach Program -If you give a variable inside a function the same name as a global variable, you shadow the global variable. This is, you hide it with your new variable. When you change its value, it only changes the value of the local variable. *It is not a good idea to shadow a global variable. It can cause confusion when you think you are using a global value but you are not. - If you want to gain complete access to a global variable, us the keyword “global”, then the function has complete access to the value, so if it is changed in the local scope, it will also be changed in the global scope. Ex. Change_global()

Preparing for the Ch. 6 Test Write a function that takes 3 integer parameters and returns the smallest of the 3. Write a function that takes 3 integer values (the coefficients of a quadratic equation) and returns the positive value of the quadratic formula (don’t worry about imaginary solutions). Write a function that takes one integer parameter. It should return true if the integer is positive and false if it is not positive. Write a function that takes 3 strings and returns one string which is the 3 strings put together with a ? between each word. Example: If I run the function with the strings “This” “is” “test” the function would return “This?is?test” (without the “ marks). Write a function that takes 2 integer parameters, one is hour the other is minute. Have your function return a string that is the time. The time will be in military time when you get it so hours of 13 is 1 pm etc. Also for midnight and noon have your function return the words midnight or noon. Call your function time. (10 points) Example: time(3,17) returns the string 3:17 am. Example: time(13,2) returns 1:02 pm Example time(0,0) should return midnight Example time(12,0) should return noon