©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Slides:



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

Introduction to C Programming
 2006 Pearson Education, Inc. All rights reserved Functions.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
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.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Computers and Programming Introduction to Methods in Java.
 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.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
© 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.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
C Functions Pepper. Objectives Create functions Function prototypes Parameters – Pass by value or reference – Sending a reference Return values Math functions.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Functions Review.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
© 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.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Chapter 6: Function Introduction to function Standard functions User defined functions –function prototype –function definition Function call Storage classes.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 6: User-Defined Functions I
Functions and an Introduction to Recursion
C Functions Pepper.
© 2016 Pearson Education, Ltd. All rights reserved.
JavaScript: Functions
Programming Fundamentals Lecture #7 Functions
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
User-Defined Functions
6 Functions.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Chapter 6 Methods: A Deeper Look
Chapter 5 - Functions Outline 5.1 Introduction
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Scope, Parameter Passing, Storage Specifiers
Functions Declarations CSCI 230
Chapter 5 Function Basics
Chapter 6 Methods: A Deeper Look
Chapter 9 Classes: A Deeper Look, Part 1
Chapter 6 - Functions Outline 5.1 Introduction
MSIS 655 Advanced Business Applications Programming
Functions, Part 1 of 3 Topics Using Predefined Functions
Topics Introduction to Functions Defining and Calling a Function
Functions, Part 1 of 3 Topics Using Predefined Functions
CS1100 Computational Engineering
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
6 Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Presentation transcript:

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. Introduction Most computer programs that solve real-world problems are much larger than the programs presented in the first few chapters. Experience has shown that the best way to develop and maintain a large program is to construct it from smaller pieces, each of which is more manageable than the original program. This technique is called divide and conquer. This chapter describes some key features of the C language that facilitate the design, implementation, operation and maintenance of large programs. ©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

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, Inc., Hoboken, NJ. All rights reserved.

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. The statements defining the function are written only once, and the statements are hidden from other functions. Functions are invoked by a function call, which specifies the function name and provides information (as arguments) that the called function needs to perform its designated task. ©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 5.9  Passing Arguments By Value and By Reference. WE WILL COVER THIS TOPIC IN POINTERS In many programming languages, there are two ways to pass arguments—pass-by-value and pass-by-reference. When arguments are passed by value, a copy of the argument’s value is made and passed to the called function. Changes to the copy do not affect an original variable’s value in the caller. When an argument is passed by reference, the caller allows the called function to modify the original variable’s value. Pass-by-value should be used whenever the called function does not need to modify the value of the caller’s original variable. ©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 5.9  Passing Arguments By Value and By Reference (Cont.) WE WILL COVER THIS TOPIC IN POINTERS This prevents the accidental side effects (variable modifications) that so greatly hinder the development of correct and reliable software systems. Pass-by-reference should be used only with trusted called functions that need to modify the original variable. In C, all arguments are passed by value. In Chapter 6, we’ll see that array arguments are automatically passed by reference for performance reasons. ©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.  Scope Rules ©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.   Scope Rules (Cont.) static still have block scope, even though they exist from before program startup. ©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. Preprocessor ©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. #include <stdio.h> #if 0 #define DUMMY 20 #else #define DUMMY 10 #endif int main(){ printf("%d\n",DUMMY); return 0; } #include <stdio.h> #define TEMP 10 #if defined(TEMP) #define DUMMY 20 #else #define DUMMY 10 #endif int main(){ printf("%d\n",DUMMY); return 0; } ©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. Header Files ©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. So far Putting all what we learnt together in the next lab and on the last lecture of this week (Thursday). ©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. Recursion ©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Simple Recursion example #include <stdio.h> void rec(int n); int main() { int n =6; rec(n); return 0; } void rec(int n) { printf("%d ",n); if (n==0) return; else return(rec(n-1)); ©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.