Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
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.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
 2007 Pearson Education, Inc. All rights reserved C Functions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
CHAPTER:09 METHODS(FUNCTIONS) IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Introduction to Methods
C programming---Arrays scalar: capable of holding a single data item aggregate variables: capable of holding a collections of values. Two kinds of aggregates.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Learners Support Publications Functions in C++
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.
COP 3275: Chapter 09 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
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.
Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures.
Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Sorting –Bubble sort –Merge sort –Quick sort –Selection.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Chapter 9: Functions Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Chapter 9: Functions Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Chapter 12: Pointers and Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
User-Written Functions
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
Chapter 6: User-Defined Functions I
C programming---Arrays
9. FUNCTIONS.
CS1061 C Prgramming Lecture 11: Functions
Programmazione I a.a. 2017/2018.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Functions.
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Lecture [7][0] Functions
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company. 1
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Chapter 6: User-Defined Functions I
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Presentation transcript:

Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions. The return statement Issues of program termination Functions that are recursive Chapter 9 - Functions

Our programs so far have consisted of just one function main. This chapter focuses on writing our own functions. A function is a series of statements that have been grouped together and given a name. Functions are the building blocks of C programs. Each function is essentially a small program, with its own declarations and statements. Using functions, we can divide a program into small pieces that are easier to follow, understand and modify. Functions are reusable: we can take a function that was originally part of one program and use it in others. Functions allow us to avoid duplicating code.

Function Definitions return-type function-name (parameters) { declarations statements } The "return type" of a function is the type of value that the function returns The following rules govern the return type: Functions may not return arrays, but no other restrictions on the return type. If the return type is omitted, the function is presumed to return a value of type int. Specifying the return type void indicates the function does not return a value. float average(float a, float b) { return (a + b) / 2; }

Declare each function before calling it. A function declaration provides the compiler with a brief glimpse at a function whose full definition will appear later. A function declaration is the first line of a function definition with a semicolon added at the end: return-type function-name (parameters); Function Declaration: #include float average(float a, float b); /* DECLARATION */ main() { float x, y, z; printf (“Enter three numbers: “); scanf("%f%f%f", &x, &y, &z) ; printf("Average of %g and %g: %g\n", x, y, average(x, y) ) ; printf("Average of %g and %g: %g\n", y, z, average(y, z) ) ; printf("Average of %g and %g: %g\n", x, z, average(x, z)) ; return 0 ; } float average(float a, float b)/* DEFINITION */ { return (a + b) / 2 ; }

float is average's return type: the type of data that the function returns each time it's called. The identifiers a and b (the function's parameters) represent the two numbers that will be supplied when average is called. A function parameter is essentially a variable whose initial value will be supplied later, when the function is called. Each parameter must have a type: float is the type of a and b. NOTE: (float must appear twice, once for a and once for b.) Compute the average of two float values. float average(float a, float b) { return (a + b) / 2; }

To activate (call) a function, write the function name, followed by a list of arguments: average (x, y) Arguments are used to supply information to a function; in this case, average needs to know which two numbers to average. The effect of the call average (x, y) is to copy the values of x and y into the parameters a and b, then execute the body of average. An argument doesn't have to be a variable, any expression of the proper type will do, average (5.1, 8.9) or average (x/2, y/3 ). Arrays can even be used as arguments. Every function has an executable part, called the body, which is enclosed in braces. The body of average consists of a single return statement. Executing this statement causes the function to "return" to the place from which it was called; the value of (a + b) / 2 will be returned by the function. float average(float a, float b) { return (a + b) / 2;/* BODY */ }

Note that the return value of average is not saved anywhere. The program prints it and then discards it. This below statement calls average, then saves its return value in the variable avg. avg = average (x,y); Put the call of average in the place where we need to use the return value. For example, we could write printf("Average: %g\n", average(x, y) ) ; to compute the average of x and y and then print it. This statement has the following effect: 1. It calls the average function, passing x and y as arguments. 2. average executes its return statement, returning the average of x and y. 3. printf prints the value that average returns. (The return value of average becomes one of printf's arguments.)

main() { float x, y, z; printf("Enter three numbers: "); scant("%f%f%f", &x, &y, &z) ; printf("Average of %g and %g: %g\n", x, y, average(x, y)); /* CALL */ printf("Average of %g and %g: %g\n", y, z, average(y, z) ) ; printf("Average of %g and %g: %g\n", x, z, average(x, z) ) ; return 0; } /* Computes pairwise averages of three numbers #include float average(float a, float b); /* DECLARATION */ float average(float a, float b)/* DEFINITION */ { return (a + b) / 2; } Enter three numbers: Average of 3.5 and 9.6: 6.55 Average of 9,6 and 10.2: 9.9 Average of 3.5 and 10.2: 6.85

The difference between a parameter and an argument. Parameters appear in function definitions; they're dummy names that represent values to be supplied when the function is called. Arguments are expressions that appear in function calls. In C, arguments are passed by value: When a function is called, each argument is evaluated and its value assigned to the corresponding parameter.

Program Termination Since main is a function, it must have a return type. We've never specified main's return type, which means that it is int by default. We can make the return type explicit if we choose: int main() { return 0; } The value returned by main is a status code. It can be tested when the program terminates. Main should return 0 if the program terminates normally; To indicate abnormal termination, main should return a value other than 0.

The exit Function Executing a return statement in main is one way to terminate a program. Another is calling the exit function, which belongs to. The argument passed to exit has the same meaning as main's return value: Both indicate program status at termination. For normal termination pass 0: exit(O) ; /* normal termination */ C allows us to pass EXIT_SUCCESS instead of 0 (the effect is the same): exit(EXIT_SUCCESS) ; /* normal termination */ Passing EXIT_FAILURE indicates abnormal termination: exit(EXIT_FAILuRE); /* abnormal termination */ The difference between return and exit is that exit can be called from any function, not just from main. Some programmers use exit exclusively so that a pattern-matching program can easily locate all exit points in a program.

Recursive Functions A function is recursive if it calls itself For example, the following function computes n! recursively, using the formula n! = nx(n- 1)! int fact(int n) {. if (n <= 1) return 1; else return n * fact(n-l);/* RECURSIVE CALL TO FACT */ } An example to look at QUICKSORT Algorithm pp