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

Slides:



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

Spring Semester 2013 Lecture 5
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Chapter Five Functions
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
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.
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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
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.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
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.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Lecture 5 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
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;
1 CS161 Introduction to Computer Science Topic #10.
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.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Pointers *, &, array similarities, functions, sizeof.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Methods.
ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Programming Fundamentals Enumerations and Functions.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
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.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
User-Written Functions
Functions.
CS1201: Programming Language 2
CPS125.
Presentation transcript:

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

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 2Winter Quarter User-Written Functions So far, we have written only one function ourselves. That is function is called "main ( )". The syntax used has been as follows: void main ( ) { /* Declarations */ /* Statements */ } The word void means that the function is not expected to return any values to another function.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 3Winter 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 Gateway Engineering Education Coalition Lect 12P. 4Winter 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 Gateway Engineering Education Coalition Lect 12P. 5Winter 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 Gateway Engineering Education Coalition Lect 12P. 6Winter 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 Gateway Engineering Education Coalition Lect 12P. 7Winter 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 Gateway Engineering Education Coalition Lect 12P. 8Winter Quarter The Body of the Function float sum ( int a, int b, int c) { int 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 Gateway Engineering Education Coalition Lect 12P. 9Winter 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 Gateway Engineering Education Coalition Lect 12P. 10Winter Quarter Factorial Function Main Program #include long factorial ( int x ) ; void main ( ) { int k; for ( k=0 ; k<=10 ; k++ ) { printf ("%2d factorial is %ld\n", k, factorial (k) ) ; }

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 11Winter 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 Gateway Engineering Education Coalition Lect 12P. 12Winter 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 Gateway Engineering Education Coalition Lect 12P. 13Winter Quarter User-Written Functions (example): #include void swap ( int a, int b ) ; void 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); } 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 Gateway Engineering Education Coalition Lect 12P. 14Winter 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.