16&17-2 Grasp the concept of top-down programming Identify Function Headers and Prototypes Understand when and where prototypes used Understand how arguments.

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
Modular Programming With Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Chapter 6: User-Defined Functions I
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
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.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
21-2 Understand various methods of sorting. Use the qsort function to sort. Binary search Related Chapter: ABC 8.5.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 13 Thanks for lecture slides: Prentice Hall, Inc., 2. C++
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
 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.
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
User-Written Functions
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Functions, Part 2 of 2 Topics Functions That Return a Value
Lecture 13 & 14.
CSC113: Computer Programming (Theory = 03, Lab = 01)
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Functions Declarations CSCI 230
Lecture 18 Arrays and Pointer Arithmetic
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
A function with one argument
A First Book of ANSI C Fourth Edition
Presentation transcript:

16&17-2 Grasp the concept of top-down programming Identify Function Headers and Prototypes Understand when and where prototypes used Understand how arguments are passed to a function using call-by-value or call-by-reference Related Chapters: ABC Chapters 5.1 – 5.7

16&17-3 In lecture slides 2-17 and we discussed a method for creating programs. In step three “Develop the Algorithm”, the nature of the programming problem may suggest that we use top down programming (as opposed to bottom-up programming,...) An analogy for top-down design is the following: Suppose a general contractor was hired to construct a commercial building. The contractor would need to know where to build and what are the blue- prints. Then the contractor would hire various sub-contractors to dig foundation, install electric, plumbing, frame, roof. That is, the large job would be broken into smaller jobs, however these jobs must be performed in some sequence and not at random.

16&17-4 In top-down design for programming, the first function to be invoked in a program (the top function or “general contractor”) is called “main”. The programmer has the “main” function call other component functions (sub-contractors), which may in turn call functions, (sub- sub-contractors) and so on.

16& Problem Definition Write a function “sinc” that computes the value of sin(x)/x. If x is zero return Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = One real number of type double. Output= sin(x)/x as a double if x is not zero else Develop Algorithm Use an if statement to check for a non-zero value.

16&17-6 #include double sinc (double z) /* function header */ { if (z == 0.0) return 1.0; else return sin(z) / z; /* notice that sinc calls sin */ } void main(void) { double x, y; printf("Enter a number: "); scanf("%lf", &x); y = sinc(x); /* function call */ printf("\n sinc(%lf) = %lf \n", x, y); } /* end of main */

16&17-7 Using gedit, user-defined functions can be typed into the same file as “main”. Functions may be typed into a separate file. For example, suppose “main” is in the file test.c and the user defined function “sinc” is entered into the file func.c then to compile both “main” and “sinc” you would type (at the Unix prompt) gcc test.c func.c -lm Note the use of the “-lm” option The -lm option is needed only if you use a math library function in “main” or “sinc”. Don’t forget that if you put the function “sinc” into a separate file, then you must still put the prototype for “sinc” in the same file as main. The prototype for “sinc” must precede the function main.

16&17-8 The function definition in C is of the form: return-type function-name(parameter-list) { /* function code */ return (value); /* parenthesis optional */ } The “value” can be a single variable, constant, or any C expression. The data type of “value” should be of type “ return-type ”, if not then a conversion of the “value” to the “ return-type ” is specified by the C compiler. If the “return-type is void then the programmer can omit the return statement or code: return ; /* return nothing */

16&17-9 A function may be called from any function (including itself) in C. A call takes one of the following forms: function-name(argument-list); var = function-name(argument-list); To “call” or invoke a function, you must pass the same number of arguments for which the function has parameters. The order of the arguments is important!!! If the data types in the argument list do not match the data types in the parameter list, a conversion(or coercion) takes place. (see p. 132 in ABC)

16&17-10 /* code fragment in main */ int x = 1; float y = 2.0; float value; double z = 5.0; value = sum(x,y,z+4.0); } /* end of main */ /* sum adds an integer a float and a double and returns */ /* a double */ double sum(int a, float b, double c) { double result; result = a + b + c; return result; } Data-types agree! Since value has type float and sum returns a double, a conversion takes place.

16&17-11 A function that does not return a value is declared to be of type void. e.g., void printFunction (int x, int y) If a function takes no parameters, we specify the parameter list as void. e.g., void printPageHeading (void) It is also possible to specify an "empty" parameter list: void myFunction ( ) - argument checking is "turned off", so that anything can be passed to myFunction. Best to avoid this in C.

16&17-12 By default, if you omit the data type of the return value, C assumes that the function returns an int value (not void). Therefore the following function header: main(void) int main(void) is equivalent to:

16& Problem Definition Redo the problem in Lecture 15 slides 12,13 using a modular approach. The problem is: Write a program that inputs a list of numbers into an array. The program then calculates the average value, and then prints a list of differences. The differences are computed by taking the original values in the list minus the average. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = list of reals in a file “input.dat”. We will use Unix redirection to read values from this file. Output= The average and the list of differences.

#include int read(double alias[]); /* prototype for read func */ double ave(int count,double alias[]); /* prototype for ave function */ void diff(double average, int count, double alias[]); /* prototype for diff function */ void main(void) { int counter,k; double datVal[500]; /* 500 element max */ double datAve; counter = read(datVal); /* read file into datVal */ datAve = ave(counter,datVal); /* compute, print average */ printf("The average is:%f \n", datAve); diff(datAve,counter,datVal); /* compute diff list */ for(k=0;k<counter;++k) /* print the diff list */ printf("%lf\n", datVal[k]); } /* end of main */

int read(double alias[]) { int count = 0; while (EOF != scanf("%lf", &alias[count])) { ++count; } return count; } double ave(int count,double alias[]) { int i; double sum = 0.0; for (i=0;i<count;++i) sum += alias[i]; return sum/count; } void diff(double average, int count, double alias[]) { int i; for (i = 0;i<count;++i) alias[i] -= average; }

16& Use gedit to enter the above code in a file problem1.c, and make sure to save the file. 2. Use gedit to create an input file input.dat and enter in integers into this file. Save and exit this file. 3. Compile the problem1.c code by typing gcc problem1.c 4. Run the program using Unix redirection <./a.out < input.dat

Example: If the file input.dat has the values: Then the program would execute as follows: unixprompt>./a.out < input.dat The average is: unixprompt>

16&17-18 In the latest versions of standard C ("ANSI C"), the function prototype is used to define the data type to be returned by the function, the number and data type of function parameters, and the order of the function parameters. This info is used by the C compiler to validate calls to the function. A general principle in C is that you must declare or define the identifier (variable or function) before you use them.

16&17-19 #include /* include statements */ int read(double alias[]); /* prototype for read function */ void main(void) /* definiton of the main function */ { /* C code here */ } /* end of main */ int read(double alias[]) /* function definition */ { /* more C code here */ } /* end of read */ One method to code the function “ read ”in C :

16&17-20 The actual variable or array name is not needed in the function prototype. Examples: Instead of the following prototypes int read(double alias[]); double ave(int count,double alias[]); void diff(double average, int count, double alias[]); we could have used int read(double []); double ave(int,double []); void diff(double, int, double []);

16&17-21 #include /* include statements */ /* Since the definition of read occurs before it is */ /* used (called) by main, we don’t need a prototype.*/ int read(double alias[]) /* function definition */ { /* C code for read here */ } /* end of read */ void main(void) /* definiton of the main function */ { /* C code for main function here */ } /* end of main */ Another method to code the function “ read ” in C without using a prototype :

16&17-22 Arrays are passed as call-by-reference. counter = read(datVal);... int read(double alias[]) { int count = 0; while (EOF != scanf("%lf", &alias[count])) { ++count; } return count; } Address Main Memory after call unknown datVal[0] datVal[1] datVal[2] 1200 Address Main Memory before call datVal[0] datVal[1] datVal[2]...

16&17-23 Non-arrays are passed as call-by-value. datAve = ave(counter,datVal);... double ave(int count,double alias[]) { int i; double sum = 0.0; for (i=0;i<count;++i) sum += array[i]; return sum/count; } 1196 Address while executing ave 5 counter 1192 Address Main Memory before call count... The value of the parameter “counter” is copied into the memory location for the variable “count”. Therefore any change to “count” would have no affect on “counter” counter 5 55

16&17-24 For two dimensional arrays, we could omit specification of the number of rows in function prototype and header. But we must include size declaration for the number of columns. (see example on next slide)

16&17-25 void doSumpin(int [][5]); int main (void) { int a[10][5] = {{0}}; doSumpin (a); } void doSumpin(int b[][5]) /* array passed by reference */ {... } /* function prototype */ /* function call */