Download presentation
Presentation is loading. Please wait.
1
Computer Science 2
2
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.
3
Today’s Overview Review of procedures Introduce Functions
Start to code
4
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.
5
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}
6
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.
7
Three parts of the function
The call statement The header The code
8
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?
9
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));
10
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,
11
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
12
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.
13
Review What is a function? When do you use a function?
Where do functions go in the code?
14
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.
15
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.