Functions Art &Technology, 3rd Semester Aalborg University Programming https://art.moodle.aau.dk/course/view.php?id=33 David Meredith

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
Conditionals Art &Technology, 3rd Semester Aalborg University Programming David Meredith
CS001 Introduction to Programming Day 3 Sujana Jyothi
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle.
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Lesson Three: Organization
A tour around Java General introduction to aspects of the language (these will be covered in more detail later) After this tour you should have a general.
Video in Processing David Meredith Aalborg University.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Python quick start guide
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Review of ArT3 Programming Course David Meredith Aalborg University
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
CPS120: Introduction to Computer Science Decision Making in Programs.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
CPS120: Introduction to Computer Science Lecture 14 Functions.
Arrays Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Algorithms Writing instructions in the order they should execute.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
© 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 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
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.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Department of Computer Engineering Methods Computer Programming for International Engineers.
Expressions Methods if else Statements Loops Potpourri.
Review Expressions and operators Iteration – while-loop – for-loop.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
Functions. 2 Modularity What is a function? A named block of code Sometimes called a ‘module’, ‘method’ or a ‘procedure’ Some examples that you know are:
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
Objects Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Chapter 6: Loops.
Exam 2 Review.
Advanced Object-Oriented Programming
Loops in Java.
Functions.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Coding Concepts (Sub- Programs)
Repetition Control Structure
CS139 October 11, 2004.
CS149D Elements of Computer Science
Flow of Control.
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Introduction to the Lab
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
ITM 352 Functions.
Corresponds with Chapter 5
Presentation transcript:

Functions Art &Technology, 3rd Semester Aalborg University Programming David Meredith

Reading Chapter 7 of Shiffman, Learning Processing

Functions If you find yourself writing the same sequence of lines in various places in your program, then maybe those lines should be stuck together in a block called a function In the sketch on the right, circle and disc are user-defined functions Using functions increases modularity – a module is a logical structural unit of a program that is as independent as possible from the rest of the program Using functions increases reusability – a function can be used many times in different situations to carry out a useful job without having to repeat all the code inside it every time you want the job done Here, instead of having to define noFill and remember to use the diameter twice in ellipse, we simply call the circle function

Defining a function To define a function, you need to specify – the return type – the function name – the arguments of the function and their types – the body of the function which contains all the statements that have to be executed when the function is called Standard, system-defined functions follow the same pattern If a function returns anything other than void, then there must be a line in the function of the form return x; where the type of x is the same as that returned by the function ReturnType functionName(ArgType1 arg1, ArgType2 arg2) { ReturnType r = something; doSomethingWithR(); return r; }

Bouncing Ball using functions

Functions with arguments

Things to remember about parameters Pass the same number of parameters as defined in the function Each parameter passed must be the same type as defined in the function You can pass – a literal value to a function as a parameter e.g., 20, 5, 4.3 – an expression that evaluates to a value of the right type e.g., 8+3, 4 * x/2, random(1,10), etc. The arguments of a function act as local variables and are not accessible outside of the function

A function that returns a value The function squareContainsMouse returns the boolean value true if a square contains the mouse and colours this square red. Otherwise, the function returns false and the square is coloured black. On every frame, 40 squares are drawn of random size in random positions.