130 likes | 307 Views
ITEC 320. Lecture 6 In-class coding Functions. Review. Types Homework Website is up Questions?. VIM. Dr. Okie has been providing VIM information Do you want a VI tutorial? What are the advantages of knowing a power editor?. Outline. Procedures Functions Problems. Functions.
E N D
ITEC 320 Lecture 6 In-class coding Functions
Review • Types • Homework • Website is up • Questions?
VIM • Dr. Okie has been providing VIM information • Do you want a VI tutorial? • What are the advantages of knowing a power editor?
Outline • Procedures • Functions • Problems
Functions • Similar to java methods with a non-void return type • Must have a return type • Must be defined before use • Cannot modify parameters
Syntax function sumArray(a: Int_Array) return integer is s: Integer := 0; -- holds the sum begin for i in a'range loop s := s + a(i); end loop; return s; end sumArray;
Procedures • No return type allowed • Parameters can be changed • In (default) • Out (allows passing back to the caller) • In Out (pass in and out) • What about the get procedure’s parameter? • Structure charts help
Syntax with ada.text_io; use ada.text_io; with ada.integer_text_io; use ada.integer_text_io; procedure inout is procedure vars(a: in Integer; b: in out Integer; c: out Integer) is begin b:= b+a; c:= b+3; end vars; x: Integer; y: Integer; z: Integer; begin x:=2; y:=3; vars(x,y,z); put(x'img & "|" & y'img & "|" & z'img); end inout;
Issues • Assigning to an in parameter • Using an out parameter’s values in the procedure (by default it can be anything) • Forgetting to assign a value to an out parameter • Can’t fool variables • I.e. passing an in mode variable to another procedure where it is an out mode parameter
Discussion • Web example
Problems • Write a function/procedure that returns whether or not a String is a palindrome
Problems • Given n of 1 or more, return the factorial of n, which is n * (n-1) * (n-2) ... 1. Compute the result recursively (without loops).
Summary • Functions • Procedures