Week 7.docx

11 Pages
Unlock Document

University of California - San Diego
Electrical & Computer Engineer
ECE 15
Alon Orlitsky

1. Applications of 2 and 3 DimensionalArrays a. Matrix transposition: i. Matrix – two dimensional array of numbers ii. Atransposition is a flip among the diagonal. b. Swapping Two Numbers: i. Two variables: x and y, and we want to exchange their values. Ex: x = 1, y = 0. 1. we need to store the original value in a temporary variable, so we can assign this “temp” variable into the other variable without losing it during initialization. ii. transpose1.c (possible homework solution) 2. Functions a. Modular Design: 1. Breaking overall task in to smaller modules, each constructed from even smaller ones. 2. Construct each part individually. 3. Easier to debug, and gain access to smaller parts of code easily. ii. Implemented using functions. It is a convenient way to encapsulate long computations. 1. They accept parameters and return a value. b. Functions: 1. Consists of 3 parts: ii. Declaration 1. Tells compiler what to expect. Consists of name, parameters, and type of value returned. 2. return-type function-name(parameter-list); iii. Definition 1. Complete description of what function does. Consists of: a. Declaration b. Body iv. Call 1. Computes the function a. int square(int x); i. Declaration b. int square(int x) { return x*x; } i. Body 1. Functions terminate upon encountering any “return” value, or bracket closing the function. c. square(5); i. Call a. examplecode v. Function Parameters 1. Functions can have multiple parameters a. power.c 2. Afunction can also have no parameters, as declared by void. vi. Multiple Return Statements 1. Functions can have multiple possible return statements, such as in an if-else statement, that gives the option of multiple return statements, although only one is actually going to be returned. 2. abs_value.c vii. Void Functions 1. Some functions perform a task and return no value. These functions are labeled with the term “void” upon declaration. It does not return a value in a sense that beforehand, it returns something that can be assigned to a variable.A“printf” statement, for example, is not a value that can be returned. a. void messages(int i) b. messages.c viii. Order of Functions 1. Functions appear sequentially, not inside other functions. 2. The order in which functions appear when writing does not matter, as long as they are called before the function itself appears. 3. If definition appears before call, a separate declaration is not needed. 4. With small programs, you would want to define the functions first, before the call, so you do not need declarations again. 5. With large programs, you want to declare functions first, call, then define the function. ix. Parameters are declared according to the location in the list, not by name. 1. Variables inside and outside function bear no relation to each other, thus, there can be two variables with the same name 2. param.c c. Revisiting: With Functions i. gcd3.c 1. Without functions, if you would want to take the Greatest Common Denominator (gcd) of more than two numbers, you would have to take the gcd twice. With functions, you would only need to declare the function taking the gcd of two numbers once. You can take the gcd of the first two numbers, and then take the gcd of that gcd of the first tow numbers and the third number. ii. nr_fun.c iii. pi_fun.c 3. Mechanics a. What happens at Function Call? i. double myfunction(double x, double y); 1. declaration ii. z = myfunction(9.9*a, 2/lambda); 1. calling iii. Memory is allocated to the function parameters double x and double y. iv. Expressions 9.9*a and 2/lambda are evaluated, order unspecified. 1. These resulting values are assigned into variables x and y. Type conversions performed as needed. This is known as “call by value”. b. What Happens at Function Return? i. if the function contains the statement “return ‘expression’”, then the function execution stops as soon as the first instance of such statement is reached. 1. the expression is evaluated, type converted if needed, and the value is assigned to the variable z. c. You Cannot Change the Calling Parameters i. When f(x) calls a function f(int y), the value of x is copied into y. 1. f has no access to x, and f cannot change x. It can only change y. ii. The function cannot change the values of the function that called it. 1. Look at the two quizzes in the lecture. 4. Array Parameters a. Array Elements and Parameters i. Both array elements and whole arrays can be parameters. ii. Ex: Replacing all array elements by their absolute value, then printing all elements. 1. array1.c a. The function takes an integer and calls it with an element of the array, abs_val(myArray[i]), with the function returning an absolute value. iii. With passing whole arrays as parameters, we want to pass the array name and the length separately. 1. array2.c a. because functions do not know the length of the array, the array name and the array length must be two separate parameters. Hence, in “int main()” it calls using myArray, and 5, the length of myArray. b. In the absAll function, it uses “int length” as a bound for “i”. iv. array3.c 1. Element parameters in absAll to abs Val are declared as int, and passed as array[i]. 2. Array parameters (from main to absAll, and printAll) are declared through int ar[], and passed into myArray. v. Unlike regular variables (int, etc.), array parameters can be changed by called functions. b. When only declaring a function, the declaration does not require parameter names. However, in order to show that a parameter is an array ty
More Less

Related notes for ECE 15

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.