1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Introduction to C Programming
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
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.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Functions Gabriel Hugh Elkaim Spring 2012.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C Programming Lecture 8-1 : Function (Basic). What is a Function? A small program(subroutine) that performs a particular task Input : parameter / argument.
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
DOCUMENTATION SECTION GLOBAL DECLARATION SECTION
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Department of Electronic & Electrical Engineering Statements Blocks{} Semicolons ; Variables Names keywords Scope.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 9: Value-Returning Functions
Chapter 7: User-Defined Functions II
Value-Returning Functions
Functions in C Mrs. Chitra M. Gaikwad.
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions Declarations CSCI 230
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Functions I Creating a programming with small logical units of code.
Functions, Part 1 of 3 Topics Using Predefined Functions
Local Variables, Global Variables and Variable Scope
Chapter 7: User-Defined Functions II
Chapter 9: Value-Returning Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Introduction to Problem Solving and Programming
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Scope Rules.
Presentation transcript:

1 Modularity In “C”

2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound statement

3 Example-1: //Write a function to display your info to screen. void display_myinfo (void) { printf (“Programmer: Good student \n”); printf (“ID: \n”); printf (“Course: CIT105 \n”); }

4 Invoke the function display_myinfo from main(): main() { display_myinfo(); } See computer demo

5 Example-2:  Write a function to compute the volume of a cubic box: float computeVolume (float side) { float volume; volume = side * side * side; return volume; }

6 Another way to write the same function: #include float computeVolume (float side) { return side * side * side; } Dummy name

7 // Invoke the function: int main() { float length, volume; printf(“ Enter side of the cube:”); scanf (“%f”, &length); // Invoke the function, store the result in vol: volume = computeVolume( length); // display the output: print f(“volume is = %f “, volume); }

8 // big picture: float computeVolume (float side) { return side * side * side; } int main() { float length, volume; printf(“ Enter side of the cube:”); scanf (“%f”, &length); // Invoke the function: volume = computeVolume( length); print f(“volume is = %f “, volume); return 0; }

9 Important:  You may not write the code for a function inside another function  functions cannot be nested inside each other.  You may invoke functions from other functions.

10 Scope Rules:  Definition: Scope of a variable is the segment of code where the variable is accessible. Scope of an identifier depends on where in the program it is declared. Scope of the variables declared in a function, is limited to that function.

11 Different Scopes in C:  Local Scope: Variables declared in a function, have local scope. They can only be used locally within that function. Such variables are referred to as local variables.  Global Scope: Variables declared outside the functions have global scope. They can be accessed in all the functions that appear after their declaration in the program. Must be used ONLY when absolutely necessary.

12 Cont…  Block Scope: Variables declared within a block { … } can be accessed only inside that block of code. Such variables have block scope. Avoid declaring block scope variables.

13 Scope of a function:  Scope of a function is from the point of its definition or declaration until the end of the program.  In order to invoke the functions from main(), you need to write the functions’ code before main() in the code window .  But main() is the driver of the program, and should appear before any other function!

14 Function Prototypes:  One line declaration of the function.  It is the first line of the function followed by ;  It provides the compiler with important information about the function: Function’s name, Return type, # of parameters, Data type of parameters.

15 Examples:  Prototype of the function display_myinfo(): void display_myinfo (void);  Prototype of the function computeVolume(): float computeVolume (float side); Or float computeVolume (float);

16 Arrangement of functions in the program: #include #define … // function prototypes int main() { … } // code for other functions

17 Talk about lab04