FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน สามารถใช้ชื่อใดๆ ตามกฎการตั้งชื่อ.

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Functions in Python. The indentation matters… First line with less indentation is considered to be outside of the function definition. Defining Functions.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
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.
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
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.
Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
ECE122 Feb. 22, Any question on Vehicle sample code?
For loops in programming Assumes you have seen assignment statements and print statements.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
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.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Functions Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
INLS 560 – F UNCTIONS Instructor: Jason Carter.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Week 8 - Friday.  What did we talk about last time?  Static methods.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Review Expressions and operators Iteration – while-loop – for-loop.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6A Methods (Concepts)
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
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.
String and Lists Dr. José M. Reyes Álamo.
Chapter 10: Void Functions
Chapter 6 Functions.
Functions CIS 40 – Introduction to Programming in Python
Functions.
User-Defined Functions
Call Stacks, Arguments and Returns
Chapter 3 Simple Functions
Spot the bug!.
Topics Introduction to Functions Defining and Calling a Void Function
G. Pullaiah College of Engineering and Technology
Topics Introduction to Functions Defining and Calling a Function
CISC101 Reminders All assignments are now posted.
Object Oriented Programming in java
Introduction to Computer Science
General Computer Science for Engineers CISC 106 Lecture 03
Chapter 10: Void Functions
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
def-ining a function A function as an execution control structure
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
Week 7 - Friday CS 113.
Presentation transcript:

FUNCTIONS

• Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน สามารถใช้ชื่อใดๆ ตามกฎการตั้งชื่อ ยกเว้น keyword • มีคำสั่งได้ไม่จำกัดจำนวน แต่ต้องเยื้องเข้าไป จาก def ( ควรใช้เป็นมาตรฐาน อาทิ 4 spaces)

FUNCTION PATTERN • A header line which begins with a keyword and ends with colon. • A body consisting of one or more statements, each indented, each indented the same amount (4 spaces) from the head line. def NAME(PARAMETERS) : STATEMENTS

The parameters of the function • draw_square has two parameter – One to tell the function which turtle to move around – The other to tell it the size of the square

Run the function • Defining a new function does not make the function run. • To run the function, we need a function call

Function Call

Functions can call other functions • We want function to draw a rectangle. • We need to be able to call the function with different argument for width and height. And, unlike the case of the square.

A new version of draw_square • A square is a special kind of rectangle. • We can use draw_rectangle function to draw a square. • Functions can call other functions.

• A caller of this function might say draw_square(tess, 50) • tx and sz are assigned the values of the tess object and the int 50 Function Call

Flow of execution • Execution always begins as the first statement of the program. • Statements are executed one at a time, in order from top to bottom. • The statements inside the function are not executed until the function is called.

Put the mouse cursor on the line of the program where create the turtle screen and press F4 key

At this point press the F7 key repeatedly to single step through the code. When start to get bored,we can use the key F8 to step over the function

Functions that require arguments • Most functions require arguments • Some function takes more than one arguments

Functions that return values • A function that returns a value is called a fruitful function • The opposite of fruitful function is void function : one that is not executed for its resulting value, but is executed because it does something useful.

Void functions • Python always wants to return something. So if the programmer doesn’t arrange to return a value, Python will automatically return the value None.

Variables and parameters are local • When a local variable is created inside a function, it only exists inside the function, we cannot use it outside • Cannot use the variable a outside the function

Variable lifetime • The variable a only exists while the function is being executed.

Parameters • Parameters are local and act like local variables. • The lifetimes of p, r, n, t – begin when final_amt is called – Ends when the function completes its execution.

Each call of the function • Each call of the function creates new local variables. • And their lifetimes expire when the function returns to the caller.