Chapter 5 Function Basics

Slides:



Advertisements
Similar presentations
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Function Part II: Some ‘advanced’ concepts on functions.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Programming Functions: Passing Parameters by Reference.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
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.
Programming in C++ 1. Learning Outcome  At the end of this slide, student able to:  Understand the usage of classes and functions.  Understand static.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 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.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 5 Functions Lecturer: Mrs Rohani Hassan.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Function Parameters and Overloading Version 1.0. Topics Call-by-value Call-by-reference Call-by-address Constant parameters Function overloading Default.
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Functions and an Introduction to Recursion
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 5 Functions DDC 2133 Programming II.
Chapter 5 Function Basics
Chapter 5 Functions.
Chapter 6 Methods 1.
Pointers and Pointer-Based Strings
Lecture 6 C++ Programming
Chapter 2 Elementary Programming
Extra.
Chapter 5 Function Basics
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
Group Status Project Status.
Chapter 5 Function Basics
Pointers & Functions.
More ‘concepts’ on Function
Chapter 4 Implementing Free Functions
Pass by Reference.
CS2011 Introduction to Programming I Methods (II)
Chapter 6 Methods.
Chapter 5 Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Functions and an Introduction to Recursion
Chapter 5 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Pointers and Pointer-Based Strings
CS150 Introduction to Computer Science 1
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
Unit-1 Introduction to Java
الكلية الجامعية للعلوم التطبيقية
More ‘concepts’ on Function
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Chapter 5 Function Basics

Call Stacks Each time a function is invoked, the system stores its arguments and variables in an area of memory, known as a stack. When a function calls another function, the caller’s stack space is kept intact, and new space is created to handle the new function call. When a function finishes its work and returns to its caller, its associated space is released.

Call Stacks

i is declared and initialized animation Trace Call Stack i is declared and initialized

j is declared and initialized animation Trace Call Stack j is declared and initialized

animation Trace Call Stack Declare k

animation Trace Call Stack Invoke max(i, j)

pass the values of i and j to num1 and num2 animation Trace Call Stack pass the values of i and j to num1 and num2

animation Trace Call Stack (num1 > num2) is true

animation Trace Call Stack Assign num1 to result

Return result and assign it to k animation Trace Call Stack Return result and assign it to k

Execute print statement animation Trace Call Stack Execute print statement

void Functions The preceding section gives an example of a value-returning function. This section shows how to declare and invoke a void function. A void function does not return a value. The following program declares a function named printGrade and invokes it to print the grade for a given score. TestVoidFunction.cpp

TestVoidFunction.cpp #include <iostream> using namespace std; /** Print grade for the score */ void printGrade(double score) { if (score < 0 || score > 100) { cout << "Invalid score"; return; } if (score >= 90.0) cout << 'A'; else if (score >= 80.0) cout << 'B'; else if (score >= 70.0) cout << 'C'; else if (score >= 60.0) cout << 'D'; else cout << 'F'; } TestVoidFunction.cpp

cout << "Enter a score: "; double score; cin >> score; int main() { cout << "Enter a score: "; double score; cin >> score; cout << "The grade is "; printGrade(score); return 0; } TestVoidFunction.cpp

Passing Parameters by Values When you invoke a function with a parameter, the value of the argument is passed to the parameter. This is referred to as pass-by-value. The variable is not affected, regardless of the changes made to the parameter inside the function.

Passing Parameters by Values #include <iostream> using namespace std; void increment(int n) { n++; cout << "n inside the function is " << n << endl; } int main() { int x = 1; cout << "Before the call, x is " << x << endl; increment(x); cout << "after the call, x is " << x << endl; system("PAUSE"); return 0; Increment.cpp

Passing Parameters by Values This program creates a function for swapping two variables. The values of the arguments are not changed after the function is invoked. #include <iostream> using namespace std; void swap(int n1, int n2) { cout << "\tInside the swap function" << endl; cout << "\t\tBefore swapping n1 is " << n1 << " n2 is " << n2 << endl; // Swap n1 with n2 int temp = n1; n1 = n2; n2 = temp; cout << "\t\tAfter swapping n1 is " << n1 << } TestPassByValue.cpp

Passing Parameters by Values int main() { // Declare and initialize variables int num1 = 1; int num2 = 2; cout << "Before invoking the swap function, num1 is " << num1 << " and num2 is " << num2 << endl; // Invoke the swap function to attempt to swap two variables swap(num1, num2); cout << "After invoking the swap function, num1 is " << num1 << " and num2 is " << num2 << endl; system("PAUSE"); return 0; } TestPassByValue.cpp

Passing Parameters by References Using pass-by-value , the values in original parameters are not changed. pass-by-reference shares the same value and hence the original value is actually changed. TestPassByReference.cpp

Passing Parameters by References #include <iostream> using namespace std; // Swap two variables void swap(int &n1, int &n2) { cout << "\tInside the swap function" << endl; cout << "\t\tBefore swapping n1 is " << n1 << " n2 is " << n2 << endl; // Swap n1 with n2 int temp = n1; n1 = n2; n2 = temp; cout << "\t\tAfter swapping n1 is " << n1 << } TestPassByReference.cpp

TestPassByReference.cpp int main() { // Declare and initialize variables int num1 = 1; int num2 = 2; cout << "Before invoking the swap function, num1 is " << num1 << " and num2 is " << num2 << endl; // Invoke the swap function to attempt to swap two variables swap(num1, num2); cout << "After invoking the swap function, num1 is " << num1 << " and num2 is " << num2 << endl; system("PAUSE"); return 0; }

Overloading Functions The max function that was used earlier works only with the int data type. But what if you need to find which of two floating- point numbers has the maximum value? The solution is to create another function with the same name, max, but different parameters. The following program creates three functions, the first finds the maximum integer, the second finds the maximum double, and the third finds the maximum among three double values. TestFunctionOverloading.cpp

Overloading Functions #include <iostream> using namespace std; /** Return the max between two int values */ int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; } /** Find the max between two double values */ double max(double num1, double num2) Overloading Functions TestFunctionOverloading.cpp

/** Return the max among three double values */ double max(double num1, double num2, double num3) { return max(max(num1, num2), num3); } int main() // Invoke the max function with int parameters cout << "The maximum between 3 and 4 is " << max(3, 4) << endl; // Invoke the max function with the double parameters cout << "The maximum between 3.0 and 5.4 is " << max(3.0, 5.4) << endl; // Invoke the max function with three double parameters cout << "The maximum between 3.0, 5.4, and 10.14 is " << max(3.0, 5.4, 10.14) << endl; system("PAUSE"); return 0; } TestFunctionOverloading.cpp

Ambiguous Invocation Sometimes there may be two or more possible matches for an invocation of a function, but the compiler cannot determine the most specific match. This is referred to as ambiguous invocation. Ambiguous invocation is a compilation error.

Ambiguous Invocation #include <iostream> using namespace std; int maxNumber(int num1, double num2) { if (num1 > num2) return num1; else return num2; } double maxNumber(double num1, int num2) int main() cout << maxNumber(1, 2) << endl; return 0;