Bryan Burlingame 13 February 2019

Slides:



Advertisements
Similar presentations
08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Introduction to Computers and Programming Introduction to Methods in Java.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
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.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
The Java Programming Language
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Lecture 7: Modular Programming (functions) B Burlingame 05 October, 2016.
C++ First Steps.
Topics Introduction to Functions Defining and Calling a Void Function
Programming Logic and Design Seventh Edition
Working with Java.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Learning to Program D is for Digital.
Introduction to the C Language
Functions Review.
CS 326 Programming Languages, Concepts and Implementation
Objectives Identify the built-in data types in C++
Chapter 6 Functions.
Topic: Functions – Part 2
Chapter 5 Function Basics
JavaScript: Functions
Functions CIS 40 – Introduction to Programming in Python
Deitel- C:How to Program (5ed)
Functions.
Lecture 7: Modular Programming (functions)
CSCI 161: Introduction to Programming Function
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Lecture 7: Functions Revisited / Analogue Signals
Abstract Data Types and Encapsulation Concepts
Introduction to the C Language
Names, Binding, and Scope
Learning to Program in Python
Defining Classes and Methods
Functions, Procedures, and Abstraction
Chapter 6 Methods: A Deeper Look
Lecture 5 Fruitful Functions
Lecture 2 Python Programming & Data Types
Chapter 1: Computer Systems
Lecture 2 Python Programming & Data Types
Topics Introduction to Functions Defining and Calling a Void Function
Namespaces – Local and Global
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Focus of the Course Object-Oriented Software Development
Topics Introduction to Functions Defining and Calling a Function
15-110: Principles of Computing
In C Programming Language
Defining Classes and Methods
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Namespaces – Local and Global
CPS125.
Functions, Procedures, and Abstraction
 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.
Agenda for Unit 3: Functions
Presentation transcript:

Bryan Burlingame 13 February 2019 Lecture 3 Functions Bryan Burlingame 13 February 2019

Labs are due at the start of lab Announcements Labs are due at the start of lab Read chapters 4 & 5 Homework 1 due up front Homework 2 due next week

Learning Objectives Explain the concept of modular program design Explain the concept of a function in Python Explain why functions are important in programming Demonstrate the structure of a simple function

Modular Programming Break a large problem into smaller pieces Smaller pieces sometimes called ‘subroutines’,‘procedures’ or functions Why? Helps manage complexity Smaller blocks of code Easier to read Encourages re-use of code Within a particular program or across different programs Allows independent development of code Provides a layer of ‘abstraction’ Hides the details of complex solutions Python has several built-in functions Decomposing complex problems into manageable subsets and then implementing those subsets as a set of functions is a key concept and skill Imagine trying to develop a huge program like the Windows or Mac OS with hundreds of thousands (or millions of lines of code). No way to do it all in one big program file. Is the work of hundreds of computer programmers, each working on smaller pieces of the problem that are then brought together. By ‘abstraction’, I mean a way to simplify or separate the details of how a process works to an essential set of features that allow the process to be used. Example: a stick figure compared to a Leonardo da Vinci drawing or a photograph of a man. In programming a function allows you to not need to know the details of the function in order to be able to use it. Ex. sqrt()

Modular Programming Break a large problem into smaller pieces Smaller pieces sometimes called ‘subroutines’,‘procedures’ or functions Why? Helps manage complexity Smaller blocks of code Easier to read Encourages re-use of code Within a particular program or across different programs Allows independent development of code Provides a layer of ‘abstraction’ Hides the details of complex solutions Python has several built-in functions Imagine trying to develop a huge program like the Windows or Mac OS with hundreds of thousands (or millions of lines of code). No way to do it all in one big program file. Is the work of hundreds of computer programmers, each working on smaller pieces of the problem that are then brought together. By ‘abstraction’, I mean a way to simplify or separate the details of how a process works to an essential set of features that allow the process to be used. Example: a stick figure compared to a Leonardo da Vinci drawing or a photograph of a man. In programming a function allows you to not need to know the details of the function in order to be able to use it. Ex. sqrt() print(“Hello world”)

Variable storing a return value (b) Functions Functions are named sequences of statements that perform some action Functions accept parameters in a parameter list and may return values Functions which do not return a value are called void functions A function call is the activation of a function a = 55.66 b = int(a) Variable storing a return value (b) Parameter (a) Function call (int)

Using Modules Modules are collections of related functions stored in one file Module Objects are the namespaces under which functions stored in a module are accessed A module is imported with the import keyword Functions within a module are accessed using dot notation (ex. math.sin(value))

Using Modules Module Objects Modules are collections of related functions stored in one file Module Objects are the namespaces under which functions stored in a module are accessed A module is imported with the import keyword Functions within a module are accessed using dot notation (ex. math.sin(value))

Using Modules Side note: when is zero not zero? sine(π) = 0, right? Modules are collections of related functions stored in one file Module Objects are the namespaces under which functions stored in a module are accessed A module is imported with the import keyword Functions within a module are accessed using dot notation (ex. math.sin(value))

Aliases Shortened names to refer to module objects Declared using the as keyword

Composition Using an expression as part of a larger statement Anywhere a value can be used, a function which returns a value can be used in its place In this example, a call to math.tan is a parameter for a call to math.cos which in turn is a parameter to math.sin, which is in turn a parameter to print. This works because tan returns a float, which is a valid parameter value for cos, which in turn returns a float, which is a valid parameter value for sin, etc.

Defining Simple Functions A function is defined with the def keyword Functions have two parts, a header and a body The header gives the function a name and defines allowed parameters The header is the first line The body contains the statements which define the functions action Note how the body is indented Together, the header and body is the function definition

Variable Scope Variables which are declared within a function or are in the parameter list can only be used within that function These variables are said to be local to that function Variables declared in the main function cannot be used in a function Where a variable is valid, is the scope of the variable s is local to print_sum, it can be used here not here Note the error

Variable Scope Parameters are copied in order Variables which are declared within a function or are in the parameter list can only be used within that function These variables are said to be local to that function Variables declared in the main function cannot be used in a function Where a variable is valid, is the scope of the variable Parameters are copied in order Note the error

More Details print_hello definition Functions must be defined before they can be used Locally defined functions can be used within other locally defined functions The main function is the entry point to the program The main function has no header and is not indented The main function follows all function definitions print_hellohello definition main function definition

Stack Diagrams x -> 5 y -> 7 __main__ m -> 5 n -> 7 A diagram showing the functions of a program and the variables and values which exist within each function in the order they are called x -> 5 y -> 7 __main__ m -> 5 n -> 7 print_sum_twice a -> 5 b -> 7 s -> 12 print_sum

Stack Diagrams When there is a run-time error in Python, the interpreter will generate a Traceback This Traceback follows the same order as a stack diagram

Jupyter Markdown Markdown is a simple language for formatting text within a Jupyter notebook To use Markdown, the cell must be set to Markdown Various symbols change the way the text is displayed Symbol Function Location # Creates a headline At beginning of line ## Creates a subheadline At the beginning of line > Indents for attribution (blockquote) At the beginning of paragraph *** Creates a horizontal rule Alone on a line _ Light Emphasis Around a word _italics_ __ Strong Emphasis Around a word __bold__ https://sourceforge.net/p/jupiter/wiki/markdown_syntax/