FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message ****...... ** Welcome.

Slides:



Advertisements
Similar presentations
Functions Prototypes, parameter passing, return values, activation frams.
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Functions, Interfaces, Parameters 8.1 – 8.2. include using namespace std; int main ( ) { int cal, fatGrams, fatCal; float fatPercent; cout
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Introduction to C Programming CE
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
C++ Control Flow Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th -22 nd Sept 2006.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
Chapter 5 Functions For All Subtasks. Void functions Do not return a value. Keyword void is used as the return type in the function prototype to show.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function.
1 CS161 Introduction to Computer Science Topic #10.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Cosc175/module.ppt1 Introduction to Modularity Function/procedures void/value-returning Arguments/parameters Formal arguments/actual arguments Pass by.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Instructor - C. BoyleFall Semester
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
Week 12 Methods for passing actual parameters to formal parameters.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
User-Defined Functions (cont’d) - Reference Parameters.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Computer Skills2 for Scientific Colleges
CSE 220 – C Programming Pointers.
EPSII 59:006 Spring 2004.
Chapter 5 Function Basics
Programming Fundamentals Lecture #7 Functions
Pointer.
Programming fundamentals 2 Chapter 2:Pointer
CS 1430: Programming in C++.
User-defined Functions
Extra.
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
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.
User-defined Functions
CS150 Introduction to Computer Science 1
Counting Loops.
Pointers & Functions.
Array and Method.
5.1 Introduction Pointers Powerful, but difficult to master
Simulating Reference Parameters in C
Function “Inputs and Outputs”
The Function Prototype
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
CS150 Introduction to Computer Science 1
Fundamental Programming
Pointers & Functions.
Presentation transcript:

FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome Home! **** **

int main() { Print2lines(); cout << “ Welcome Home!” << endl; Print4lines(); return 0; } where the 1 st function is void Print2lines() { cout << “**** **” << endl; } The compiler needs to know that Print2lines and Print4lines are VOID functions with no parameter lists. Therefore, PROTOTYPING declarations are needed before they are called in ‘main’. After #include put void Print2lines(); // prototypes void Print4lines();

A better solution is to use one function that prints a variable number of lines indicated in the parameter list. #include void Printlines(int); // function prototype int main() { Printlines(2); cout << “ Welcome Home!” << endl; Printlines(4); return 0; } void Printlines(int numlines) { int count; for (count = 1; count <= numlines; count++) cout << “**** **” << endl; }

Next Day Problem cin >> month >> day >> year; cout << “The day following” << month << ‘/’ << day << ‘/’ << year << “is”; UPDATE DATE cout << month << ‘/’ << day << ‘/’ << year; example The day following 5/1/1966 is 5/2/1966 Convert month to letters 5May

void writemonth (int) ; void writemonth (int month) { switch (month) { case 1: cout << “January”; break; case 2: cout << “Februrary”; break; : case 12: cout << “December”; break; }

The final output statement would be: cout << writemonth(month) << “ “ day << “, “ << year;

Value Parameters Used when the only role of the parameter(s) is to carry value(s) into a FUNCTION void writemeters(int feet, int inches) { int lengthins; float meters; lengthins = 12 * feet + inches; meters = lengthins / 39.39; cout << setw(6) << setprecision(2) << meters; } feet and inches are VALUE parameters Possible calls are writemeters(6, 4); writemeters(ft, ins); or ft+2, ins-1

Reference Parameters Used to carry value(s) into and back from a FUNCTION ex: to swap or exchange the values of two variables void swap(int& a, int& b) { int temp; // temporary variable temp = a; a = b; b = temp; } Possible calls swap(x, y); swap(m, n); where the actual parameters must be integer variables and must have already been assigned values

Parameter Passing e.g. writemeters(ft, ins) void writemeters(int feet, int inches) swap(x, y) void swap(int& a, int& b) Pass by VALUE A copy of the value of the parameter is passed into the FUNCTION Values in the calling FUNCTION are not changed Pass by REFERENCE (Address) The address of the parameter is passed into the FUNCTION All changes made inside the FUNCTION are permanent (i.e. values of x and y)

Parameter Passing Mechanisms void calc(int& p1, float p2); (in main) size = 3; distance = 4.5; calc(size, distance); ACTUAL PARAMETERS matched to FORMAL PARAMETERS on a 1 to 1 basis Note: Type and Order must be the same MAIN FUNCTION CALLED FUNCTION 3 calc(size, distance) void calc(int& p1, float p2)

Function Declarations and Calls void TryThis(int, int&, float); int main() { int int1; int int2; float someFloat; TryThis(int1, int2, someFloat); } void TryThis(int param1, int& param2, float param3) { int i; float x; ( statements involving “params” ) }....