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

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Introduction to C Programming
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Modular Programming With Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
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.
 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.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
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.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
C++ for Engineers and Scientists Third Edition
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modular Programming with Functions.
1 Chapter 9 Scope, Lifetime, and More on Functions.
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.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
© 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 6: User-Defined Functions
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
C Programming Lecture 8-1 : Function (Basic). What is a Function? A small program(subroutine) that performs a particular task Input : parameter / argument.
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 Random Number Generation  rand function – Load – Returns "random" number between
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 ICS103 Programming in C Lecture 8: Functions I.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
CHAPTER 8 Scope, Lifetime, and More on Functions.
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.
A First Book of ANSI C Fourth Edition
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
Functions Course conducted by: Md.Raihan ul Masood
C Functions -Continue…-.
Functions, Part 2 of 2 Topics Functions That Return a Value
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Functions in C Mrs. Chitra M. Gaikwad.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
User-defined Functions
Modular Programming with Functions
Scope, Parameter Passing, Storage Specifiers
Chapter 6 - Functions Outline 5.1 Introduction
6 Chapter Functions.
User-defined Functions
Chapter 7: User-Defined Functions II
1-6 Midterm Review.
Presentation transcript:

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions

Etter/Ingber Modularity

Etter/Ingber Modularity 4 Execution of a program begins in the main function 4 The main function can call other functions –Functions defined in the same file –Function defined in other files or libraries 4 Functions are also referred to as modules 4 A module is a set of statements that performs a task or computes a value

Etter/Ingber Advantages of using modules 4 Modules can be written and tested separately 4 Large projects can be developed in parallel 4 Reduces length of program, making it more readable 4 Promotes the concept of abstraction

Etter/Ingber Programmer Defined Functions

Etter/Ingber Functions 4 Defined to –return a single value to the calling function –perform a task –change the value of the function arguments (call by reference)

Etter/Ingber Functions 4 Pre-defined –standard libraries 4 Programmer defined

Etter/Ingber Pre-defined Functions Example #include int main(void) { double angle; printf( input angle in radians: \n); scanf(%1f, &angle); printf( \nthe sine of the angle is %f\n,sin(angle) ); return 0; }//end main

Etter/Ingber Programmer Defined Functions Terminology 4 Function Prototype –describes how a function is called 4 Function Call 4 Actual parameter –used in the function call 4 Function Definition 4 Formal Parameters –used in function definition 4 Formal parameters must match with actual parameters in order, number and data type.

Etter/Ingber Value Returning Functions 4 Function returns a single value to the calling program 4 Function definition declares the type of value to be returned 4 A return expression; statement is required in the function definition

Etter/Ingber Example - factorial function /*function definition n! = n*(n-1)*(n-2)*…*1, 0! = 1 by definition - fact returns n! assumes n is non-negative integer */ int fact(int n) { int fact = 1; while(n>1) { fact = fact*n; n--; }//end while block return(fact); }//end fact

Etter/Ingber Function prototype - prototype can be included with preprocessor directives, or with variable declarations. #include int main(void) { /* Declare variables and function prototypes. */ int n; int fact(int n); printf(Enter a positive integer\n); scanf("%i, &n); if(n>=0) printf(%i! is %i\n, n, fact(n) ); return 0; } Note: In this example the function fact is called in the printf statement.

Etter/Ingber Calling a function - the value returned by a function can be assigned to a variable, printed, or used in an expression #include int main() { /*Declare variables and function prototypes */ int fact(int); int n, factorial; printf(enter positive integer\n); scanf(%lf,&n); if(n>=0) { factorial = fact(n); printf(%i! is %i\n, n, factorial); } return 0; }

Etter/Ingber void Functions 4 A void function may be called to perform a particular task (clear the screen) modify data perform input and output 4 A void function does not return a value to the calling program 4 if a return; statement is used (no return value)

Etter/Ingber Example of void function definition void print_date(int mo, int day, int year) { /*output formatted date */ printf(%i%i%i\n, mo, day, year ); return; }

Etter/Ingber Parameter Passing 4 Call by value –formal parameter receives the value of the actual parameter –function can not change the value of the actual parameter (arrays are an exception) 4 Call by reference –actual parameters are pointers (pointers will be discussed in chapter 6)

Etter/Ingber Storage Class and Scope 4 Scope refers to the portion of the program in which it is valid to reference a function or a variable 4 Storage class refers to the lifetime of a variable

Etter/Ingber Scope 4 Local scope - a local variable is defined within a function or a block and can be accessed only within the function or block that defines it 4 Global scope - a global variable is defined outside the main function and can be accessed by any function within the program file.

Etter/Ingber Storage Class - 4 types 4 automatic - key word auto - default for local variables –Memory set aside for local variables is not reserved when the block in which the local variable was defined is exited. 4 external - key word extern - used for global variables –Memory is reserved for a global variable throughout the execution life of the program. 4 static - key word static –Requests that memory for a local variable be reserved throughout the execution life of the program. The static storage class does not affect the scope of the variable. 4 register - key word register –Requests that a variable should be placed in a high speed memory register.