EECE.2160 ECE Application Programming

Slides:



Advertisements
Similar presentations
1 Functions Functions used to break problem down into small, "bite- sized" pieces. Functions have an optional type of return value, a name, and optional.
Advertisements

ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3170 Microprocessor Systems Design I
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
ECE Introduction to Computer Engineering I
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3170 Microprocessor Systems Design I
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

EECE.2160 ECE Application Programming Instructor: Dr. Michael Geiger Fall 2018 Lecture 15: Functions

ECE Application Programming: Lecture 15 Lecture outline Announcements/reminders Program 4 due 10/22 Today’s lecture Review: for loops Functions 7/6/2019 ECE Application Programming: Lecture 15

ECE Application Programming: Lecture 15 Review: for loops for (<init var>; <test>; <change var>) <statements> Operators to directly change variable x++, x--  post-increment/decrement ++x, --x  pre-increment/decrement +=, -=, *=, /=  augmented assignment 7/6/2019 ECE Application Programming: Lecture 15

ECE 160 - Introduction to Computer Engineering I 02/09/2005 Functions Functions used to break problem down into small, "bite-sized" pieces. Make code more manageable and readable Identify reusable pieces Functions have an optional type of return value, a name, and optional arguments Functions return at most, ONE value Functions must be either "prototyped" or declared prior to use. Good programming practices requires all functions to be prototyped. 7/6/2019 ECE Application Programming: Lecture 15 (c) 2005, P. H. Viall

ECE Application Programming: Lecture 15 7/6/2019 ECE Application Programming: Lecture 15

ECE Application Programming: Lecture 15 Functions name of function type of value returned parameters of function (variables in) double hyp(double a, double b) { double sum, result; sum = a*a + b*b; result = sqrt(sum); return result; } Alternate way of writing above function double hyp(double a, double b) { return sqrt(a*a + b*b); } Single value returned by function 7/6/2019 ECE Application Programming: Lecture 15

Functions - complete program #include <stdio.h> #include <math.h> double hyp(double a, double b); void main() { double x,y,h; printf("Enter two legs of triangle: "); scanf("%lf %lf",&x,&y); h=hyp(x,y); printf("Trgle w legs %lf and %lf has hyp of %lf\n",x,y,h); } double hyp(double a, double b) { double sum, result; sum = a*a + b*b; result = sqrt(sum); return result; } prototype (note semi-colon ) actual function definition (NO semi-colon ) 7/6/2019 ECE Application Programming: Lecture 15

ECE Application Programming: Lecture 15 Functions - scope #include <stdio.h> #include <math.h> double hyp(double a, double b); void main() { double x,y,h; printf("Enter two legs of triangle: "); scanf("%lf %lf",&x,&y); h=hyp(x,y); printf("Trgle w legs %lf and %lf has hyp of %lf\n",x,y,h); } double hyp(double a, double b) { double sum, result; sum = a*a + b*b; result = sqrt(sum); return result; } x ? y ? h ? a ? b ? sum ? result ? 7/6/2019 ECE Application Programming: Lecture 15

ECE Application Programming: Lecture 15 Functions - scope #include <stdio.h> #include <math.h> double hyp(double a, double b); void main() { double x,y,h; printf("Enter two legs of triangle: "); scanf("%lf %lf",&x,&y); h=hyp(x,y); printf("Trgle w legs %lf and %lf has hyp of %lf\n",x,y,h); } double hyp(double a, double b) { double sum, result; sum = a*a + b*b; result = sqrt(sum); return result; } x 3.0 y 4.0 h ? a ? b ? sum ? result ? 7/6/2019 ECE Application Programming: Lecture 15

ECE Application Programming: Lecture 15 Functions - scope #include <stdio.h> #include <math.h> double hyp(double a, double b); void main() { double x,y,h; printf("Enter two legs of triangle: "); scanf("%lf %lf",&x,&y); h=hyp(x,y); printf("Trgle w legs %lf and %lf has hyp of %lf\n",x,y,h); } double hyp(double a, double b) { double sum, result; sum = a*a + b*b; result = sqrt(sum); return result; } x 3.0 y 4.0 h ? a 3.0 b 4.0 sum ? result ? 7/6/2019 ECE Application Programming: Lecture 15

ECE Application Programming: Lecture 15 Functions - scope #include <stdio.h> #include <math.h> double hyp(double a, double b); void main() { double x,y,h; printf("Enter two legs of triangle: "); scanf("%lf %lf",&x,&y); h=hyp(x,y); printf("Trgle w legs %lf and %lf has hyp of %lf\n",x,y,h); } double hyp(double a, double b) { double sum, result; sum = a*a + b*b; result = sqrt(sum); return result; } x 3.0 y 4.0 h ? a 3.0 b 4.0 sum 25.0 result ? 7/6/2019 ECE Application Programming: Lecture 15

ECE Application Programming: Lecture 15 Functions - scope #include <stdio.h> #include <math.h> double hyp(double a, double b); void main() { double x,y,h; printf("Enter two legs of triangle: "); scanf("%lf %lf",&x,&y); h=hyp(x,y); printf("Trgle w legs %lf and %lf has hyp of %lf\n",x,y,h); } double hyp(double a, double b) { double sum, result; sum = a*a + b*b; result = sqrt(sum); return result; } x 3.0 y 4.0 h ? a 3.0 b 4.0 sum 25.0 result 5.0 7/6/2019 ECE Application Programming: Lecture 15

ECE Application Programming: Lecture 15 Functions - scope #include <stdio.h> #include <math.h> double hyp(double a, double b); void main() { double x,y,h; printf("Enter two legs of triangle: "); scanf("%lf %lf",&x,&y); h=hyp(x,y); printf("Trgle w legs %lf and %lf has hyp of %lf\n",x,y,h); } double hyp(double a, double b) { double sum, result; sum = a*a + b*b; result = sqrt(sum); return result; } x 3.0 y 4.0 h ? a 3.0 b 4.0 sum 25.0 result 5.0 7/6/2019 ECE Application Programming: Lecture 15

ECE Application Programming: Lecture 15 Functions - scope #include <stdio.h> #include <math.h> double hyp(double a, double b); void main() { double x,y,h; printf("Enter two legs of triangle: "); scanf("%lf %lf",&x,&y); h=hyp(x,y); printf("Trgle w legs %lf and %lf has hyp of %lf\n",x,y,h); } double hyp(double a, double b) { double sum, result; sum = a*a + b*b; result = sqrt(sum); return result; } x 3.0 y 4.0 h 5.0 7/6/2019 NOTE - a and b are NOT copied back to x and y ECE Application Programming: Lecture 15

Exercise - What prints (if 5, 12 entered) #include <stdio.h> #include <math.h> double hyp(double a, double b); void main() { double x,y,h; printf("Enter two legs of triangle: "); scanf("%lf %lf",&x,&y); h=hyp(x,y); printf("Trgle w legs %lf and %lf has hyp of %lf\n",x,y,h); } double hyp(double a, double b) { double sum, result; a = 3; b = 4; sum = a*a + b*b; result = sqrt(sum); return result; } x y h a b sum result 7/6/2019 ECE Application Programming: Lecture 15

ECE Application Programming: Lecture 15 Answer Trgle w legs 5.000000 and 12.000000 has hyp of 5.00000 7/6/2019 ECE Application Programming: Lecture 15

ECE Application Programming: Lecture 15 Example What does the following print? int f(int a, int b); int main() { int x = 1; int y = 2; int result1, result2, result3; result1 = f(x, y); result2 = f(y, result1); result3 = f(result1, result2); printf("x = %d, y = %d\n", x, y); printf("Result 1: %d\n", result1); printf("Result 2: %d\n", result2); printf("Result 3: %d\n", result3); return 0; } int f(int a, int b) { int i; // Loop index int r = 0; // Result for (i = 0; i < a; i++) r += b; return r; } 7/6/2019 ECE Application Programming: Lecture 15

ECE Application Programming: Lecture 15 Example solution x = 1, y = 2 Result 1: 2 Result 2: 4 Result 3: 8 7/6/2019 ECE Application Programming: Lecture 15

ECE Application Programming: Lecture 15 Final notes Next time Continue with functions Exams to be returned next week Reminders: Program 3 graded; regrades due 3/7 Program 4 due 3/2 No Thursday office hours this week If normal office hours don’t work, please make an appointment for another day 7/6/2019 ECE Application Programming: Lecture 15