ECE Introduction to Computer Engineering I

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
An Introduction to Programming with C++ Fifth Edition
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Functions Pass by Value Pass by Reference IC 210.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
16&17-2 Grasp the concept of top-down programming Identify Function Headers and Prototypes Understand when and where prototypes used Understand how arguments.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CPS120: Introduction to Computer Science Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 C Programming Week 2 Variables, flow control and the Debugger.
Functions with Input/Ouput. Assignments Due – Lab 2 Reading – Chapter 4 –
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.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
UniMAP Sem2-09/10 DKT121:Fundamental of Computer Programming1 Functions (2)
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
User-Defined Functions (cont’d) - Reference Parameters.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
ECE Application Programming
Introduction to Programming
Chapter 7: User-Defined Functions II
EKT120: Computer Programming
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
Functions, Part 2 of 2 Topics Functions That Return a Value
Chapter 10: Void Functions
Variables have a type have an address (in memory) have a value
Function There are two types of Function User Defined Function
Module 4 Functions – function definition and function prototype.
Functions Department of Computer Science-BGU יום רביעי 12 ספטמבר 2018.
Programming Fundamentals Lecture #7 Functions
DKT121:Fundamental of Computer Programming
Functions in C Mrs. Chitra M. Gaikwad.
CSCI 161: Introduction to Programming Function
Functions.
Scope, Parameter Passing, Storage Specifiers
FUNCTIONS WITH ARGUMENTS
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company. 1
Functions.
ICS103 Programming in C Lecture 13: Arrays II
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
A function with one argument
Week 2 Variables, flow control and the Debugger
Chapter 7: User-Defined Functions II
Chapter 9: Value-Returning Functions
Function.
Introduction to Problem Solving and Programming
Functions Extra Examples.
EECE.2160 ECE Application Programming
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
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
Function.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
C Parameter Passing.
Presentation transcript:

ECE 160 - Introduction to Computer Engineering I 02/09/2005 Functions Functions used to break problem down into small, "bite-sized" 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. (c) 2005, P. H. Viall

Functions Alternate way of writing above function 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

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 )

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 ?

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 ?

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 ?

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 ?

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

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

NOTE - a and b are NOT copied back to x and y 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 NOTE - a and b are NOT copied back to x and y

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

Answer Trgle w legs 5.000000 and 12.000000 has hyp of 5.00000

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,h,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, result; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(y,x); } x 4600 y 4608 r 4610 th 4618

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,h; 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, result; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(y,x); } x 3.0 4600 y 4.0 4608 r ? 4610 th ? 4618

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,h; 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, result; 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 result 7385

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,h; 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, result; 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 result ? 7385

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,h; 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, result; 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 result ? 7385

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,h; 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, result; 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 result 36.87 73a0

Functions - pass by address ECE 160 - 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,h; 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, result; 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 (c) 2005, P. H. Viall