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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 3: Modularization
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 6: User-Defined Functions I
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
Software Engineering Principles and C++ Classes
Main task -write me a program
Introduction to Methods
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
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.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Python Functions.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
VHDL Discussion Subprograms IAY 0600 Digital Systems Design Alexander Sudnitson Tallinn University of Technology 1.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Programming Fundamentals Enumerations and Functions.
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 6: User-Defined Functions I
Lesson #6 Modular Programming and Functions.
Introduction To Repetition The for loop
Lesson #6 Modular Programming and Functions.
UNIT - V STORED PROCEDURE.
Topic: Functions – Part 2
Functions CIS 40 – Introduction to Programming in Python
User-Defined Functions
Lesson #6 Modular Programming and Functions.
Call Stacks, Arguments and Returns
MSIS 655 Advanced Business Applications Programming
Topics Introduction to Functions Defining and Calling a Void Function
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Lesson #6 Modular Programming and Functions.
Topics Introduction to Functions Defining and Calling a Function
Top-Down Design with Functions
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CPS125.
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
Top-Down Design with Functions
Introduction to Methods and Interfaces
Presentation transcript:

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 keyword” It has a name It should have a specific task to do (functional cohesion) It has a parameter list (data that it needs to work on for the task) It is also called a subroutine, subprogram, procedure or module in different languages

Why write code in functions? You can write the code once, use it many times It’s easier to divide work among the members of a team – each one is responsible for a certain function You can put off the details when you are writing the top-level code of the program “abstraction or generalization” and then later, focus on just one function at a time (top down design) You can write the program incrementally, one function at a time (bottom up design) Testing is easier (unit testing)

Why write code in functions? You can think of a function as a ‘black box’ – you only know what goes into it and what should come out of it, not what is going on inside This organization allows for easier changes of a function’s implementation – the function code is all that has to change Software companies often sell libraries of functions – they sell the executable code and documentation of the headers of the functions, that is, the parameter lists and the value that is returned by the function – you don’t get to see the implementation, just the interface. Later the company may implement the function library in a more efficient way – they can send you the update without your having to change your code at all!

A Black Box You can think of a function as a black box. Put certain things IN and certain things (or actions) come OUT. The details of what’s in the box can be postponed for a while. Black Box InputOutput

Syntax of functions – two parts A function has to have a definition And a function needs to be called (otherwise it is not executed)

Syntax of a definition of a function Starts with def identifier (parameters): at left edge of screen Statements follow the def line, indented one tab stop (the “body”) When the lines stopped being indented, the function definition is finished It may or may not end with a return statement Parameter list can be empty or as many identifiers as desired with commas between, parameters are not expressions or constants Where the function definition is placed in the source file is up to you – the main rule is that it must be defined before the first call to the function appears in the file Functions can be defined inside other function definitions. This is generally not necessary – please do not do this in this class

Syntax of function call (invocation) There are two kinds of calls to functions If the function returns a value, the function call must be part of a statement – it cannot stand alone Example: y = sqrt(x) Example: print(sqrt(x)) NOT correct: sqrt(x) # Python allows this to be written but it does # nothing with the result of the function, so it’s a waste of time If the function does not return a value, the function call is a stand-alone statement Example: win.close() Example: circ1.setFill(“red”) # changes the object, does not return a value Not correct: y = win.close() # y will get the value None