Computer Science 2 4-3-2018.

Slides:



Advertisements
Similar presentations
Program CheckPass; var TestScore, ExamScore : integer; FinalScore : real; Status : string; begin write(‘Enter the Test score:’); readln(Testscore); write(‘Enter.
Advertisements

1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
INFORMATION TECHNOLOGY CSEC CXC 10/25/ PASCAL is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides.
Introduction to Pascal The Basics of Program writing.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Programming, an introduction to Pascal
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
Computer Science 1 How do you store a bunch of similar stuff?
Computer Science I: Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
Computer Science 1 9/10/2014 Review Dry Run Math in Pascal How do you start, when you don’t know where to start?
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Computing Fundamentals
CS 108 Computing Fundamentals Notes for Thursday, September 14, 2017
Program Options: 10 Minutes online
CMP 131 Introduction to Computer Programming
Java Methods Making Subprograms.
3-7 Sample Programs This section contains several programs that you should study for programming technique and style. Computer Science: A Structured.
Chapter 4 - Visual Basic Schneider
Program options Write a program for one of the following
Computer Science Procedures
How do you store a bunch of similar stuff?
Computer Science II First With files.
Computer Science II First With files.
Java Methods Making Subprograms.
Truth tables: Ways to organize results of Boolean expressions.
How can you model playing rock-paper-scissors?
Review Dry Run Taking Names Online time Math is Good
kbkjlj/m/lkiubljj'pl;
Computer Science 1 Get out your notebook
Computer Science And he made a molten sea, ten cubits from the one brim to the other: it was round all about, and his height was five cubits: and.
Program Options: 10 Minutes online
Truth tables: Ways to organize results of Boolean expressions.
Computer Science
Java Methods Making Subprograms.
How do you store a bunch of similar stuff?
Computer Science 1 Warm-up: True/False Dry Run
Computer Science
Computer Science 2 5/17/2016 Finish the Queue Program
Computer Science 2 Tally Arrays 2/4/2016.
What do you do when you don’t know how to get started.
Functions Divide and Conquer
Computer Science II Second With files.
Computer Science 2 Take out a piece of paper for the following dry runs. Label It Recursive Dry Runs 4/12/2018. It will be turned in when completed
Computer Science Procedures Day 2.
Computer Science 2 More Trees.
Life is Full of Alternatives
Computer Science Procedures Day 2.
Computer Science 1 while
Computer Science 1 For..do loop Dry Run Take notes on the For loop
Warm-up: Dry Run DIV / MOD 2nd If Then Program
Warm-up: True/False Dry Run DIV / MOD 2nd If Then Program
How do you store a bunch of similar stuff?
Computer Science
Computer Science I: Get out your notes.
How can you model playing rock-paper-scissors?
Program options Write a program for one of the following
Computer Science 1 while
Computer Science 1 Get out your notebook
Computer Science 1 Get out your notebook
Dry Run Fix it Write a program
Computer Science And he made a molten sea, ten cubits from the one brim to the other: it was round all about, and his height was five cubits: and.
COMPUTING.
Computer Science II First With files.
Presentation transcript:

Computer Science 2 4-3-2018

Learning Objectives Review how to dry run a program that uses procedures Understand how read a program that uses functions. Understand the three parts of a function. Be able to write a program that uses functions.

Today’s Overview Review of procedures Introduce Functions Start to code

Remember boxes and arrows. program confusion7; var w,x,y,z:integer; procedure see1(var n, z:integer; w:integer); y:integer; begin writeln(w:5, n:5, y:5, z:5); y:=4; n:= w mod x; z:= w div y; w:= 3 * y; end; procedure see2( n, z:integer; var w:integer); y:= 5; Remember boxes and arrows. begin x:=10; y:=9; z:=8; w:=7; writeln(w:5, x:5, y:5, z:5); see1(x, w, y); see2(y, w, x); end.

What do you think this program does? Program ProcedureFunctionExample; {************************************} procedure getTheNumbers(var first, second:integer); begin writeln(‘Please enter a number’); readln(first); writeln(‘Please enter another number’); readln(second); end; {***************************************} function findTheTotal(beginning, ending : integer):integer; writeln(‘Now in the FindTheTotal function’); findTheTotal:=beginning + ending; {*****************************************} var num1,num2,tot:integer; begin {Of the main body} getTheNumbers(num1, num2); tot:=findTheTotal(num1, num2); writeln(‘The total of ‘,num1,’ + ‘, num2, ‘ = ‘, tot); readln; end. {Of the main body}

Functions Like a procedure, but the call statement represents a value. When do you use one? When you want to return one value. Calculate the average of a bunch of numbers Fancy Boolean expression.

Three parts of the function The call statement The header The code

What is different than the procedure’s call statement? The call statement tot:=findTheTotal(num1, num2); In General answer:=functionname(Arguments); If there are no arguments answer:= functionname; What is different than the procedure’s call statement?

Getting Fancy with Calls Statements Use order of operations Calling a function in a writeln Writeln(findTheTotal(num1, num2), ‘ is the answer’); Calling a function in a condition If (findTheTotal(num1,num2) < 20) then Calling a function in a function call Total:= findTheTotal(num1, findTheTotal(num3, num4));

The Header ReturnType can be any simple data type: function FindTheTotal(beginning, ending : integer):integer; In General function Functionname(Parameters):ReturnType; ReturnType can be any simple data type: Integer, real, string, boolean, char,

The Code {***************************************} Can have local: const, type and var {***************************************} function FindTheTotal(beginning, ending : integer):integer; begin writeln(‘Now in the FindTheTotal function’); FindTheTotal:=beginning + ending; end; {*****************************************} This line of code gives the value to the function

Reading a program. begin a:= 6; b:= 12; writeln(a:4,b:4); program funInFunction; var a,b:integer; function one(a,b:integer):integer; c:integer; begin c:= a+2*b; one:= c DIV 2; end; function two:integer; first, second:integer; first:= 15; second:= 10; two:= first - second; Reading a program. begin a:= 6; b:= 12; writeln(a:4,b:4); a:= one(b,a); writeln(two:4); b:= one(a,b) + two - 6; writeln(a:4,b:4 ); end.

Review What is a function? When do you use a function? Where do functions go in the code?

Functions Program #1 Write a function to convert from Radians to degrees Convert from Fahrenheit to Celsius Sent the radius and height and calculate the volume of a cylinder.

Function #2 Program Options Sent nothing, but inside the functions gets 10 scores and calculates and returns the average Factorial Function Input: Any positive integer; Output: The factorial of the number Example: Input: 5 Output: 5! = 120 Create the header and call statement for one.