Deitel- C:How to Program (5ed)

Slides:



Advertisements
Similar presentations
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.
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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
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.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
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.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
1 Lecture 3 Part 1 Functions with math and randomness.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 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. Functions (Header files and Library Functions) Outline.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
© 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. 5.3Math Library Functions Math library functions –perform.
© 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.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
Dale Roberts CSCI 230 Functions Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
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.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
5 C Functions.
Functions Course conducted by: Md.Raihan ul Masood
Functions.
5 C Functions.
Functions and an Introduction to Recursion
5 C Functions.
C Functions Pepper.
Functions, Part 2 of 2 Topics Functions That Return a Value
Programming Fundamentals Lecture #7 Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals Lecture #7 Functions
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
6 Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Functions Declarations CSCI 230
Chapter 6 - Functions Outline 5.1 Introduction
5 C Functions.
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
5 C Functions.
Function.
1-6 Midterm Review.
6 Functions.
Review Lab assignments Homework #3
Assignment Operators Topics Increment and Decrement Operators
Function.
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Deitel- C:How to Program (5ed) (NTUST-ME2008) C Functions Deitel- C:How to Program (5ed) Week01 –Brief Intro. Of C – ME2008---2010Q1-NTUST.ME

OBJECTIVES In this chapter you will learn: To construct programs modularly from small pieces called functions. The common math functions available in the C Standard Library. To create new functions. The mechanisms used to pass information between functions. Simulation techniques using random number generation.

5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions 5.5 Function Definitions 5.6 Function Prototypes 5.7 Function Call Stack and Activation Records 5.8 Headers 5.9 Calling Functions: Call-by-Value and Call-by- Reference

5.10 Random Number Generation 5.11 Example: A Game of Chance 5.12 Storage Classes 5.13 Scope Rules 5.14 Recursion 5.15 Example Using Recursion: Fibonacci Series 5.16 Recursion vs. Iteration

5.3 Math Library Functions perform common mathematical calculations #include <math.h> Format for calling functions FunctionName( argument ); If multiple arguments, use comma-separated list printf( "%.2f", sqrt( 900.0 ) ); Calls function sqrt, which returns the square root of its argument All math functions return data type double Arguments may be constants, variables, or expressions

Fig. 5.2 | Commonly used math library functions. (Part 1 of 2.)

Fig. 5.2 | Commonly used math library functions. (Part 2 of 2.)

5.4 Functions Functions Benefits of functions Modularize a program All variables defined inside functions are local variables Known only in function defined Parameters Communicate information between functions Local variables Benefits of functions Divide and conquer Manageable program development Software reusability Use existing functions as building blocks for new programs Abstraction - hide internal details (library functions) Avoid code repetition

5.5 Function Definitions Function definition format return-value-type function-name( parameter-list ) { declarations and statements } Function-name: any valid identifier Return-value-type: data type of the result (default int) void – indicates that the function returns nothing Parameter-list: comma separated list, declares parameters A type must be listed explicitly for each parameter unless, the parameter is of type int

5.5 Function Definitions Function definition format (continued) return-value-type function-name( parameter-list ) { declarations and statements } Definitions and statements: function body (block) Variables can be defined inside blocks (can be nested) Functions can not be defined inside other functions Returning control If nothing returned return; or, until reaches right brace If something returned return expression;

Outline fig05_03.c Function prototype indicates function will be defined later in the program Call to square function Function definition

Outline fig05_04.c (1 of 2 ) Function prototype Function call

Outline Function definition fig05_04.c (2 of 2 )

5.6 Function Prototypes Function prototype Function name Parameters – what the function takes in Return type – data type function returns (default int) Used to validate functions Prototype only needed if function definition comes after use in program The function with the prototype int maximum( int x, int y, int z ); Takes in 3 ints Returns an int Promotion rules and conversions Converting to lower types can lead to errors

Fig. 5.5 | Promotion hierarchy for data types.

5.8 Headers Header files Custom header files Contain function prototypes for library functions <stdlib.h> , <math.h> , etc Load with #include <filename> #include <math.h> Custom header files Create file with functions Save as filename.h Load in other files with #include "filename.h" Reuse functions

Fig. 5.6 | Some of the standard library headers. (Part 1 of 3.)

Fig. 5.6 | Some of the standard library headers. (Part 2 of 3.)

Fig. 5.6 | Some of the standard library headers. (Part 3 of 3.)

5.9 Calling Functions: Call-by-Value and Call-by-Reference Copy of argument passed to function Changes in function do not effect original Use when function does not need to modify argument Avoids accidental changes Call by reference Passes original argument Changes in function effect original Only used with trusted functions For now, we focus on call by value

5.10 Random Number Generation rand function Load <stdlib.h> Returns "random" number between 0 and RAND_MAX (at least 32767) i = rand(); Pseudorandom Preset sequence of "random" numbers Same sequence for every function call Scaling To get a random number between 1 and n 1 + ( rand() % n ) rand() % n returns a number between 0 and n - 1 Add 1 to make random number between 1 and n 1 + ( rand() % 6) number between 1 and 6

Outline fig05_07.c Generates a random number between 1 and 6

Outline fig05_08.c (1 of 3 )

Outline fig05_08.c (2 of 3 )

Outline fig05_08.c (3 of 3 )

5.10 Random Number Generation srand function <stdlib.h> Takes an integer seed and jumps to that location in its "random" sequence srand( seed ); srand( time( NULL ) );/*load <time.h> */ time( NULL ) Returns the number of seconds that have passed since January 1, 1970 “Randomizes" the seed

Outline fig05_09.c (1 of 2 ) Seeds the rand function

Outline fig05_09.c (2 of 2 )