中国高校课件下载中心 》 教学资源 》 大学文库

上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture04 MODULAR PROGRAMMING Functions

文档信息
资源类别:文库
文档格式:PDF
文档页数:79
文件大小:740.1KB
团购合买:点击进入团购
内容简介
上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture04 MODULAR PROGRAMMING Functions
刷新页面文档预览

上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Computational Thinking and Approach Lecture 4 Dr.Jialiang LU Jialiang.lu@situ.edu.cn

Computational Thinking and Approach Lecture 4 Dr. Jialiang LU Jialiang.lu@sjtu.edu.cn 1

上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions MODULAR PROGRAMMING 2

MODULAR PROGRAMMING Functions 2

上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Motivations Divide huge tasks to blocks:divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions to reduce code duplication and increase program modularity. ·Easy to: -Design→Implement→Test→Maintain→Reuse 3

Motivations • Divide huge tasks to blocks: divide programs up into sets of cooperating functions. • Define new functions with function calls and parameter passing. • Use functions to reduce code duplication and increase program modularity. • Easy to: – Design  Implement  Test  Maintain  Reuse 3

上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Function of Functions Decomposition and abstraction through functions Break up into modules Suppress detail Create "new primitive' 4

Function of Functions • Decomposition and abstraction through functions – Break up into modules – Suppress detail – Create “new primitive” 4

上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions,Informally A function is like a subprogram,a small program inside of a program. The basic idea-we write a sequence of statements and then give that sequence a name.We can then execute this sequence at any time by referring to the name. 5

5 Functions, Informally • A function is like a subprogram, a small program inside of a program. • The basic idea – we write a sequence of statements and then give that sequence a name. We can then execute this sequence at any time by referring to the name

上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions,Informally The part of the program that creates a function is called a function definition. -def functionName(): When the function is used in a program,we say the definition is called or invoked. functionName() 6

6 Functions, Informally • The part of the program that creates a function is called a function definition. – def functionName(): • When the function is used in a program, we say the definition is called or invoked. – functionName()

上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY The Function of Functions e So far,we've seen three different types of functions: - Our programs comprise a single function called main(). Built-in Python functions(type() -Functions from the standard libraries (string.split()) 7

7 The Function of Functions • So far, we’ve seen three different types of functions: – Our programs comprise a single function called main(). – Built-in Python functions (type()) – Functions from the standard libraries (string.split())

上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions,Informally Happy Birthday lyrics... def main(): print"Happy birthday to you! print"Happy birthday to you!" print"Happy birthday,dear Fred..." print"Happy birthday to you!" Gives us this... >>main() Happy birthday to you! Happy birthday to you! Happy birthday,dear Fred... Happy birthday to you! 8

8 Functions, Informally • Happy Birthday lyrics… def main(): print"Happy birthday to you!" print"Happy birthday to you!" print"Happy birthday, dear Fred..." print"Happy birthday to you!" • Gives us this… >>> main() Happy birthday to you! Happy birthday to you! Happy birthday, dear Fred... Happy birthday to you!

上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions,Informally There's some duplicated code in the program! (print "Happy birthday to you!" We can define a function to print out this line: def happy(): print"Happy birthday to you!" With this function,we can rewrite our program. 9

9 Functions, Informally • There’s some duplicated code in the program! (print "Happy birthday to you!") • We can define a function to print out this line: def happy(): print"Happy birthday to you!" • With this function, we can rewrite our program

上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions,Informally 。The new program- def singFred(): happy ( happy ( print"Happy birthday,dear Fred..." happy ( Gives us this output- >>singFred() Happy birthday to you! Happy birthday to you! Happy birthday,dear Fred... Happy birthday to you! 10

10 Functions, Informally • The new program – def singFred(): happy() happy() print"Happy birthday, dear Fred..." happy() • Gives us this output – >>> singFred() Happy birthday to you! Happy birthday to you! Happy birthday, dear Fred... Happy birthday to you!

刷新页面下载完整文档
VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
相关文档