Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

Slides:



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

Introduction to C Programming
C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
CS 201 Functions Debzani Deb.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Guide To UNIX Using Linux Third Edition
CSCI 130 Chapter 2. Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition Comments.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Computer Science 210 Computer Organization Introduction to C.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
chap13 Chapter 13 Programming in the Large.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Chapter 6: User-Defined Functions
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Computer Programming I Hour 2 - Writing Your First C Program.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
CSCI 171 Presentation 6 Functions and Variable Scope.
DOCUMENTATION SECTION GLOBAL DECLARATION SECTION
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Chapter 6: User-Defined Functions I
Computer Science 210 Computer Organization
Topic Pre-processor cout To output a message.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
C Programming Tutorial – Part I
Introduction to C Topics Compilation Using the gcc Compiler
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Computer Science 210 Computer Organization
Lesson #6 Modular Programming and Functions.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions Inputs Output
Chapter 4 void Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
Introduction to Classes and Objects
Lesson #6 Modular Programming and Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
SPL – PS1 Introduction to C++.
Presentation transcript:

Introduction to Programming Using C Modularity

2 Contents Modularity Functions Preprocessor Comments Global variables

3 Modularity As programs get bigger, they get harder to manage This happens with most things and we have developed ways to handle it – Too many things – put them in baskets – Need to play several songs – put them in a playlist – Got a tough problem – break it into a series of simpler problems

4 Modularity The C language lets us split our code into parts called functions A function is one way to create a module A module is simply a part of a larger program A module – Encapsulates part of a program – Can be invoked many times

5 Functions A function – Is a block of code – Has a name – Has a series of parameters which can be passed to it – Can return a value – Can be invoked by other code

6 The Function piTimes A simple function to multiply a number times  double piTimes(double n) { return n * ; } Function nameParameter list Type returned Function Body Indicates value to return

7 Functions Every function has – A name which is used to invoke it and must be unique – A parameter list of values passed to the function, which might be empty – A return type, indicating the type of value returned – A body which performs some calculation using the values from the parameter list and returns a result

8 An Interest Calculator We want to calculate interest – On a give amount of money – At a specific interest rate – For a given number of years We will show – How to write a function to do this – How to invoke this function

9 An Interest Calculator double interest(double principal, double rate, int time) { int i; for(i = 1; i <= time; i = i + 1) principal = principal * (1 + rate / 100); return principal; }

10 An Interest Calculator Points of interest – 3 parameters are passed to the function – A local variable, i, is declared within the function – A loop is used to calculate the result – The result is returned via the return statement

11 Invoking the Calculator main() { double start, end, rate; int years; printf(“Enter amount to invest or 0 to stop\n”); scanf(“%lf”, &start); while(start > 0) { printf(“Enter rate\n”); scanf(“%lf”, &rate); printf(“Enter years to invest\n”); scanf(“%d”, &years); end = interest(start, rate, years); printf(“Your investment will be worth %.1lf\n”, end); printf(“Enter amount to invest or 0 to stop\n”); scanf(“%lf”, &start); }

12 Compilation The C language uses a one-pass compiler – This means that it reads the code once only from start to finish – If you call a function before it sees the function, it will say the function does not exist We can overcome this problem by – Using forward declarations for every function in the program at the start of the program

13 Forward Declarations Forward declarations – Use the prototype of the function to tell the compiler The name of the function The types of the parameters for the function The return type of the function A prototype is – The function header without the body of the function

14 Forward Declarations The forward declaration for the interest function would be – double interest(double principal, double rate, int time); – This is simply the function header without the body – Notice the semi-colon at the end of the line * See functiondemo.c

15 The Preprocessor Before a C program is compiled it is run through a preprocessor The preprocessor – Looks for directives starting with # and Textually includes other files Remembers the definitions of macros Conditionally includes or excludes code from compilation – Expands defined macros to their real values

16 The Preprocessor C Source File C Preprocessor Modified C Source File C Compiler Object File Linker Library Executable File

17 The #include Directive The first preprocessor directive we will examine is the #include directive It has the format – #include Or – #include “filename” These both textuall include another file into your program, replacing the directive

18 The #include Directive The first form – #include – Searches for the file name on the include path – Think of it as searching in the system libraries for the file name you specified The second form – #include “filename” – Searches for the file relative to the current directory

19 The #include Directive Both printf and scanf are just regular functions Have you wondered how the compiler knows about them? We include their prototypes from what is called a header file – #include This file contains the prototypes of many common input and output functions

20 Comments Comments are notes inserted into programs to explain to humans what the code is doing Comments are enclosed between /* and */ – /* this is a comment */ Comments can cover several lines Comments are removed by the preprocessor and are never seen by the compiler It is good practice to comment your programs

21 Typical Program Layout Comment describing program and author #include directives for standard functions Prototypes of functions other than main() Definition of the main() function Defintion of remaining functions with a comment for each

22 Sample Program /*****************************************************************/ /* Display table of investment profits at different rates. */ /*****************************************************************/ #include double interest(double principal, double rate, int time); main() { double percent; printf(“Comparison of 5 year returns at different rates\n”); printf(“ Rate Profit per thousand $\n”); printf(“ \n”); for(percept = 3.5; percent < 9.6; percent = percent + 0.5) printf(“ %.2lf %.2lf\n”, percent, interest(1000.0, percent, 5) – 1000); } /*************************************************************************/ /* Calculate value of principal after time years at rate percent */ /*************************************************************************/ double interest(double principal, double rate, int time) { int i; for(i = 1; i <= time; i = i + 1) principal = principal * (1 + rate / 100); return principal; }

23 Main main is a function, but … – It is the first function called in your program – It is called by the operating system – It can communicate with the operating system – It has a default return type and optional parameter list The full prototype for main is – int main(char *argv[]) – Technically, we should return 0 from main – We won’t be using it to communicate with the O/S in this course

24 Global Variables We can declare variables in several places – At the top of the file, outside any function This is a global variable, visible everywhere – At the start of a function This is a local variable, visible only within the function – Within a set of curly brackets inside a function This is a variable visible only within the curly brackets in which it is declared

25 Global Variables The scope of a variable is the amount of code which can see the variable Global variables – Can be seen by any code in the file Local variables – Can be see throughout a function Block variables – Can be see throughout the block in which they are declared

26 Global Variables Many students think global variables are handy because – You don’t have to pass parameters because they are visible in every function – You only declare them once However, this is not the case because – It is very hard to find the functions which modify the global variables – This makes it Harder to understand how the program works Harder to find mistakes in the program