Fundamentals of Programming I Default and Optional Parameters

Slides:



Advertisements
Similar presentations
Object-Oriented Programming
Advertisements

1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Fundamentals of Python: From First Programs Through Data Structures Chapter 2 Software Development, Data Types, and Expressions.
Chapter 7: User-Defined Functions II
Computer Science 111 Fundamentals of Programming I Dictionaries.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
CS150 Introduction to Computer Science 1
PSU CS 106 Computing Fundamentals II VB Subprograms & Functions HM 4/29/2008.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Fundamentals of Python: First Programs
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Computer Science 101 Introduction to Programming.
General Computer Science for Engineers CISC 106 Lecture 04 Dr. John Cavazos Computer and Information Sciences 09/10/2010.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Executable Statements 1. Def. Executable statements are instructions to the computer to perform specific tasks. 2. Two examples: method calls and assignment.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Computer Science 111 Fundamentals of Programming I Default and Optional Parameters Higher-Order Functions.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
23/07/2016CSE1303 Part B lecture notes 1 Introduction to computer systems Lecture B01 Lecture notes section B01.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introduction to Programming
Fundamentals of Programming I Higher-Order Functions
Fundamentals of Programming I Overview of Programming
Programming what is C++
Math class Random() method Floor method Top-level parseInt function
Arithmetic Expressions Function Calls Output
Fundamentals of Programming I Dictionaries
Predefined Functions Revisited
CS-104 Final Exam Review Victor Norman.
FIGURE 4-10 Function Return Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Fundamentals of Programming I Managing the Namespace
Section 3.2c Strings and Method Signatures
FIGURE 4-10 Function Return Statements
Conversions of the type of the value of an expression
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Passing Parameters by value
FIGURE 4-10 Function Return Statements
5. Functions Rocky K. C. Chang 30 October 2018
Computer Science 111 Fundamentals of Programming I
Assignment Operators Topics Increment and Decrement Operators
Summary of what we learned yesterday
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
COMPUTER PROGRAMMING SKILLS
In this class, we will cover:
Predefined Functions Revisited
FIGURE 4-10 Function Return Statements
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
CISC101 Reminders Assignment 3 due today.
Templates CMSC 202, Version 4/02.
Assignment Operators Topics Increment and Decrement Operators
The return Statement © 2018 Kris Jordan.
Introduction to Computer Science and Object-Oriented Programming
Defining Functions.
Presentation transcript:

Fundamentals of Programming I Default and Optional Parameters Computer Science 111 Fundamentals of Programming I Default and Optional Parameters

Arguments and Return Values A function can receive data (arguments) from its caller A function can return a single value to its caller y = math.sqrt(x)

Why Use Parameters? Parameters allow a function to be used with different data in different parts of a program The general method or algorithm is the same, but the arguments vary with the situation >>> math.sqrt(2) 1.4142135623730951 >>> math.sqrt(16) 4.0 >>>

Default and Optional Parameters One or more parameters can have default values, so the caller can omit some arguments >>> round(3.1416) # Default precision is 0 3 >>> round(3.1416, 3) 3.142 >>> list(range(5)) # Default lower bound is 0, and [0,1,2,3,4] # default step value is 1 >>> list(range(1, 5)) [1,2,3,4] >>> list(range(1, 5, 2)) [1,3]

Convert Based Numbers to ints Write a general function that expects a string representation of a number and its base (an int) as arguments The function returns the integer represented >>> repToInt('10', 2) # 102 = 2 2 >>> repToInt('10', 10) # 1010 = 10 10 >>> repToInt('10', 16) # 1016 = 16 16 >>> repToInt('100', 2) # 1002 = 4

Implementation def repToInt(digits, base): """Returns the integer represented by the digits in the given base.""" intValue = 0 expo = len(digits – 1) for ch in digits: ch = string.upper(ch) intvalue += hexdigits[ch] * base ** expo expo -= 1 return intValue

Default and Optional Parameters One or more parameters can have default values, so the caller can omit some arguments >>> repToInt('111', 2) 7 >>> repToInt('111', 10) 111 >>> repToInt('111', 16) 273 >>> repToInt('111') # Same result as the previous line The caller can treat base16 as the standard base in this system or use other bases by mentioning them

Implementation def repToInt(digits, base = 16): """Returns the integer represented by the digits in the given base.""" intValue = 0 expo = len(digits – 1) for ch in digits: ch = string.upper(ch) intvalue += hexdigits[ch] * base ** expo expo -= 1 return intValue

Some Syntax Rules The required arguments used in a function call must match the required parameters named in the definition, by position The programmer should list the required parameters first (to the left) in the function’s definition def <function name>(<required params>, <default params>):

Some Syntax Rules A required parameter is just a name A default parameter looks like an assignment statement def <function name>(<name>,…, <name> = <expression>,…):

Section 6.6 Higher-Order Functions For Wednesday Section 6.6 Higher-Order Functions