© 2016 Pearson Education, Ltd. All rights reserved.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
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.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
1 Lecture 3 Part 1 Functions with math and randomness.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
 Monday 10/18/2010  Content: Week 1 – Week 6  Format:  Multiple choice questions  Matching questions  Determine what’s wrong  Determine the results.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Functions Review.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
REEM ALMOTIRI Information Technology Department Majmaah University.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 5 C Functions C How to Program, 7/e
Functions Course conducted by: Md.Raihan ul Masood
Functions.
Chapter 6: User-Defined Functions I
© 2016 Pearson Education, Ltd. All rights reserved.
Functions and an Introduction to Recursion
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 2, Part I Introduction to C Programming
TMF1414 Introduction to Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
Chapter 5 Function Basics
JavaScript: Functions
JavaScript Functions.
Programming Fundamentals Lecture #7 Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
JavaScript: Functions.
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Functions Declarations CSCI 230
Chapter 6 Methods: A Deeper Look
Chapter 6 - Functions Outline 5.1 Introduction
Functions, Part 2 of 3 Topics Functions That Return a Value
Chapter 4 Writing Classes.
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
A First Book of ANSI C Fourth Edition
Chapter 6: User-Defined Functions I
6 Methods: A Deeper Look.
6 Functions.
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

© 2016 Pearson Education, Ltd. All rights reserved. Chapter 5 C Functions C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.

5.1 Modularizing Programs in C Functions are used to modularize programs C programs are typically written by combining new functions you write with prepackaged functions available in the C standard library. The C standard library provides a rich collection of functions for performing common mathematical calculations, string manipulations, character manipulations, input/output, and many other useful operations. © 2016 Pearson Education, Ltd. All rights reserved.

5.2 Modularizing Programs in C (Cont.) The functions printf, scanf and pow that we’ve used in previous chapters are standard library functions. You can write your own functions to define tasks that may be used at many points in a program. These are sometimes referred to as programmer-defined functions. Functions are invoked by a function call, which specifies the function name and provides information (as arguments) that the called function needs. © 2016 Pearson Education, Ltd. All rights reserved.

5.3 Math Library Functions Function arguments may be constants, variables, or expressions. If c1 = 13.0, d = 3.0 and f = 4.0, then the statement printf("%.2f", sqrt(c1 + d * f)); calculates and prints the square root of 13.0 + 3.0 * 4.0 = 25.0, namely 5.00. In the figure, the variables x and y are of type double. © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. 5.4  Functions Functions allow you to modularize a program. All variables defined in function definitions are local variables—they can be accessed only in the function in which they’re defined. Most functions have a list of parameters that provide the means for communicating information between functions. A function’s parameters are also local variables of that function. © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. 5.5  Function Definitions Each program we’ve presented has consisted of a function called main that called standard library functions to accomplish its tasks. We now consider how to write custom functions. Consider a program that uses a function square to calculate and print the squares of the integers from 1 to 10 (Fig. 5.3). © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

5.5 Function Definitions (Cont.) Function square is invoked or called in main within the printf statement printf("%d ", square(x)); // function call Function square receives a copy of the value of x in the parameter y. Then square calculates y * y. The result is passed back returned to function printf in main where square was invoked, and printf displays the result. This process is repeated 10 times using the for statement. © 2016 Pearson Education, Ltd. All rights reserved.

5.5 Function Definitions (Cont.) The parameter-list is a comma-separated list that specifies the parameters received by the function when it’s called. If a function does not receive any values, parameter-list is void. A type must be listed explicitly for each parameter. © 2016 Pearson Education, Ltd. All rights reserved.

5.5 Function Definitions (Cont.) main’s Return Type Notice that main has an int return type. The return value of main is used to indicate whether the program executed correctly. In earlier versions of C, we’d explicitly place return 0; at the end of main—0 indicates that a program ran successfully. The C standard indicates that main implicitly returns 0 if you to omit the preceding statement—as we’ve done throughout this book. © 2016 Pearson Education, Ltd. All rights reserved.

5.5 Function Definitions (Cont.) Function maximum Our second example uses a programmer-defined function maximum to determine and return the largest of three integers (Fig. 5.4). Next, they’re passed to maximum, which determines the largest integer. This value is returned to main by the return statement in maximum. © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

5.6 Function Prototypes: A Deeper Look An important feature of C is the function prototype. This feature was borrowed from C++. The compiler uses function prototypes to validate function calls. © 2016 Pearson Education, Ltd. All rights reserved.

5.6 Function Prototypes: A Deeper Look (Cont.) The function prototype for maximum in Fig. 5.4 is // function prototype int maximum(int x, int y, int z); It states that maximum takes three arguments of type int and returns a result of type int. Notice that the function prototype is the same as the first line of maximum’s function definition. © 2016 Pearson Education, Ltd. All rights reserved.

5.10 Random Number Generation We now take a brief and, hopefully, entertaining diversion into simulation and game playing. The element of chance can be introduced into computer applications by using the C standard library function rand from the <stdlib.h> header. Consider the following statement: i = rand(); The rand function generates an integer between 0 and RAND_MAX (a symbolic constant defined in the <stdlib.h> header). Microsoft Visual C++ RAND_MAX value is 32767 and on GNU gcc RAND_MAX value is 2147483647. © 2016 Pearson Education, Ltd. All rights reserved.

5.10 Random Number Generation (Cont.) For example, a program that simulates coin tossing might require only 0 for “heads” and 1 for “tails.” A dice-rolling program that simulates a six-sided die would require random integers from 1 to 6. Rolling a Six-Sided Die To demonstrate rand, let’s develop a program to simulate 20 rolls of a six-sided die and print the value of each roll. The function prototype for function rand is in <stdlib.h>. We use the remainder operator (%) in conjunction with rand as follows rand() % 6 to produce integers in the range 0 to 5. © 2016 Pearson Education, Ltd. All rights reserved.

5.10 Random Number Generation (Cont.) This is called scaling. The number 6 is called the scaling factor. The output of Fig. 5.7 confirms that the results are in the range 1 to 6—the actual random values chosen might vary by compiler. © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

5.10 Random Number Generation (Cont.) Rolling a Six-Sided Die 60,000,000 Times To show that these numbers occur approximately with equal likelihood, let’s simulate 60,000,000 rolls of a die with the program of Fig. 5.12. Each integer from 1 to 6 should appear approximately 10,000,000 times. As the program output shows, by scaling and shifting we’ve used the rand function to realistically simulate the rolling of a six-sided die. © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

5.10 Random Number Generation (Cont.) Note the use of the %s conversion specifier to print the character strings "Face" and "Frequency" as column headers. After we study arrays in Chapter 6, we’ll show how to replace this switch statement elegantly with a single-line statement. © 2016 Pearson Education, Ltd. All rights reserved.