Functions g prototypes g arguments g overloading g return values part I Re-read Section 1.2.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
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.
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.
Chapter 6: User-Defined Functions I
Functions Modules in C++ are called functions and classes
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 Structured Programming 256 Chapter 6 Functions g prototypes g arguments g overloading g return values part I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
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.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
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.
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.
Chapter 7 Functions CS185/09 - Introduction to Programming Caldwell College.
1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL.
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.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
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 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.
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.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
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.
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.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
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.
Programming Fundamentals Enumerations and Functions.
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.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
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 Functions Part 1 Prototypes Arguments Overloading Return values.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Functions prototypes arguments overloading return values part I.
User-Defined Functions
Extra.
CSC1201: Programming Language 2
Chapter 6: User-Defined Functions I
CSC1201: Programming Language 2
Functions Imran Rashid CTO at ManiWeber Technologies.
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

Functions g prototypes g arguments g overloading g return values part I Re-read Section 1.2

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

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

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

Functions Why use functions? 1 make programs easier to write, debug and maintain - divide and conquer! * Two main types of functions: 1 predefined (library functions) found in the header files & the C++ library 1 user-defined – make your own, include it yourself

Functions - an example { int var1=1, var2=2, var3=3, var4=4; (‘A’, var1); //Call the function function1(‘A’, var1); //Call the function some statements; (var4, var3); //Call function2(var4, var3); //Call (var2); //Call function3(var2); //Call } Arguments to the functions

Function - an example void function1(char grade, int place) {cout << grade << “ is #” << place << endl; } void function2(int ml, int m2) {cout <<“var3 x var4 = “ << m1 * m2<<endl; } void function3(int Upick) {cout << Upick << “ is the value in var2\n”; }

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

Functions 1. #include using namespace std; 2. void demofunction(void); //prototype 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 Function Output 5. In main 6. (calls function )In Demo Function )Still in function. 7. Back in main Line # * *

Function declaration (prototype) double Pythagorus( double, double ); data types only semicolon Function identifier – its name

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

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

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

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

Program Structure #include 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); * Need

Function Prototypes Examples 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 Example Syntax function_name(arguments); Example PrintPayCheck(employeeID, Amount); hyp = Pythagorus(3.0,4.0); big = find_max(a,b); * Return from function assigned to variables

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

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; answer = answer * 100; cout << “The hypotenuse = “ << answer << endl; * * 100 cout << “Hypotenuse = “ << Pythagorus(3.0,4.0) * 100 <<endl;

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

Program Structure main function square call cube call square function cube function #include square prototype cube prototype

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

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

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

The challenge! Look at the Function Exercises following 6.1 Pages 238 and 239 Do any or all of Exercises 2, 4, or 6. Run and test your programs ONE of these will be part of your next programming assignment.

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 * *. // a is used // c is used // b is used // d is used myFunction(3,4,5); myFunction(3.0, 4.0); myFunction(11.1, 9, 2); myFunction(23.56); { callcall 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 return The return statement: 1 tells the function which value to send back to the calling program 1 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 1 passing by value gives a single value 1p1passing by reference may give back several values accomplished by using references (this topic) using pointers *

The NOT rule for functions The arguments in the function prototype, the function call, and the function header must agree in number, order, and type Remember this rule!

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

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

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; } Passing Data - by Value *

double Pythagorus(double a, double b) {double c; a++; b++; c = sqrt(a*a + b*b); return c; } Passing Data - by Value * 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 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

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