Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.

Slides:



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

Introduction to C Programming
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
C Programming Getting Started Getting Started Hello World Hello World Data types and variables Data types and variables main() main() I/O routines I/O.
Procedural programming in Java
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
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.
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© 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.
CS 201 Functions Debzani Deb.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Chapter 6: Functions.
Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
Procedural programming in Java Methods, parameters and return values.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
Week 8: Decisions Bryan Burlingame 21 October 2015.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Lecture 10: Peripherals Bryan Burlingame 04 November 2015.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CISC105 – General Computer Science Class 2 – 6/7/2006.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Programming Fundamentals Enumerations and Functions.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
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.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Lecture 7: Variable Scope B Burlingame March 16, 2016.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Lecture 7: Modular Programming (functions) B Burlingame 05 October, 2016.
Functions + Overloading + Scope
User-Written Functions
Chapter 7: User-Defined Functions II
Lecture 8: Variable Scope & Working with Files
Lecture 7: Modular Programming (functions)
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
In C Programming Language
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CPS125.
Presentation transcript:

Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015

Learning Objectives Introduce more C Discuss the importance of style in code Explain the concept of modular program design Explain the concept of a function in C Explain why functions are important in programming Explain the structure of a function  Return data type  Parameters Apply the concept of a function to a practical problem

Announcements Homework #5 due in two weeks Midterm in lab this week

Compound assignments C has many assignment operators which perform an operation and then store the results value += 5;  Same as value = value + 5; value *= 10;  value = value * 5;

All compound assignments += (addition) -= (subtraction) *= (multiplication) /= (division) %= (modulo division) &= (bitwise and) |= (bitwise or) ^= (bitwise xor) <<= (left shift) >>= (right shift)

Increment and decrement value++; similar to value = value + 1, increments after returning value ++value; similar to value = value + 1, increments before returning value value--; similar to value = value – 1; --value;

Programming Style The layout of program directives using white space, syntax, & punctuation to visually highlight a programmer’s intention Good Style is  Consistent – A given structure always looks the same  Illustrative – The intent of the code is shown by the grammar  Clear – Any reasonable programmer should be able to follow the intent

Programming Style Why does it matter?  Reduces bugs – the human eye is excellent at detecting patterns  Reduces development time  Allows others to understand how to approach your code  Style is a fundamental part of code documentation

#include // Programmer: B. Burlingame // Program: Good_form.c double square( double x ); // Version: 1.0 // int main( void ) // Prints the area of a square { double side = 0.0; // Stores the length of the side double area = 0.0; // Stores the area of the square char buffer[100] = ""; // Input buffer printf( "Enter the side of a square: " ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%lf", &side ); // Obtain the length of a side area = square( side ); // Call square function printf( "The area is %.3lf.\n", area ); // Display the area to the screen return 0; // Return 0 to the OS } ////////////////////////////////////////////////////////////////////////////// double square( double x ) // Return x squared { return( x * x ); }

#include double square( double x ); int main( void ) { double side = 0.0, area = 0.0; char buffer[100] = ""; printf( "Enter the side of a square: " ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%lf", &side ); area = square( side ); printf( "The area is %.3lf.\n", area ); return 0; } double square( double x ) { return( x * x ); }

Modular Programming Break a large problem into smaller pieces  Smaller pieces sometimes called ‘modules’ or ‘subroutines’ or ‘procedures’ or functions  Why? Helps manage complexity  Smaller blocks of code  Easier to read Encourages re-use of code  Within a particular program or across different programs Allows independent development of code Provides a layer of ‘abstraction’ a = sqrt(9.0);

Functions The ‘building blocks’ of a C program  You’ve used predefined functions already: main() printf(), sscanf(), pow()  User-defined functions Your own code In combination with predefined functions

Functions - Mathematical View X Function Returned value

Flowchart notation

Functions - Definition Structure Function 'header'  Return data type (if any)  Name Descriptive  Arguments (or parameter list) Notice: data type and name Statements  Variable declaration  Operations  Return value (if any) type function_name (type arg1, type arg2 ) { statements; } double product(double x, double y) { double result; result = x * y; return result; } A function that calculates the product of two numbers

Functions - Example Function prototype  Like a variable declaration Tells compiler that the function will be defined later Helps detect program errors Note semicolon!! Function definition  See previous slide  Note, NO semicolon Function return  return statement terminates execution of the current function  Control returns to the calling function  if return expression; then value of expression is returned as the value of the function call Only one value can be returned this way Function call  main() is the 'calling function'  product() is the 'called function'  Control transferred to the function code  Code in function definition is executed #include /* function prototype */ double product(double x, double y); int main() { double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }

Function - Practice 1 Write a function named 'sum'  sums two integers  returns the sum 2 min. on your own Share with neighbor Steps 1.Function header return data type function name argument list with data types 2.Statements in function definition variable declaration operations return value

Function - sum() int sum_int(int x, int y) { int result; result = x + y; return result; }

Passing Arguments into Functions How are the arguments passed into functions?  'Pass by value'  function arguments are expressions  In the function call: Expressions are evaluated and copies of their values are put into temporary memory locations The names of the corresponding parameters in the function definition are made to be the names of the copies  The values of the expressions in the function call are not changed #include double product(double x, double y); int main() { int a = 10; double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double A, double B) { double result; result = A * B; return result; }

Identifiers and Scope Identifier  The name of a variable, function, label, etc. int my_var1;/* a variable */ pow_table();/* a function */ start:/* a label */ Question:  Does it make a difference where in a program an identifier is declared? YES! --> concept of ‘scope’

Scope of Identifiers Scope of a declaration of an identifier  The region of the program that the declaration is active (i.e., can access the variable, function, label, etc.) Five types of scope:  Program (global scope)  File  Function prototype  Function  Block (“between the { } scope”)

Scope of Identifiers - Program (Global) Scope Program (global) scope  if declared outside of all functions  "Visible" to all functions from point of declaration  Visible to functions in other source files  Use only when necessary and then very carefully!!  ex. from Ch var_scope.c #include int a = 10; double product(double x, double y); int main() { double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }

Function scope  Applies only to labels start: * goto start;  Active from the beginning to the end of a function  Ex. Statement labels in a switch selection structure Scope of Identifiers - Function Scope #include int main() { int user_sel; /* prompt user for entry */ /* get user entry */ switch( user_sel ) { case 1: printf("\n message..."); /* call game function1 here */ break; case 2: printf("\n message..."); /* call game function2 here */ break; default: printf("Error"); break; }

Scope of Identifiers - Block Scope Block (local) scope  A block is a series of statements enclosed in braces { }  The identifier scope is active from the point of declaration to the end of the block ( } )  Nested blocks can both declare the same variable name and not interfere  ex. from Ch var_scope_block.c scope_nested_blocks.c #include double product(double x, double y); int main() { int a = 10; double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }

Storage Duration How long the identifier exists in memory Static storage class  Identifier exists when program execution begins For variables:  Storage allocated and variable is initialized once  Retains their values throughout the execution of the program #include void just_count(void); /* proto */ int main() { int i; for(i=0;i<10;i++) { just_count(); } return 0; } void just_count(void) { static int count_a; int count_b; count_a = count_a + 1; count_b = count_b + 1; printf("count_a== %d\n", count_a); printf("count_b== %d\n", count_b); } just_count.c

For functions:  function name exists when execution begins For variables with global scope:  i.e., declared outside of all functions and uses static keyword  "Visible" to all functions from point of declaration in this source file only  Keeps data ‘private’ to this file only Storage Duration, cont. #include static int a = 10; double product(double x, double y); int main() { double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }

References Modular Programming in C math.h /xsh/math.h.html /xsh/math.h.html e.html e.html