1 Gentle Introduction to Programming Session 2: Functions.

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
Procedural programming in Java
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Introduction to Computing Science and Programming I
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
1 Gentle Introduction to Programming Session 4: Arrays, Sorting, Efficiency.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Program Design and Development
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
1 Gentle Introduction to Programming Tirgul 1: Shell and Scala “hands on” in the lab.
CS 201 Functions Debzani Deb.
1 Gentle Introduction to Programming Session 3: Higher Order Functions, Recursion.
1 Gentle Introduction to Programming Session 3: Recursion.
1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
1 Gentle Introduction to Programming Session 2: Functions.
The switch Statement, DecimalFormat, and Introduction to Looping
High-Level Programming Languages: C++
Introduction to Python
Introduction to Computational Linguistics Programming I.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
Programming for Engineers in Python Sawa 2015 Lecture 2: Lists and Loops 1.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
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.
1 Controlling Behavior Chap.5 Study Sections 5.1 – 5.3 The if and for Statements.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Controlling Program Flow with Decision Structures.
COMP Loop Statements Yi Hong May 21, 2015.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
1 Programming for Engineers in Python Autumn Lecture 8: Recursion.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
UCT Department of Computer Science Computer Science 1015F Iteration
CS314 – Section 5 Recitation 9
COMPUTATIONAL CONSTRUCTS
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
The switch Statement, and Introduction to Looping
Programming for Engineers in Python
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Lesson #6 Modular Programming and Functions.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Coding Concepts (Basics)
CISC124 Labs start this week in JEFF 155. Fall 2018
Lesson #6 Modular Programming and Functions.
The structure of programming
REPETITION Why Repetition?
Presentation transcript:

1 Gentle Introduction to Programming Session 2: Functions

2 Admin Upcoming Sunday: practical Schreiber Computer lab #4, 9-11, Scala / Eclipse installation status?

3 Review General terms in CS & programming Scala variables & types operators if / while Compiler / Interpreter Eclipse / Scala shell Ohad’s talk

4 Today More Scala: loops var / val Strings & API Defining variables (type inference) Variable scope Importance of style Guest lecture by Oded Magger (10:10) Home work review Functions Home work

5 Loops Used to repeat the same instructions until a stop criterion is met for, while loops: while ( ) for ( )

6 Example - while // factorial println( “ Enter a non-negative number ” ) var n : Int = Console.readInt() var fact : Int = 1 var i : Int = 1 while (i <= n) { fact = fact * i i = i + 1 } println(n + “ ! = “ + fact)

7 Example - for // factorial println( “ Enter a non-negative number ” ) var n : Int = Console.readInt() var fact : Int = 1 for (i <- 1 to n) { fact = fact * i } println( “ n! = “ + fact)

8 Examples at Factorial.scala

9 Example - Prime // Find whether a number is a prime println( “ Enter a non-negative number ” ) var n : Int = Console.readInt() var j : Int = 2 var stop : Boolean = false while ((j <= n/2) && !stop) { stop = (n%j == 0) j = j + 1 } if (!stop) { println(n) } Prime.scala

10 When to use for/while? Some applications are more natural to for, and others to while for is more suited when something is performed a predefined number of times, or when iterating over a list of objects while is more suited if the number of iterations is not known in advance (e.g., asking for legal input from a user)

11 Infinite Loops What are they? Beware of them

12 Scala’s Control Strctures All of Scala’s control structures result in some value: Example: println(if (price > 100) “expensive” else “cheap”)

13 Var versus Val val – “final” variable / imutable Example:

14 Strings val s : String = “ABC” A sequence of characters Have additional functionality (expressed by methods)

15 Application Programming Interface (API) How could I know that String has a method toLowerCase? How about checking the Scala API? Oops…

16 Java and Scala Scala is fully compatible with Java Actually, Scala is compiled into Java Bytecode And here is the String API (finally…)

17 Type Inference Scala’s ability to figure out types you leave off

18 Variable Scope output MultiTable.scala

19 Programming Style Comments: //, /* */ Indentation (cntl + i in eclipse) Meaningful variables names Why is it important?

20 Today More Scala: loops var / val Strings & API Defining variables (type inference) Variable scope Importance of style Guest lecture by Oded Magger (10:10) Home work review Functions Home work

21 Exercise 0 Write your first “Hello World!” program in Scala: Use the interpreter In Eclipse Make sure it compiles and executes properly Congratulations! Do you want us to do it together? Hello.scala

22 Fibonacci Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 Definition: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) en.wikipedia.org/wiki/Fibonacci_number

23 Exercise 1 Write a program that receives from the user an integer n > 0 and prints to the screen the n th Fibonacci number. a.Use a “ while ” loop b.Use a “ for ” loop

24 Solution ? Fibonacci.scala

25 Command Line Arguments In Eclipse

26 Command Line Arguments In Eclipse

27 Functions A group of declarations and statements that is assigned a name Effectively, a named statement block Usually has a value A sub-program When we write our program we always define a function named main Inside main we can call other functions Which can themselves use other functions, and so on…

28 Today More Scala: loops var / val Strings & API Defining variables (type inference) Variable scope Importance of style Guest lecture by Oded Magger (10:10) Home work review Functions Home work

29 What are They Good For? Generalize a repeated set of instructions We don’t have to keep writing the same thing over and over Solve bugs once… They can break your problem down into smaller sub-tasks Easier to solve complex problems They make a program much easier to read and maintain Abstraction – we don’t have to know how a function is implemented to use it

30 In Short Why do we need functions? Code reusability Modularity Abstraction

31 Function Definition in Scala

32 Example

33 Example

34 Function Output The last statement defines returned value to the calling function The type of the value returned must be the same as the return-type defined for the function (or a ‘lower’ type) If no value is to be returned, the return-type of the function should be set to ‘Unit’, and a unit value is returned

35 Other Types of Functions Method – member of some object (e.g., String – toLowerCase) Local functions: functions nested within functions Function literals (First-class functions), e.g: (x:Int) => x+1 Why do we need it anyway? Function values (e.g., val f = (x:Int => x+1))

36 Example: Nested Functions PrimesNested.Scala

37 There are many ways to define functions in Scala FuncDef.scala

38 Example ((x : Int) => x * 2)(a) ((x : Int) => x * 2)(5) ((x : Int) => x * 2) 55 10

39 Today More Scala: loops var / val Strings & API Defining variables (type inference) Variable scope Importance of style Guest lecture by Oded Magger (10:10) Home work review Functions Home work

40 Exercise – Integer Division Input: Two integers – A and B Output: How many times A contains B (it is the result of the integer division A/B) Do not use the operators ‘/’, ‘*’ Solution:

41 Exercise – Power of Two Input: integer A Output: is there an integer N such that A == 2^N? Solution:

42 Write a program that prints an upside-down half triangle of *. The height of the pyramid is the input. ***** *** ** **** * Exercise – Triangle Printout