USING & CREATING FUNCTIONS. MODULAR PROGRAMMING  Why Modular Programming?  Improves Readability & Understandability  Improve Maintainability  Allows.

Slides:



Advertisements
Similar presentations
Chapter 6: User-Defined Functions I
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 Algorithms Basic Control Structures Comparisons and if (…) statement What is a function? Math Library Functions Character Functions Reading Sample Programs.
Numeric Types, Expressions, and Output ROBERT REAVES.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
1 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 6: User-Defined Functions I
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
A function is a subprogram that acts on data and often returns a value. You are already familiar with the one function that every C++ program possesses:
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
1 Chapter 9 Scope, Lifetime, and More on Functions.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Lecture 5 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
STANDARD FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
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.
Scis.regis.edu ● CS-361: Control Structures Week 2 Dr. Jesús Borrego Lead Faculty, COS Regis University 1.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 Chapter 9 Scope, Lifetime, and More on Functions.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn about.
Recursion It is a process of calling the same function itself again and again until some condition is satisfied. Syntax: func1() { ……….. func1(); }
CHAPTER 6 USER-DEFINED FUNCTIONS I
Simple C Programs.
Operations Making Things Happen.
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 9: Value-Returning Functions
Chapter 6: User-Defined Functions I
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Chapter 6: User-Defined Functions I
FUNCTIONS EXAMPLES.
CSCI 161: Introduction to Programming Function
Math Library and IO formatting
User-Defined Functions
Copyright © 2012 Pearson Education, Inc.
Lecture 2B Expressions Richard Gesick
Formatted and Unformatted Input/Output Functions
Chapter 9 Scope, Lifetime, and More on Functions
User Defined Functions
Functions October 23, 2017.
FUNCTION CSC128.
Chapter 6: User-Defined Functions I
Fundamental Programming
Output Formatting Bina Ramamurthy 4/16/2019 BR.
Functions Imran Rashid CTO at ManiWeber Technologies.
CPS125.
Output Formatting Bina Ramamurthy 9/25/2019 BR.
Presentation transcript:

USING & CREATING FUNCTIONS

MODULAR PROGRAMMING  Why Modular Programming?  Improves Readability & Understandability  Improve Maintainability  Allows For Re-Usability  Within Program  From Program To Program  Top-Down Approach  Break Task Down Into Smaller & Smaller Processes  Stop When A Process Will Not Break Down Further  Write Functions For Lowest Level Processes

PREDEFINED FUNCTIONS  Similar To Calculator Functions  Input Value (Parameter)  Press Function Key (Call Function)  Read Result (Use Function Output)  Functions Organized In Libraries  # include  Appendix F Shows Various Libraries And Functions

PREDEFINED FUNCTIONS  Example math (cmath) functions  ceil(x) - returns the smallest whole number value that is not less than x  abs(x) - returns the absolute value of integer x  exp(x) - returns the natural logarithm of x  floor(x) - returns the largest whole number value that is not greater than x  sin(x) - returns the sine of x  sqrt(x) - returns the square root of x  cos(x) - returns the cosine of x. Note: In all of the above cmath functions, the input paramter, x, is defined as a floating point value.

PREDEFINED FUNCTIONS  Example character (cctype) functions  isalnum(ch) - returns true if the char is an ASCII alphabetic or digit character ('a' - 'z', 'A' - 'Z', or '0' - '9')  char(int) - returns the ASCII character associated with the given number  isdigit(ch) - returns true if the char is an ASCII digit ('0' - '9')  isspace(ch) - returns true if the char is a whitespace character (blank, newline, tab, return)  tolower(ch) - returns the lowercase ASCII equivalent of the given char if it is an uppercase character. Otherwise returns the given char unchanged.  toupper(ch) - returns the uppercase ASCII equivalent of the given char if it is a lowercase character. Otherwise returns the given char unchanged.

PREDEFINED FUNCTIONS  Four Ways To Use Functions  Direct Output cout << “The value “ << x << “ raised to the power of “ << y; cout << “ is “ << pow(x,y) << endl;  Assignment Statement temp = pow(x,y); z = z + pow(x,y);  Function Call Parameter temp = pow(x,pow(y,z));  Relational Expression if (pow(x,y) > 15)

USER DEFINED FUNCTIONS  Three Categories  Void Function Without Parameters  Void Function With Parameters  Value-Returning Function (with or without parameters)  Void Function  May or May Not Have Parameters  Does Not Return A Value ( unless reference parameters are used )  Value-Returning Function  Typically Has Parameters  Parameters Declared In Function Header  Returns Single Value Of Function Return Type

USER DEFINED FUNCTIONS  Void Function Without Parameters functionType functionName() { // Function May Have Local Constants // Function May Have Local Variables statement(s); }  Void Function Call functionName();

USER DEFINED FUNCTIONS  Void Function With Parameters void functionName(formal parameter list) { // Function May Have Local Constants // Function May Have Local Variables statement(s) }  Void Function Call functionName(actual parameter list)

USER DEFINED FUNCTIONS  Void Function With Parameters (cont.)  Formal Parameter List (in function header) (datatype variable, datatype variable)  Actual Parameter List (in function call) (expression or variable, expression or variable)

USER DEFINED FUNCTIONS  Value Returning Function return type functionName(formal parameter list) { // Function May Have Local Constants // Function May Have Local Variables statement(s) return variable; }  Function Call functionName(actual parameter list);

USER DEFINED FUNCTIONS  Value Returning Function (cont.)  Formal Parameter List (in function header) (datatype variable, datatype variable)  Actual Parameter List (in function call) (expression or variable, expression or variable)  Return Statement Used To Pass Value Back To Function Call

USER DEFINED FUNCTIONS  Where Are They Located In Program?  Can Be Listed Before Or After main()  Most Programmers List After main()  If Listed Before main() Creates Issues  Function Prototypes Eliminates Issues  Function Prototypes Listed Prior To main() functionType functionName(parameter list);  Parameter List Variable Names Are Optional double larger(double x, double y); or double larger(double, double);