Course A201: Introduction to Programming

Slides:



Advertisements
Similar presentations
Outline What Is Function ? Create Function Call Function Parameters Functions Function Returning Values PHP Variable Scopes Passing by Reference Vs Value.
Advertisements

Course A201: Introduction to Programming 11/11/2010.
Functions Jay Summet. Aug Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)
An Introduction to Programming with C++ Fifth Edition
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
CHAPTER 6 Functions. Function Overview We’ve used built-in functions:  Examples:  print(“ABC”, x+10, sep=“:”)  round(x * 5, 2)  pygame.draw.circle(screen,
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
Course A201: Introduction to Programming 11/04/2010.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Functions Chapter 6. Function Overview We’ve used built-in functions: – Examples: print(“ABC”, x+10, sep=“:”) round(x * 5, 2) pygame.draw.circle(screen,
JavaScript III Functions and Abstraction. 2 JavaScript so far statements assignment function calls data types numeric string boolean expressions variables.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Python Functions.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Python Let’s get started!.
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Programming Fundamentals Enumerations and Functions.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
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.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
C++ Lesson 1.
Chapter 9: Value-Returning Functions
User-Written Functions
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 7 Pointers and C-Strings
Python Let’s get started!.
Functions Jay Summet.
Predefined Functions Revisited
Chapter 10: Void Functions
Topic: Functions – Part 2
FUNCTIONS IN C++.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Functions CIS 40 – Introduction to Programming in Python
Error Handling Summary of the next few pages: Error Handling Cursors.
User-Defined Functions
Chapter 9 Inheritance and Polymorphism
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.
More on Functions This lecture provides more ways of using functions
Dr. Bhargavi Dept of CS CHRIST
Introduction to Visual Programming
Topics Introduction to Functions Defining and Calling a Void Function
Functions Institute for Personal Robots in Education (IPRE)‏
Basics (Cont...).
Chapter 9: Value-Returning Functions
Functions Jay Summet.
Introduction to Value-Returning Functions: Generating Random Numbers
G. Pullaiah College of Engineering and Technology
Topics Introduction to Functions Defining and Calling a Function
15-110: Principles of Computing
Functions Institute for Personal Robots in Education (IPRE)‏
Introduction to C++ Programming Language
Functions Institute for Personal Robots in Education (IPRE)‏
Functions Jay Summet.
Predefined Functions Revisited
Introduction to Computer Science
Scope Scope is the space within which a variable label exists and can be used. Python talks of names being bound to the code in an area and that determining.
Functions So far we've been using functions from the builtin module, which is automatically loaded in most cases. Occasionally we've accessed functions.
What is a Function? Takes one or more arguments, or perhaps none at all Performs an operation Returns one or more values, or perhaps none at all Similar.
Constructors & Destructors
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
 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.
Functions Jay Summet.
Parameters and Arguments
Scope Rules.
Presentation transcript:

Course A201: Introduction to Programming 12/9/2010

Review – Part 2 Functions Global variables vs local variables

I will perform a certain job. Recall Function [functions] input1, input2, input3, … You give me some inputs I am a function. I will perform a certain job. I give you some outputs back, explicitly or implicitly output1, output2, output3, …

Functions: define and call def circle_area(radius): return (3.14159 * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) This is the definition of a function. This block define what job will circle_area perform, input and output. This is how you call the function: name and parentheses

Functions: define and call def instructions(): print(“““Welcome to the greatest intellectual challenge of all time: Tic-Tac-Toe. This will be a showdown between your human brain and my silicon processor.”””) instructions() This is the definition. This is how you call the function: name and parentheses

Functions: Return values So, from the above two examples: Function circle_area() does return a value, which is the area of a circle Function instructions() does NOT return a value, and by default the return value will be None The keyword return

Functions: Return values You have two functions: def circle_area1(radius): area = 3.14159 * radius ** 2 return area def circle_area2(radius): print(area)

Functions: Return values 1) What’s the output of print(circle_area1(5)) 2) And, what’s the output of print(circle_area2(5)))

Functions: Return values 1) What’s the output of print(circle_area1(5)) 2) And, what’s the output of print(circle_area2(5))) When a function doesn’t have a return statement in its definition, by default the return value will be None 78.53975 78.53975 None

Functions: Return values Be careful with the requirement, if it tells you to write a function which return the value, then you need to write a return statement It is always better to store the return value in a variable if the function has the return statement. area = circle_area1(5) print(area) This is called: Catch the return value!

Parameters and Arguments def circle_area(radius): return (3.14159 * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) Parameters Arguments

Parameters and Arguments def add(a, b): return (a+b) add(2, 4) Parameters Arguments

Parameters and Arguments Parameters are essentially variable names inside the parentheses of a function header: add(a,b), a and b are parameters of the function add You can have 0, 1 or many parameters The function receives values/arguments through its parameters e.g. add(1,2) The code in the function add now treats a and b as variables with values 1 and 2

Parameters and Arguments Example from the final practice: def newYearGreeting(name= "None", year= 2010): print("Happy New Year,", name, "!", "It is", year, "!\n")

Parameters and Arguments Example from the final practice:

Parameters and Arguments 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 header is perfectly fine: def monkey_around(bananas = 100, barrel_of = "yes"): But this isn't: def monkey_around(bananas = 100, barrel_of): The above header will generate an ERROR.

Global variables Global variables: variables that are initialized in the main script Local variables: variables that are initialized in the definition of a function

Global variables def my_function(): value = 1 print(“In the local scope, value is”, value) value = 10 print(“In the global scope, value is”, value) Two ‘value’s have the same names, but they don’t refer to the same variable Local variable Global variable

Global variables If you have a family friend named Bill Clinton. While in your neighborhood, ‘Bill Clinton’ refers to this friend by default. But out of your neighborhood, ‘Bill Clinton’ refers to the formal president by default.

Global variables Local variable or global variable? def my_function1(): value = 1 print(“In my function 1, value is”, value) def my_function2(): print(“In my function 2, value is”, value) value = 10 print(“In the global scope, value is”, value) Local variable or global variable?

Global variables This is a global variable. def my_function1(): value = 1 print(“In my function 1, value is”, value) def my_function2(): print(“In my function 2, value is”, value) value = 10 print(“In the global scope, value is”, value) This is a global variable.

Global variables So, if there is an assignment statement, such as value = 1, in the definition of your local function, this value will be automatically considered as you create a new variable in your function, with the same name, but refers to a different value. This variable has nothing to do with the global one. But if there is no assignment statement in your local function, as the above example, it will be considered as you are referring to the global variable The ‘nearest’ policy

Global variables Then, how to change a global variable in your local function? Use the keyword: global Compare two examples

Global variables def my_function(): value = 1 value = 10 print(“At first, value is”, value) my_function() print(“Now, value is”, value) Output: At first, value is 10 Now, value is 10

Global variables You explicitly declare this is a global variable def my_function(): global value value = 1 value = 10 print(“At first, value is”, value) my_function() print(“Now, value is”, value) Output: At first, value is 10 Now, value is 1 You explicitly declare this is a global variable

Global variables To sum up: Situation 1: def my_function(): value = 1 print(“In my function 1, value is”, value) value = 10 my_function() print(“In the global scope, value is”, value) This is a local variable. They are different.

Global variables To sum up: This is a global variable. Situation 2: def my_function(): print(“In my function 1, value is”, value) value = 10 my_function() print(“In the global scope, value is”, value) This is a global variable. Both of them refer to the same variable.

Global variables To sum up: Situation 3: def my_function(): global value value = 1 print(“In my function 1, value is”, value) value = 10 my_function1() print(“In the global scope, value is”, value) They now refer to the same variable, because you used the key word global. Global ‘value’ is changed to 1.