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
Instructors: Dr. Michael Geiger Fall 2018 Lecture 18: More function examples

2 ECE Application Programming: Lecture 18
Lecture outline Announcements/reminders Program 3 regrades due F 10/19 Program 4 due M 10/22 This week: no Th office hours Exam 1, Q3b: if you answered (ii), (iii), and (iv), please see me to get partial credit Today’s lecture More function examples Pointer arguments 7/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 7/6/2019 ECE Application Programming: Lecture 18

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

5 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(“-”); } 7/6/2019 ECE Application Programming: Lecture 18

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

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

8 Justifying pass by address
May want the ability to “return” multiple values from function Functions can only return at most one value Functions can take multiple arguments ... ... but, as we’ve discussed so far, passing by value just copies arguments No way to change arguments and have change reflected outside of function Solution uses pointers 7/6/2019 ECE Application Programming: Lecture 18

9 ECE Application Programming: Lecture 18
Pointers Pointer: variable that holds address of another variable Can get address of existing variable using & Can get value of existing pointer using * Pointer declaration: <base type>* <pointer name> Base type determines how reference is interpreted Be careful when declaring multiple pointers Be sure to initialize pointer before use 7/6/2019 ECE Application Programming: Lecture 18

10 ECE Application Programming: Lecture 18
Pointer arguments Passing pointer gives ability to modify data at that address In prototype/definition—argument has pointer type For example: int f(int *addr_y); When calling function, can pass explicit pointer or use address operator (&<var>) Examples: int y = 2; int result1; result1 = f(&y); 7/6/2019 ECE Application Programming: Lecture 18

11 Functions - pass by address
#include <stdio.h> #include <math.h> void get_r_theta(double a, double b, double *adr_r, double *adr_th); void main() { double x,y,r,th; printf("Enter x, y components of vector: "); scanf("%lf %lf",&x,&y); get_r_theta(x,y,&r,&th); printf("Vector with x=%lf and y=%lf has r=%lf, theta=%lf\n",x,y,r,th); } void get_r_theta(double a, double b, double *adr_r, double *adr_th) { double sum; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(b,a); } x 4600 y 4608 r 4610 th 4618 7/6/2019 ECE Application Programming: Lecture 18

12 Functions - pass by address
#include <stdio.h> #include <math.h> void get_r_theta(double a, double b, double *adr_r, double *adr_th); void main() { double x,y,r,th; printf("Enter x, y components of vector: "); scanf("%lf %lf",&x,&y); // user enters 3,4 get_r_theta(x,y,&r,&th); printf("Vector with x=%lf and y=%lf has r=%lf, theta=%lf\n",x,y,r,th); } void get_r_theta(double a, double b, double *adr_r, double *adr_th) { double sum; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(b,a); } x 3.0 4600 y 4.0 4608 r ? 4610 th ? 4618 7/6/2019 ECE Application Programming: Lecture 18

13 Functions - pass by address
#include <stdio.h> #include <math.h> void get_r_theta(double a, double b, double *adr_r, double *adr_th); void main() { double x,y,r,th; printf("Enter x, y components of vector: "); scanf("%lf %lf",&x,&y); // user enters 3,4 get_r_theta(x,y,&r,&th); printf("Vector with x=%lf and y=%lf has r=%lf, theta=%lf\n",x,y,r,th); } void get_r_theta(double a, double b, double *adr_r, double *adr_th) { double sum; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(b,a); } x 3.0 4600 y 4.0 4608 r 4610 th 4618 a 3.0 7380 b 4.0 7385 adr_r 4610 7388 adr_th 4618 738c sum 7380 7/6/2019 ECE Application Programming: Lecture 18

14 Functions - pass by address
#include <stdio.h> #include <math.h> void get_r_theta(double a, double b, double *adr_r, double *adr_th); void main() { double x,y,r,th; printf("Enter x, y components of vector: "); scanf("%lf %lf",&x,&y); // user enters 3,4 get_r_theta(x,y,&r,&th); printf("Vector with x=%lf and y=%lf has r=%lf, theta=%lf\n",x,y,r,th); } void get_r_theta(double a, double b, double *adr_r, double *adr_th) { double sum; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(b,a); } x 3.0 4600 y 4.0 4608 r ? 4610 th ? 4618 a 3.0 7380 b 4.0 7385 adr_r 4610 7388 adr_th 4618 738c sum 25.0 7380 7/6/2019 ECE Application Programming: Lecture 18

15 Functions - pass by address
#include <stdio.h> #include <math.h> void get_r_theta(double a, double b, double *adr_r, double *adr_th); void main() { double x,y,r,th; printf("Enter x, y components of vector: "); scanf("%lf %lf",&x,&y); // user enters 3,4 get_r_theta(x,y,&r,&th); printf("Vector with x=%lf and y=%lf has r=%lf, theta=%lf\n",x,y,r,th); } void get_r_theta(double a, double b, double *adr_r, double *adr_th) { double sum; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(b,a); } x 3.0 4600 y 4.0 4608 r 5.0 4610 th ? 4618 a 3.0 7380 b 4.0 7385 adr_r 4610 7388 adr_th 4618 738c sum 25.0 7380 7/6/2019 ECE Application Programming: Lecture 18

16 Functions - pass by address
#include <stdio.h> #include <math.h> void get_r_theta(double a, double b, double *adr_r, double *adr_th); void main() { double x,y,r,th; printf("Enter x, y components of vector: "); scanf("%lf %lf",&x,&y); // user enters 3,4 get_r_theta(x,y,&r,&th); printf("Vector with x=%lf and y=%lf has r=%lf, theta=%lf\n",x,y,r,th); } void get_r_theta(double a, double b, double *adr_r, double *adr_th) { double sum; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(b,a); } x 3.0 4600 y 4.0 4608 r 5.0 4610 th 36.87 4618 a 3.0 7380 b 4.0 7388 adr_r 4610 7390 adr_th 4618 7394 sum 25.0 7398 7/6/2019 ECE Application Programming: Lecture 18

17 Functions - pass by address
ECE Introduction to Computer Engineering I 02/09/2005 Functions - pass by address #include <stdio.h> #include <math.h> void get_r_theta(double a, double b, double *adr_r, double *adr_th); void main() { double x,y,r,th; printf("Enter x, y components of vector: "); scanf("%lf %lf",&x,&y); // user enters 3,4 get_r_theta(x,y,&r,&th); printf("Vector with x=%lf and y=%lf has r=%lf, theta=%lf\n",x,y,r,th); } void get_r_theta(double a, double b, double *adr_r, double *adr_th) { double sum; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(b,a); } x 3.0 4600 y 4.0 4608 r 5.0 4610 th 36.87 4618 7/6/2019 ECE Application Programming: Lecture 18 (c) 2005, P. H. Viall

18 ECE Application Programming: Lecture 18
Final notes Next time Finish pointer discussion PE2: Functions Reminders: Program 3 graded; regrades due F 10/19 Program 4 due M 10/22 This week: no Th office hours Exam 1, Q3b: if you answered (ii), (iii), and (iv), please see me to get partial credit 7/6/2019 ECE Application Programming: Lecture 18


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google