Computer Science 2 3-10-2014.

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.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
ARRAYS 1.Basic Ideas 2.The Array Type 3.Processing Arrays 4.Parallel Arrays 5.Two-dimensional Array 6.Arrays as Parameters.
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.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
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
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
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.
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:
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
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/…
Array : 1-dimension อนันต์ ผลเพิ่ม Anan Phonphoem
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
for Repetition Structures
Computing Fundamentals
Program Options: 10 Minutes online
CMP 131 Introduction to Computer Programming
Java Methods Making Subprograms.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
3-7 Sample Programs This section contains several programs that you should study for programming technique and style. Computer Science: A Structured.
Higher-Order Procedures
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.
kbkjlj/m/lkiubljj'pl;
Computer Science 1 Get out your notebook
Continue on the Array of Records 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.
Program Options: 10 Minutes online
Truth tables: Ways to organize results of Boolean expressions.
Java Methods Making Subprograms.
CS150 Introduction to Computer Science 1
How do you store a bunch of similar stuff?
Computer Science 1 Warm-up: True/False Dry Run
Computer Science 2 5/17/2016 Finish the Queue Program
What do you do when you don’t know how to get started.
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.
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
Life is Full of Alternatives
C++ Programming Basics
Computer Science
Warm-up: True/False Dry Run DIV / MOD 2nd If Then Program
How do you store a bunch of similar stuff?
Computer Science I: Get out your notes.
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.
Computer Science II First With files.
Presentation transcript:

Computer Science 2 3-10-2014

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

begin x:=10; y:=9; z:=8; w:=7; writeln(w:5, x:5, y:5, z:5); 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; 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 item. 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); What is different than the procedure’s call statement?

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.