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