Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.

Slides:



Advertisements
Similar presentations
Python Basics: Statements Expressions Loops Strings Functions.
Advertisements

CSE 251 Dr. Charles B. Owen Programming in C1 Functions.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Modularization
Introduction to C Programming
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS 201 Functions Debzani Deb.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Introduction to C Programming
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Introduction to Python
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 -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
CSE 131 Computer Science 1 Module 1: (basics of Java)
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Functions Chapter 4 Python for Informatics: Exploring Information
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
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.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Python Programming Module 3 Functions Python Programming, 2/e1.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Lesson 06: Functions Class Participation: Class Chat:
Exam #1 You will have exactly 30 Mins to complete the exam.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Functions.
CSCI 161: Introduction to Programming Function
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Functions Declarations CSCI 230
Functions, Procedures, and Abstraction
Lesson 06: Functions Class Chat: Attendance: Participation
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
G. Pullaiah College of Engineering and Technology
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.
Presentation transcript:

Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo

2 Outline Functions Built-in functions User defined functions Abstraction Reusability Parameters and arguments Returning values Variables Scope

3 Functions From mathematics, a functions perform some operation and returns a result. Functions hide the details of an operation, to make it easy to use by others –e.g. sqrt() for computing the square root.

4 Function In the context of programming, a function is a named sequence of statements that performs a computation. When you define a function you specify the name and the sequence of statements. Functions are invoked by their name.

5 Python provides built-in functions Type conversion functions

6 A big list of built-in functions

7 Remember the Math module? Math functions

8 Lots of Python modules available Built-in modules –The Python Standard Library: Other modules –PyPI - the Python Package Index –The Python Package Index is a repository of software for the Python programming language. –There are thousands of packages there.

User defined functions

10 Mathematical notation Consider a function that converts temperatures from Celsius to temperatures in Fahrenheit: –Formula: F = 1.8*C –Functional notation: F = celsisus2Fahrenheit(C) where celsius2Fahrenheit(C) will compute 1.8*C and return the result.

11 Function definition Math: – f(C) = 1.8*C Python: Terminology: parameter “C”

12 © 2011 Pearson Addison-Wesley. All rights reserved.

13 Definition and calling

14 Triple quoted string in function A triple quoted string just after the definition is called a docstring docstring is used for documentation of the function’s purpose. It is used to indicate to the user what the function does.

Abstraction

16 Abstraction

17 Abstraction 1.The ability of dealing with ideas rather than events. 2.Something that exists only as an idea.

18 Abstraction in computing A programmer would use abstraction to define general purpose functions. Abstraction is one of the most important techniques in software engineering as it is used to reduce complexity.

Reusability

20 Why have functions? Support divide-and-conquer strategy Abstraction of operations Reuse: once written, use again Sharing: others can use it Security: if well tested, more secure for reuse Simplify code: more readable

21 How to write a function Does one thing. If it does too many things, it should be refactored into multiple functions. Readable. You should be able to read it as well as others. Reusable. If it performs its task well, you can reuse. Complete. A function should check for all the cases where it might be invoked. Check for potential errors. Not too long. As it does one thing, code is usually succinct.

22 Parameters and argument A parameter is the variable which is part of the function's definition. An argument is an expression, value, or literal used when calling the method. When calling a function, the arguments must match the parameters

23 © 2011 Pearson Addison-Wesley. All rights reserved. val=25* = 77.0 celsius=25

24 Return Statement The return statement indicates the value that is returned by the function. The return statement is optional. If there is no return statement, the function is often called a procedure. Procedures are often used to perform duties such as printing output or storing a file

25 Multiple returns in a function A function can have multiple return statements, but only one will execute. –The first return statement found executes and ends the function. Multiple return statements can make your code confusing, therefore should be used carefully.

26 Multiple return statements

27 Local variables When you create a variable inside a function, it is local (i.e. it only exists inside the function). For example: When cat_twice terminates, the variables cat, part1, and part2 are destroyed. These cannot be used outside of the function.

28 OpenLab and Blackboard Check OpenLab for any new lab. Check Blackboard for any new quiz.