Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.2160 ECE Application Programming

Similar presentations


Presentation on theme: "EECE.2160 ECE Application Programming"— Presentation transcript:

1 EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger & Dr. Lin Li Spring 2019 Lecture 18: Functions

2 ECE Application Programming: Lecture 18
Lecture outline Announcements/reminders Program 4 due 3/21 Exam 2: Monday, April 1 Today’s lecture Review: Functions Function examples 4/6/2019 ECE Application Programming: Lecture 18

3 ECE Application Programming: Lecture 18
Review: functions Used to break programs into smaller pieces Useful when code sequences repeated Functions have: An optional return value A name Optional arguments Must be prototyped or written completely prior to use Preferred method: prototypes in header file, function definitions in one source file, main function in separate source file We’ve discussed arguments passed by value: Copy of argument is sent to function Arguments cannot be modified outside function 4/6/2019 ECE Application Programming: Lecture 18

4 ECE Application Programming: Lecture 18
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; } 4/6/2019 ECE Application Programming: Lecture 18

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

6 Example: Writing functions
Write a function that: Takes an integer, length, as an argument and prints a series of “length” dashes on a single line Reads an integer value from the console input and returns 1 if the value is even, 0 if it’s odd Takes four double-precision numbers as arguments and returns their average 4/6/2019 ECE Application Programming: Lecture 18

7 ECE Application Programming: Lecture 18
Example solutions Write a function that: Takes an integer, length, as an argument and prints a series of “length” dashes on a single line void printLine(int length) { int i; for (i = 0; i < length; i++) printf(“-”); } 4/6/2019 ECE Application Programming: Lecture 18

8 Example solutions (cont.)
Write a function that: reads an integer value from the console input and returns 1 if the value is even, 0 if it’s odd int checkEvenOdd() { int value; scanf(“%d”, &value); if ((value % 2) == 0) return 1; else return 0; } 4/6/2019 ECE Application Programming: Lecture 18

9 Example solutions (cont)
Write a function that: takes four double-precision numbers as arguments and returns their average double avgFour(double a, double b, double c, double d) { return (a + b + c + d) / 4.0; } 4/6/2019 ECE Application Programming: Lecture 18

10 ECE Application Programming: Lecture 18
Final notes Next time More function examples (Monday, 3/18) Enjoy your Spring Break! Reminders: Program 4 due 3/21 Exam 2: Monday, April 1 4/6/2019 ECE Application Programming: Lecture 18


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google