Function 2 (L17) * Function Prototype * Promotion Rules * Data Type * Library Header File * Customer Header File * Case Study * Exercise/Home Work Dr.

Slides:



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

Functions Prototypes, parameter passing, return values, activation frams.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Overview creating your own functions calling your own functions.
Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Basic Elements of C++ Chapter 2.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
1 Lecture 3 Part 1 Functions with math and randomness.
For Repetition Structures (L13) * General Form of the for Statement * Components of a Typical for Header * Pre/Postincrement of the Counter * Stream Manipulator.
Review 2 - Chapter 3 and 4 Function 1 Function 2 Function 3 Function 4
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
Functions Why we use functions C library functions Creating our own functions.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
CMSC 1041 Functions II Functions that return a value.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
Hello World Using C++ (L03) * Introduction C++ * Programming Style * Hello World Using C++ Hollow World Using C/C++ Dr. Ming Zhang.
Selection Control Structures 2 (L09) * Nested if Statements * The if-else Chain * Exercise: if, if-else, nested if, and if-else chain Selection Control.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
Chapter Topics The Basics of a C++ Program Data Types
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Dr. Shady Yehia Elmashad
Functions prototypes arguments overloading return values part I.
Functions, Part 2 of 2 Topics Functions That Return a Value
Basic Elements of C++.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Dr. Shady Yehia Elmashad
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Basic Elements of C++ Chapter 2.
Dr. Shady Yehia Elmashad
توابع در C++ قسمت اول اصول كامپيوتر 1.
Value returning Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

Function 2 (L17) * Function Prototype * Promotion Rules * Data Type * Library Header File * Customer Header File * Case Study * Exercise/Home Work Dr. Ming Zhang

Function Prototype A function prototype tells the compiler * the name of the function, * the type of data returned by the function, * the number of parameters the function expects to receive, * the types of the parameters, and * the order in which these parameters are expected. Dr. Ming Zhang

Example 1 of Function Prototype int maximum( int, int, int); * the name of the function: maximum * the type of data returned by the function: int * the number of parameters the function expects to receive: 3 * the types of the parameters: int * the order in which these parameters are expected: (int, int, int) Dr. Ming Zhang

Example 2 of Function Prototype float minimum( int, int, int, float); * the name of the function: minimum * the type of data returned by the function: float * the number of parameters the function expects to receive: 4 * the types of the parameters: int and float * the order in which these parameters are expected: (int, int, int, float) Dr. Ming Zhang

Promotion Rules * The promotion rules apply to expressions containing values of two or more data types (mixed-type expression). * The type of each value in a mixed-type expression is promoted to the highest type in the expression ( Actually a temporary version of each value is created and used for the expression - the original values remain unchanged). Dr. Ming Zhang

Common Use of Promotion * A common use of promotion is when the type of an argument to a function does not match the parameter type specified in the function definition. * The type of an argument to a function is promoted to the highest parameter type specified in the function definition. Dr. Ming Zhang

Date Type (from highest to lowest ) Data Type Minimum Maximum Long double system defined double system defined float system defined unsigned long int long int unsigned int int unsigned short int short int unsigned char char Dr. Ming Zhang

Library Header Files * Each Standard library has a corresponding header file containing the function prototypes for all the functions in the library and definition of various data types and constants needed by those functions. * The header files ending in “.h” are “old-style’ header files. * The new-style header files do not use “.h”. Dr. Ming Zhang

Examples of Old-Style Header Files * Contains the floating-point size limits of the system. New version is. * Contains function prototypes for math library functions. New version is * Contains function prototypes for the standard input/output library functions and information used by them. New version is ……….. Dr. Ming Zhang

Examples of New-Style Header Files * Contains classes and functions used by the standard library to allocated memory to the standard library containers. * Contains the definition of class string from the standard library. * Contains a class for defining the numerical data type limits on each computer platform. …………….. Dr. Ming Zhang

Custom Header Filers * The programmer can create custom header files. * Programmer-defined header files should end in “.h”. * A programmer-defined header file can be included by using the #include preprocessor. * Example #include “square.h” Dr. Ming Zhang

Fahrenheit & Celsius (1)- Case Study #include #define MAXCOUNT 4 using std:cout; using std::cin; using std::endl; int main( ) { int count, fahren; float tempvert(int); for(count =1; count <=MAXCOUNT, count++) { cout << “Enter a Fahrenheit temperature”<<endl; cin >> fahren; cout << “The Celsius equivalent is” << tempvert(fahren) << endl; } Dr. Ming Zhang

Fahrenheit & Celsius (2)- Case Study /* Convert Fahrenheit to Celsius*/ float tempvert(int intemp) { return( ( intemp - 32) * (5.0 /9.0) ); } Dr. Ming Zhang

Exercise/Home Work 1 For the following function headers, determine the number, type, and order (sequence) of the values that must be passed to the function: (1) void factorial( int n) (2) void price( int type, double yield, double maturity) (3) void yield(int type, double price, double maturity) (4) void interest(char flag, float price, float time) Dr. Ming Zhang

Exercise/Home Work 2 Write a function named find_abs that accepted a double-precision number passed to it, computes its absolute value, and display the absolute value. The absolute value of a number is the number itself is the number is positive, and the negative of the number it the number is negative. Dr. Ming Zhang

Exercise/Home Work 3 * A second-degree polynomial in x is given by the expression ax 2 + bx + c, where a, b, and c are know numbers and a is not equal to zero. Write a C++ function named poly_two(a, b, c, x) that computes and returns the value of a second-degree polynomial for any passed integer values of a, b, c, and x. And also write a C++ working program to input a, b, c, and x. And print out the result of polynomial function. Dr. Ming Zhang