Day 5
This is the day 5 page for computer programming course 101, an introduction to computer programming. Day 5 is the first of two parts on the topic of functions.Below are the topics and course materials we are learning today.
Procedures and Functions
Procedures and functions, referred to collectively as routines, are self-contained statement blocks which can be called from different locations in a program. A function is a routine that returns a value when it executes. A procedure is a routine that does not return a value.Function calls, because they return a value, can be used as expressions in assignment statements. For example:
S := UpperCase(BookTitle);Procedure calls and function calls be used as complete statements. For example:
ShowMessage(Error);Routine Arguments
Routines can have zero or more arguments. Arguments to a routine are specified by the caller through an opening parenthesis, a list of values seperated by zero or more commas, and a closing parenthesis. For example:
MoveTo(X, Y);Maxmimize();Maxmimize;Declaring Your Own Procedures and Functions
Often times when writing programs you'll encounter situations where to want to often a block of logic you've written. Procedures and functions serve this purpose, to provide a mechanism to isolate and reuse pieces of code you've create that are helpful in creating more complex programs. They also simply your work as a programmer by reducing the number of lines in your source code, as well as providing a means to store clean bug free logic.When you declare a procedure or function, you specify its name, the number and type of parameters it takes, and, in the case of a function, the type of its return value. Then you write a block of code that executes logic to match the name you've chosen for your the procedure or function.
A function is declared like so:
function FunctionName(ArgumentList): ReturnType;beginStatements;end;Here is an example of a function:
function IntegerToString(Value: Integer): string;varS: string;I: Integer;beginS := '';I := Abs(Value);repeatS := Chr(I mod 10 + Ord('0')) + S;I := I div 10;until I = 0;if Value < 0 thenS := '-' + S;Result := S;end;The statements inside functions and procedures can call other routines. In our implementation of IntegerToString we are calling the Abs, Chr, and Ord routines. We are also using assignments, looping statements, and conditional branching statements.
After we have defined our IntegerToString function, we can use it elsewhere in code like so:
ShowMessage('There are ' + IntegerToString(Minutes) + ' left until the server must reboot');Application: Spreadsheet
The instructor walks the student through the spreadsheet example application. We add password protection to the main form, preventing unauthorized viewing of our propriety company data.Built In Procedures and Functions
The Free Pascal language provides many built in procedures and functions. In this section we'll introduce a few of them. As a computer programmer you're going to have to memorize both the names and use of many routines in order to write anything useful. Try out each of these routines and commit them to your memory.Flow Control and Type Routines
The following routines can be used to either control the flow of you program or return information related to a type.Break Continue Exit Default SizeOf Low High
procedure Break;procedure Continue;procedure Exit; overload;procedure Exit(R: ResultType); overload;function Default(V: VariableType): VariableType; overload;function Default(VariableType): VariableType; overload;function SizeOf(T: TypeName): Integer; overload;function SizeOf(TypeName): Integer; overload;function Low(Ordinal: OrdinalType): OrdinalType; overload;function Low(OrdinalType): OrdinalType; overload;function High(O: OrdinalType): OrdinalType; overload;function High(OrdinalType): OrdinalType; overload;Command Line Routines
The following are routines useful when working with the terminal or a command line.ReadLn WriteLn ParamCount ParamStr
procedure ReadLn(var C: Char); overload;procedure ReadLn(var S: string); overload;procedure ReadLn(var I: Integer); overload;procedure ReadLn(var F: Float); overload;procedure WriteLn; overload;procedure WriteLn(T: InstrinsicType); overload;procedure WriteLn(T1: InstrinsicType; T2, T3, ...); overload;function ParamCount: Integer;function ParamStr(const S: string): string;String Related Routines
These routines are helpful when using strings.Chr Ord Length SetLength Space
function Chr(I: Integer): Char;function Ord(C: Char): Integer; overload;function Ord(O: OrdinalType); Integer; overload;function Length(const S: string): Integer;procedure SetLength(var S: string; L: Integer);function Space(N: Integer): string;Integer Related Routines
The following routines are useful when working with integer numbers.Abs Inc Dec Odd Round Trunc Random
function Abs(I: IntegerType): IntegerType; overload;function Abs(F: FloatingType): FloatingType; overload;function Inc(I: IntegerType): IntegerType;function Dec(I: IntegerType): IntegerType;function Odd(I: IntegerType): Boolean;function Round(F: FloatingType): Integer;function Trunc(F: FloatingType): Integer;function Random(Range: Integer): Integer; overload;function Random: Double; overload;Floating Point Related Routines
The following routines are useful when working with floating point numbers.Sqrt Sin Cos Tan Int Frac
function Sqrt(N: Double): Double;function Sin(N: Double): Double;function Cos(N: Double): Double;function Tan(N: Double): Double;function Int(N: Double): Double;function Frac(N: Double): Double;Homework
Scrabble is a popular word board game where players lay down wooden tiles to spell words taken from the English language. Given the following distribution of letter points, write a program to the total score of a letters in a word for the game of Scrabble.1: A, E, I, O, U, L, N, S, R, T 2: D, G 3: B, C, M, P 4: F, H, V, W, Y 5: K 8: J, X 10: Q, ZThe final program should be based on this project and must meet these requirements:
- You must write a routine to calculate the score of words when the score button is pressed
- Both the current score and the highest score must be displayed in the score label
- The project folder should be compressed with 7z and the name must be course101-day005.7z
- The compressed project file must be uploaded to your cloud file account
7za x filename.7zYou can create a 7z archive of a folder from the command line using the syntax:
7za a filename.7z foldername