Lecture 6Defining FunctionsFall, 2015Xiaojuan CaiComputational Thinking
Xiaojuan Cai Computational Thinking 1 Lecture 6 Defining Functions Fall, 2015
Objective. To understand why using functions. To be able to define new functions.. To understand the details of function calls andparameterpassing. To write programs that use functions to reducecode duplication and increase program modularity2XiaojuanCaiComputationalThinking
Xiaojuan Cai Computational Thinking 2 Objective • To understand why using functions. • To be able to define new functions. • To understand the details of function calls and parameter passing. • To write programs that use functions to reduce code duplication and increase program modularity
Roadmap. Functions, informally. Function definition and call: Getting results from functions· Return values? Modify params: Functions and program structureXiaojuan CaiComputational Thinking
Xiaojuan Cai Computational Thinking 3 Roadmap • Functions, informally • Function definition and call • Getting results from functions • Return values • Modify params • Functions and program structure
What is function?· A function is like a subprogram, a smallprogram inside of a program.. The basic idea:writeasequenceofstatements: give that sequence a name (definition)execute this sequence at any time by the name(functioncall)Xiaojuan CaiComputational Thinking
Xiaojuan Cai Computational Thinking 4 What is function? • A function is like a subprogram, a small program inside of a program. • The basic idea: • write a sequence of statements • give that sequence a name (definition) • execute this sequence at any time by the name (function call)
Functions so far: Our programs comprise a single function calledmain().: Built-in Python functions (abs): Functions from the standard libraries(math.sqrt): Functions from the graphics module (p.getx ())Computational ThinkingXiaojuanCai
Xiaojuan Cai Computational Thinking 5 Functions so far • Our programs comprise a single function called main(). • Built-in Python functions (abs) • Functions from the standard libraries (math.sqrt) • Functions from the graphics module (p.getX())