Introduction to Function in C++ Programming Language LeMoyne-Owen College Chen-Huei Chou University of Wisconsin-Milwaukee April 4, 2008.

Slides:



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

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Overview creating your own functions calling your own functions.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
Functions Computer Programming. 2 Top-down design using Functions A program was produced which entered the hours worked and an hourly rate of.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
CS102 Introduction to Computer Programming Chapter 6 Functions Continued.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function.
FUNCTIONS AND STRUCTURED PROGRAMMING CHAPTER 10. Introduction A c program is composed of at least one function definition, that is the main() function.
CPS120: Introduction to Computer Science Lecture 14 Functions.
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.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
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.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Fundamental Programming Fundamental Programming Introduction to Functions.
Lecture 20 Polymorphism. Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Function Topic 4.
Functions and an Introduction to Recursion
Variables A piece of memory set aside to store data
Function Basics.
User Defined Functions
Chapter 5 Function Basics
Value returning Functions
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
CS150 Introduction to Computer Science 1
Counting Loops.
Lec8.
CS150 Introduction to Computer Science 1
Functions and an Introduction to Recursion
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
CS1201: Programming Language 2
Introduction to Functions
CPS125.
Presentation transcript:

Introduction to Function in C++ Programming Language LeMoyne-Owen College Chen-Huei Chou University of Wisconsin-Milwaukee April 4, 2008

What is a function? A function is a subprogram that acts on data and often returns a value. Advantages of using functions  Program re-use  Structural programming  Team programming  Easy for maintenance and debugging Your most familiar function: main()

Main function int main(void) { statement; function1(); statement; function2(); statement; return 0; }

Function Definition function-type function-name( parameter- list ) { local-definitions; function-implementation; } function-type  with returned value: int, float, char  without returned value: void function-name: same rule of naming

Function Definition (cont.) parameter-list: formal parameters of the function together with their types local-definitions:  definitions of variables that are used in the function-implementation.  no meaning outside the function function-implementation:  executable statements that implement the effect of the function

Where to put function? Before main function void function1(void) { statements } void main() { function1(); }

Where to put function? (cont.) After main function with function prototype void function1(void); //prototype of function1 void main() { function1(); } void function1(void) { statements }

Types of Functions Functions with no parameters Functions with parameters and no return value Functions that return values

Functions with no parameters Of limited use Example: skip 3 lines #include void skipthree(void) // Function to skip three lines { cout << endl << endl << endl; } void main() { int....; float....; cout << "Title Line 1"; skipthree(); cout << "Title Line 2"; }

Functions with parameters and no return value Example: skip n lines void skip(int n) // Function skips n lines on output { int i; // a local variable to this function // now loop n times for (i = 0; i < n; i++) cout << endl; } void main() { int m = 6, n = 3; ; skip(m); ; skip(m + n); ; skip(4); ; }

Functions that return values Example: distance from the origin to p float distance(float x, float y) // Returns the distance of (x, y) from origin { float dist; //local variable dist = sqrt(x * x + y * y); return dist; } x y 0 p Sqrt(x 2 +y 2 )

Hands-on practice Please implement a program which can calculate the distance from origin by using function. Hint: float distance(float x, float y)

#include // Function prototypes float distance(float,float); void main() { float x,y,dist; // Test function distance(x,y) cout << "Testing function distance(x,y)" << endl; cout << "Enter values for x and y: "; cin >> x >> y; dist = distance(x,y); cout << "Distance of (" << x << ',' << y << ") from origin is " << dist << endl << "Tested" << endl; } // End of main

// Function Definitions float distance(float x, float y) // Returns the distance of (x, y) from origin { float dist; //local variable dist = sqrt(x * x + y * y); return dist; }

Any Question?