Intro to CS Nov 29, 2016.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Python Objects and Classes
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CS346 Javascript -3 Module 3 JavaScript Variables.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Python Functions.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Function Basics. Function In this chapter, we will move on to explore a set of additional statements that create functions of our own function (subroutine,
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
 The real power of PHP comes from its functions; it has more than 1000 built-in functions.  PHP User Defined Functions  Besides the built-in PHP functions,
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Programming Right from the Start with Visual Basic .NET 1/e
Lesson 06: Functions Class Participation: Class Chat:
Chapter 9: Value-Returning Functions
Exam #1 You will have exactly 30 Mins to complete the exam.
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 6: User-Defined Functions I
Functions A function is a block of code with a name. function functionName() { code to be executed; }
Chapter 7: User-Defined Functions II
Introduction to Python
Chapter 3 Assignment and Interactive Input.
© 2010 David A Watt, University of Glasgow
Chapter 6 Functions.
Modern JavaScript Develop And Design
JavaScript: Functions
Functions CIS 40 – Introduction to Programming in Python
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
User-Defined Functions
CHAPTER FOUR Functions.
Chapter 4 - Visual Basic Schneider
Chapter 4 void Functions
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
6 Chapter Functions.
CMSC201 Computer Science I for Majors Lecture 14 – Functions (Continued) Prof. Katherine Gibson Based on concepts from:
CISC101 Reminders Assn 3 due tomorrow, 7pm.
PHP.
Lesson 06: Functions Class Chat: Attendance: Participation
Statement-Level Control Structures
5. Functions Rocky K. C. Chang 30 October 2018
Topics Introduction to Functions Defining and Calling a Void Function
Objectives In this chapter, you will:
MCS/BCS.
Chapter 6: User-Defined Functions I
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
G. Pullaiah College of Engineering and Technology
Topics Introduction to Functions Defining and Calling a Function
15-110: Principles of Computing
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Loops and Simple Functions
COMPUTER PROGRAMMING SKILLS
CSE 231 Lab 4.
CISC101 Reminders Assignment 3 due today.
ITM 352 Functions.
CPS125.
 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.
Presentation transcript:

Intro to CS Nov 29, 2016

Today Functions Understanding scope Arguments Return Value Generator

Function Definition def function-name(): statements-to-be-executed Function-name can not be a keyword or a built-in function

Functions Program flow resumes at the point directly following the function call Functions provide modularity and can be called repeatedly

Scope Variables created outside functions can be referenced by statements inside functions. They have global scope Variables created inside functions can not be referenced from outside the function. They have local scope.

Local Variables Local variables of the same name can appear in different functions without conflict You can coerce a local variable to make it global with the “global” keyword followed by the variable name only Where a global variable and a local variable have the same name, the function uses the local variable.

Scope of Variables global_var = 1 def my_vars(): print('Global variable:', global_var) local_var = 2 print('Local variable:', local_var) global inner_var inner_var = 3 my_vars() print('Coerced global:', inner_var)

Arguments An “argument” name can be specified between the parentheses in the function definition def echo (user): print(‘User:’, user) A value can be specified in the parentheses when the function is called echo(‘Mike’) The function can reference the “passed in” value via the argument name

Multiple Arguments Multiple arguments can specified by a comma-separated list The call to the function must include the same number of values as arguments The passed values must appear in the same order as the arguments Unless the arguments are specified

Multiple Arguments def echo(user, lang, sys): print('User:', user, 'Language:', lang, 'Platform:', sys) echo('Mike', 'Python', 'Windows') echo(lang='Python', sys='Mac OS', user='Anne')

Default Arguments Default value can be specified in the argument Used by the function when no value gets passed by the caller Default value gets overridden when the caller specifies a value for that argument

Default Arguments def mirror(user = 'Carole', lang = 'Python'): print('\nUser:', user, 'Language:', lang) mirror() mirror(lang = 'Java') mirror(user = 'Tony') mirror('Susan', 'C++')

Return Value A function can return a value to the caller using the “return” keyword The returned result may be assigned to a variable by the caller The returned result can be used directly in-line such as in a print statement

Return Value num = input('Enter an integer:') def square(num): if not num.isdigit(): return 'Invalid Entry' num = int(num) return num * num print(num, 'Squared is:', square(num))

Generator A generator is a special function that returns a “generator object” rather than a data value This retains the state of the function when it was last called It will continue from the point when next called

Yield Generator function contains the ‘yield’ keyword Specifies the generator object to be returned to the caller When the ‘yield’ statement gets executed, the state of the generator object is frozen and the current value is retained

Next The generator object returned by the yield statement can be assigned to a variable The built-in next() function can take the generator object to continue execution of the function from the point it was frozen

Generator example def incrementer(): i = 1 while True: yield i i += 1 incr = incrementer() print(next(incr))

Summary Functions Understanding scope Arguments Return Value Generator