1 ICS103 Programming in C Lecture 8: Functions I.

Slides:



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

Introduction to C Programming
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
CS 201 Functions Debzani Deb.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
ICS103 Programming in C Lecture 9: Functions I
Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 6: Functions.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
C++ for Engineers and Scientists Third Edition
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Functions Why we use functions C library functions Creating our own functions.
1 ICS103 Programming in C Ch3: Top-Down Design with Functions.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
142 F -1 Functions chapter 3 of the text int main(void) { double x,y,z; … x = cube(y/3.0); … printf(“%f cubed is %f”,x,cube(x)); … return 0; } double cube(double.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
User defined functions
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
1 ICS103 Programming in C Lecture 9: Functions I.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.
User-Written Functions
Functions, Part 2 of 2 Topics Functions That Return a Value
CSCI 161: Introduction to Programming Function
User-Defined Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
CS149D Elements of Computer Science
6 Functions.
Functions, Part 2 of 3 Topics Functions That Return a Value
Standard Version of Starting Out with C++, 4th Edition
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Top-Down Design with Functions
Presentation transcript:

1 ICS103 Programming in C Lecture 8: Functions I

2 Outline Review about Functions Types of Functions  void Functions with Arguments –Actual Arguments & Formal Parameters –Writing Modular programs using functions  Functions with Input Argument and a Single Result –Re-usability of Functions –Logical Functions –Functions with Multiple Arguments –Argument List Correspondence –The Function Data Area Advantages of Using Function Subprograms  Reuse of Functions.

3 Types of Functions We use function arguments to communicate with the function. There are two types of function arguments:  Input arguments – ones that are used to pass information from the caller (such as main function) to the function.  Output arguments – ones that return results to the caller from the function. [we shall learn about these in the next lecture] Types of Functions  No input arguments, no value returned – void functions without arguments [already discussed in chapter 3]  Input arguments, no value returned - void functions with arguments.  Input arguments, single value returned.  Input arguments, multiple value returned [next lecture]

4 void Functions with Input Arguments … A function may take one or more arguments as input but returns no result. Such functions should be declared as void, but each argument should be declared in the bracket following the function name An argument is declared in the same way as variables (its type followed by the name of the argument). Example: void print_rboxed(double rnum); If there are more than one argument, they should be separated by comma. Example: void draw_rectangle(int length, int width); The following function example displays its argument value in a rectangular box.

5 void Functions with Input Arguments… /* Uses the print_rboxed function to display a double argument */ #include void print_rboxed(double rnum); //prototype for the function int main(void) { double x; printf("Enter a double value > "); scanf("%lf", &x); print_rboxed(x); //function call return 0; } /* Displays a real number in a box. */ void print_rboxed(double rnum) { printf("***********\n"); printf("* *\n"); printf("* %7.2f *\n", rnum); printf("* *\n"); printf("***********\n"); }

6 Actual Arguments & Formal Parameters Actual argument: an expression used inside the parentheses of a function call. Formal parameter: An identifier that represents a corresponding actual argument in a function definition. Actual argument can be any expression that evaluates to a value expected by the function: x, 125.5, x+y, etc. When the function call is encountered at run-time, the expression for the actual argument is first evaluated, the resulting value is assigned to the formal parameter, then the function is executed. Arguments make functions more versatile because they enable a function to manipulate different data each time it is called.

7 Functions with Input Argument and a Single Result By far, the must common types of functions in C are those that takes one or more arguments and return a single result. For example, virtually all the functions in the library, sqrt, log, abs, sin, cos, etc. are in this category. Unlike void functions, for which the function call is a statement on its own, functions that return a single result are often called as part of another expression. To declare these types of function, instead of void, the function name is preceded by the type of result the function returns (int, double, char, etc.).

8 Logical functions As we have observed by now, C uses integers to represent logical values. Thus, a function that returns a logical result should be declared as type int. In the implementation, the function return 0 for false or any other value for true. Example, the following is a logical function that checks if its integer argument is even or not. One important advantage of using functions is that they are reusable. The function is used as follows: /* Indicates whether or not num is even. Returns 1 if it is, 0 if not */ int even(int num) { int ans; ans = ((num % 2) == 0); return (ans); } if (even(n)) even_nums++; else odd_nums++;

9 Argument List Correspondence When using multiple-argument functions, the number of actual argument used in a function call must be the same as the number of formal parameters listed in the function prototype. The order of the actual arguments used in the function call must correspond to the order of the parameters listed in the function prototype. Each actual argument must be of a data type that can be assigned to the corresponding formal parameter with no unexpected loss of information.

10 The Function Data Area Each time a function call is executed, an area of memory is allocated for storage of that function’s data. Included in the function data area are storage cells for its formal parameters and any local variables that may be declared in the function. Local Variables: variable declarations within a function body.  Can only be used from within the function they are declared in – no other function can see them  These variables are created only when the function has been activated and become undefined after the call. The function data area is always lost when the function terminates. It is recreated empty when the function is called again.  So if you set a local variable value, that value will be reset again next time the function is called.

11 Why do we use Functions? There are two major reasons: 1.A large problem can be solved easily by breaking it up into several small problems and giving the responsibility of a set of functions to a specific programmer. It is easer to write two 10 line functions than one 20 line function and two smaller functions will be easier to read than one long one. 2.They can simplify programming tasks because existing functions can be reused as the building blocks for new programs. Really useful functions can be bundled into libraries.

12 Reuse of Function Subprograms Functions can be executed more than once in a program.  Reduces the overall length of the program and the chance of error. Once you have written and tested a function, you can use it in other programs or functions.

13 Common Programming Errors Remember to use a #include preprocessor directives for every standard library from which you are using functions. Place prototypes for your own function subprogram in the source file preceding the main function; place the actual function definitions after the main function. The acronym NOT summarizes the requirements for argument list correspondence.  Provide the required Number of arguments  Make sure the Order of arguments is correct  Make sure each argument is the correct Type or that conversion to the correct type will lose no information. Include a statement of purpose on every function you write. Also be careful in using functions that are undefined on some range of values.