Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect FXP. 1Winter Quarter Course Wrap Up and Final Review Topics Lecture.
Spring Semester 2013 Lecture 5
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
Chapter Five Functions
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
An Introduction to Programming with C++ Fifth Edition
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review Topics.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 1Winter Quarter Personal Libraries.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M2P. 1Winter Quarter Midterm 2 Review Topics.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 1Winter Quarter Inheritance and Overloading.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 1Winter Quarter C++: I/O and Classes Lecture 25.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Learners Support Publications Functions in C++
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Methods.
Programming Fundamentals Enumerations and Functions.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Chapter 9: Value-Returning Functions
User-Written Functions
Lecture 7: Repeating a Known Number of Times
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
A First Book of ANSI C Fourth Edition
Chapter 9: Value-Returning Functions
CS149D Elements of Computer Science
Functions Extra Examples.
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
CPS125.
Presentation transcript:

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 2Winter Quarter Things to Look Out For Vocabulary –Prototypes –Definition –Call –Return type –Arguments or parameters Local variables Pass by value Pass by reference

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 3Winter Quarter User-Written Functions So far, we have written only one function ourselves. That function is called "main ( )". The syntax used has been as follows: int main ( ) { /* Declarations */ /* Statements */ return 0; } The word int means that the function is expected to return an integer value to another function.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 4Winter Quarter User-Written Functions The parentheses, ( ), both indicate that main is a function, AND provide a place for any variable names (arguments) that could be sent as values from another function. Since main is the main program function, it will normally not have a parameter list. (Who calls the main function anyway?) Most, but not all, other functions have parameter lists. Some such, as rand ( ), do not since they need no data or values from the calling function.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 5Winter Quarter Writing User-Written Functions There are typically three different types of statements needed to properly set-up and make use of a user-written function. They are: 1. The function prototype statement. 2. The first line of the function definition. 3. The calling statement.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 6Winter Quarter 1. The Function Prototype Statement. The function prototype's format is (note semicolon): return-value-type function-name (parameter-list) ; The return-value-type may be any legal data type such as int, long, float, double, char, or it may be void. The parameter list is a list of data-types for the arguments and, optionally, the names of arguments. Example: float sum ( int, int, int ) ; /* OR */ float sum ( int a, int b, int c ) ;

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 7Winter Quarter 2. The First Line of the Function Definition. The first line of the function definition is the same as the examples of the function prototype except that the SEMICOLON IS ELIMINATED and variable or argument NAMES ARE REQUIRED. Example: float sum ( int a, int b, int c ) Here, the function sum is of type float and has three calling parameters, all of which are of type int. The variables a, b, and c take on the values of the variables or constants used in the calling statement.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 8Winter Quarter The Body of the Function A complete function consists of the the function definition and the body of the function in the { }. Example: float sum ( int a, int b, int c) { float total; total = a + b + c ; return total; } Note: The function has return statement because it has a return type.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 9Winter Quarter The Body of the Function float sum ( int a, int b, int c) { float total; total = a + b + c ; return total; } The names a, b, and c are known only inside sum. Likewise, any variables, like total, declared within the function are local variables, and they are known only inside the function in which they are defined.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 10Winter Quarter 3. The Calling Statement. The function call (or invocation) is like the first line of the function definition except NO DATA TYPES are shown and constants may be used instead of variable names. The valued returned by a function is normally assigned to a variable in the calling function. Example: value = sum ( i, j, k ) ; or value = sum ( 5, 6.73, 2 ) ;

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 11Winter Quarter Factorial Function Main Program #include long factorial ( int x ) ; int main ( ) { int k; for ( k=0 ; k<=10 ; k++ ) { printf ("%2d factorial is %ld\n", k, factorial (k) ) ; } return 0 ; } ? ?

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 12Winter Quarter Factorial Function long factorial ( int x ) { long fact = 1; int k; if ( x < 0 ) return 0; else if ( x==0 || x==1 ) return 1; else { for ( k = 2 ; k <= x ; k++ ) fact = fact * k ; return fact ; }

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 13Winter Quarter Factorial Program Output 0 factorial is 1 1 factorial is 1 2 factorial is 2 3 factorial is 6 4 factorial is 24 5 factorial is factorial is factorial is factorial is factorial is factorial is

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 14Winter Quarter The Next Slide is VERY VERY Important!!

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 15Winter Quarter User-Written Functions (example): #include void swap ( int a, int b ) ; int main ( ) { int a = 5, b = 6; printf ("a=%d b=%d\n",a,b); swap (a, b); printf ("a=%d b=%d\n",a,b); return 0; } void swap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf ("a=%d b=%d\n", a, b ); } a=5 b=6 a=6 b=5 a=5 b=6

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 16Winter Quarter Call By Value vs. Call By Reference Two ways in which variables are passed: –Call by value –Call by reference "Call by value" means function only gets a copy of the value of the calling argument. The called function can not change the value back in the calling routine. This is normally how it is in C. "Call by reference" means the function gets the original argument, and thus the called function can change the value of the variable back in the calling routine. Must use pointers in C to do this.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 17Winter Quarter Problem G11 Problem G11 calculates the values of sine for angles between 0 and 90 degrees. You are provided with a partial program that has a main function and a user written sine function. You are to complete the user written sine function (mysine(x) ). The main program will compare the calculations or your mysine function with the sin function from the math.h library.