Functions prototypes arguments overloading return values part I.

Slides:



Advertisements
Similar presentations
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Advertisements

Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Chapter 6: User-Defined Functions I
Chapter 6: Functions.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Functions g g Data Flow g Scope local global part 4 part 4.
Functions Structured Programming 256 Chapter 6 Functions g prototypes g arguments g overloading g return values part I.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
1 Functions Chapter 7 2 Hope you can function! What is R2D2 doing here? What is his function? Who is Nibble? Can he function? IS he a function? Who is.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CPS120: Introduction to Computer Science Functions.
Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function.
Functions Pass by reference, or Call by reference Passing addresses Use of & part 3.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Functions g prototypes g arguments g overloading g return values part I Re-read Section 1.2.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
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.
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.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
1 Functions Part 1 Prototypes Arguments Overloading Return values.
Functions + Overloading + Scope
Chapter 9: Value-Returning Functions
User-Written Functions
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Function Topic 4.
CO1401 Programming Design and Implementation
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
User Defined Functions
FUNCTION CSC128.
CS150 Introduction to Computer Science 1
Chapter 6: User-Defined Functions I
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
Functions Imran Rashid CTO at ManiWeber Technologies.
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

Functions prototypes arguments overloading return values part I

Functions are self-contained blocks of code, the inner workings of which are invisible to the remainder of the program. are subprograms in C++ . perform a specific task. can act on data and return a value. Every C++ program has at least one function: main(). * * * *

Functions Why use functions? make programs easier to write, debug and maintain - divide and conquer! Two main types of functions: predefined -- found in the header files user-defined -- today’s topic *

Function - an example { int var1=1, var2=2, var3=3, var4=4; function1(“ASU”, var1); some statements; function2(var4, var3); function3(var2); }

Function - an example void function1(char name, int place) { cout << name << “ is #” << place << endl; } void function2(int al, int mel) { cout <<“var3 x var4 = “ << mel / al<<endl; } void function3(int casey) { cout << casey << “ is the value in var2\n”; }

Function properties may be called may be passed data called arguments may return a value to the calling program will not change the data stored in a received variable unless specifically instructed to do so

Functions 1. #include <iostream.h> 2. void demofunction(void); 3. void main(void) 4. { 5. cout << "In main\n"; 6. demofunction(); 7. cout << "Back in main\n"; 8. } 9. void demofunction(void) 10. { 11. cout << "In DemoFunction\n"; 12. cout << “Still in function.\n”; 13. }

Function Output Line # - 12.) Still in function. * * * * 5. In main 6. (calls function - 11.) In Demo Function - 12.) Still in function. 7. Back in main * * * *

Function declaration double Pythagorus( double, double ); data types only

Function Syntax Syntax { statements function body } * function header line function header { statements function body } *

Function Header Syntax type function_name(parameters) no ; Example double Pythagorus(double a, double b)

{ Function Definition Example double Pythagorus(double a, double b) { type var Example double Pythagorus(double a, double b) { double c; c = sqrt(a*a + b*b); return c; } no ; * *

Function Call void main(void) { cout << “The hypotenuse is “ << Pythagorus(12, 5); } OUTPUT The hypotenuse is 13

Program Structure #include <iostream.h> function prototypes; { void main(void) { variable declarations; statements [including function calls] } function definition(s)

Function Prototypes Syntax return type function_name(type); Example double Pythagorus(double, double); *

Function Prototypes Examples void do_stuff(void); double Pythagorus(double, double); void do_stuff(void); double times-em(int, int, int, int); double myfunc(double, int); void print_em(char, double);

Function Calls Syntax function_name(arguments); Example Pythagorus(3.0, 4.0); * *

Function Calls find_max(firstnum, secnum); memory find_max( , ) 865 9090 secnum memory get firstnum get secnum find_max( , ) find_max(firstnum, secnum); 865 9090 * * * * * *

Function Calls answer = Pythagorus(3.0,4.0); cout << “The hypotenuse = “ << answer << endl; cout << “The hypotenuse = “ << Pythagorus(3.0,4.0)<<endl; *

Function Calls answer = Pythagorus(3.0,4.0); answer = answer * 100; cout << “The hypotenuse = “ << answer << endl; cout << “Hypotenuse = “ << Pythagorus(3.0,4.0) * 100 <<endl; *

Program Structure #include <iostream.h> function prototypes; { void main(void) { variable declarations; statements [including function calls] } function definition(s)

Program Structure #include <iostream.h> square prototype main function square call cube call square function cube function #include <iostream.h> square prototype cube prototype

Program Structure . . . int square(int); // function prototype int cube(int); // or function declaration void main(void) { int x = 8; cout <<“The square is “<< square(x) <<‘\n’; cout <<“The cube is “ << cube(x) <<endl; . . . } int square(int n) // function definition { continued on next slide

{OR return n*n; {OR return n*n*n; Program Structure int square(int n) // function definition { int answer; answer = n*n; return answer; } int cube(int n) // function definition answer = n*n*n; {OR return n*n; {OR return n*n*n; *

Function Summary Prototype type function_name(parameter types); double Pythagorus(double, double); Call function_name(actual parameters); Pythagorus(height, base); Definition formal type function_name(parameter types & names) { } double Pythagorus(double a, double b) * * * * *

Function Overloading Two or more distinct functions may have the same name. The data types of the arguments in the function calls must match those in the prototypes and in the definitions. The same function is given multiple definitions or implementations. The correct one is chosen by the compiler, not the programmer. * * *

Function Overloading The functions must differ in their parameter lists. The type and/or number of parameters must be different. Examples double myFunction(int, int, int); int myFunction(double, int, int); int myFunction (double, double); void myFunction(double); *

{ } Function Overloading . myFunction(3,4,5); myFunction(3.0, 4.0); // a is used // c is used // b is used // d is used a double myFunction(int, int, int) b int myFunction(double, int, int) c int myFunction (double, double) d void myFunction(double) } Header * * * *

Returning Values A function can receive many values Only one value can be directly returned

Returning Values The return statement: tells the function which value to send back to the calling program terminates the function call and returns immediately to the calling program

Return Statement Syntax return expression; Examples return c; return hypotenuse;

Return Statement int find_max(int x, int y) { int maximum; if (x >= y) maximum = x; else maximum = y; return maximum; } same data type *

Passing Data passing by value gives a single value passing by reference may give back several values accomplished by using references (this topic) using pointers *

Passing Data - by Value passing by value: A copy of a value is passed from the calling function to the called function. double Pythagorus(double a, double b) { double c; c = sqrt(a*a + b*b); return c; } double Pythagorus(double a, double b) { a = a * a; b = b * b; double c = sqrt(a*a + b*b); return c; } * *

Storing Values into Parameters call to find_max find_max(firstnum, secnum); value in first_num is passed value in sec_num is passed find_max(x, y) x 865 9090 y arguments * *

Passing Data - by Value * * void main(void) { double height = 4.0, base = 3.0; double Pythagorus(double, double); cout << “Hypotenuse = “ << Pythagorus(height, base)<<endl; . . . } double Pythagorus(double a, double b) { double c; c = sqrt(a*a + b*b); return c; 4.0 3.0 * *

Passing Data - by Value * double Pythagorus(double a, double b) 4.0 3.0 double Pythagorus(double a, double b) { double c; a++; b++; c = sqrt(a*a + b*b); return c; } back in main: cout << height; cout << base: *

Passing Data - by Value void print_val(int); // function prototype void main(void) { int w = 3; cout <<"w before the function call is "<<w<<‘\n’; print_val(w); cout <<"w after the function call is "<<w<<‘\n’; } void print_val(int q) { cout<<"Value passed to the function is "<<q<<endl; q = q *2; // doubles the value cout<<"Value at the end of the function is "<< q <<endl;

Passing Data - by Value Output Value passed to the function is 3 w before the function call 3 Value passed to the function is 3 Value at the end of the function is 6 w after the function call is 3

Passing Data - by Reference Syntax double Pythagorus(double &, double &); Pythagorus(height, base); double Pythagorus(double& a, double& b) function prototype function call function definition

Passing Data - by Reference void main(void) { double height = 4.0, base = 3.0; double Pythagorus(double &, double &); cout << “Hypotenuse = “ << Pythagorus(height, base) << endl; . . . } double Pythagorus(double& a, double& b) { double c; c = sqrt(a*a + b*b); return c; address of height address of base * *

Passing Data - by Reference address of height address of base double Pythagorus(double& a, double& b) { double c; a++; b++; c = sqrt(a*a + b*b); return c; } back in main: cout << height; cout << base: *

Passing Data - by Reference In main() values referenced as 1 value stored a height 1 value stored b base In Pythagorus() values referenced as *

Passing Data - by Reference { float a, b, c, sum, product; void calc(float, float, float, float &, float &); // prototype cout << "Enter three numbers: "; cin >> a >> b >> c; calc(a, b, c, sum, product); // call cout << a<<“ + “<<b<<“ + “c<<“ = " << sum; cout << ‘\n’<<a<<“ * “<<b<<“ * “c<<“ = " << product; } void calc(float x, float y, float z, float &tot, float& multiply) { tot = x + y + z; // definition multiply = x * y * z; x++; y++; z--; // for demo purposes

Passing Data - by Reference Output Enter three numbers: 5 7 9 5 + 7 + 9 = 21 5 * 7 * 9 = 315 x is 6, y is 8, z is 8 tot and sum refer to the same address product and multiply refer to the same address *

Passing Data - by Reference void main(void) { int w = 3; void print_val(int &); // function prototype cout <<"w before the function call is "<<w<<‘\n’; print_val(w); cout <<"w after the function call is "<<w<<‘\n’; } void print_val(int& q) { cout<<"Value passed to the function is "<<q<<endl; q = q *2; // doubles the value cout<<"Value at the end of the function is "<< q <<endl;

Passing Data - by Reference Output w before the function call 3 Value passed to the function is 3 Value at the end of the function is 6 w after the function call is 6

Swap Routine void swap(float& num1, float& num2) { float temp; temp = num1; num1 = num2; num2 = temp; }

Data Type Mismatch value parameters implicit type conversion - value of the actual parameter is coerced to the data type of the formal parameter reference parameters no coercion because an address is passed, not a value * *

A Comparison formal actual parameter is parameter may be value variable, constant, or expression type coercion may take place reference variable only of exact same type as formal

What’s Happening???? call sequence 1. memory is allocated 2. parameters are passed 3. transfer of control return sequence 1. value of the return is stored 2. memory is deallocated 3. transfer of control * *

“Louis Pasteur’s theory of germs is ridiculous fiction.” Pierre Pachet End Note “Louis Pasteur’s theory of germs is ridiculous fiction.” Pierre Pachet Professor of Physiology Toulouse, 1872 Copyright © 1997 by Freedom TLC, Inc.

Function Practice - I: wholepart( ) accepts a double returns the integer part of any fraction Hint: Assign the passed argument to an integer variable. Ex. 123.4567 returns 123 the calling function displays the result Bronson p. 240-10

Function Practice - I: fracpart( ) accepts a double returns the fractional part of any number passed to it. Ex. 123.4567 returns .4567 the calling function displays the result Bronson p. 240-11

Function Practice - I: find_abs( ) accepts a double computes its absolute value returns the value calling function displays the result Bronson p. 240-5

Function Practice - I: mult( ) accepts two doubles multiplies them returns the result calling function displays the result Bronson p. 240-5

Function Practice - I: powfun( ) accepts two integers raises the first to the power of the second the returned value is a long integer the calling function displays the result Bronson p. 240-6

Function Practice - I: tempvert( ) accepts a double converts C to F or F to C returns the converted temperature the calling function asks the direction of conversion the temperature to convert displays the result Bronson p. 240-8 IN IN double tempvert(double in_temp, char type) { if (type == 'c' || type == 'C') return( (5.0/9.0) * (in_temp - 32.0) ); else //if (type == 'f' || type == 'F') return ((9.0/5.0) * in_temp + 32.0); }