Function Defaults C++ permits functions to be declared with default values for some, or all, of its parameters Allows for the function to be called without.

Slides:



Advertisements
Similar presentations
1 Pointers and Strings Section 5.4, , Lecture 12.
Advertisements

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)
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Functions Prototypes, parameter passing, return values, activation frams.
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.
Functions:Passing Parameters by Value Programming.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
Function Part II: Some ‘advanced’ concepts on functions.
Announcements Final Exam:. Review Passing Arrays as Parameters Pass Array Name into Function int intArray[100]; func(intArray); Accept into Function as.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
Python Functions.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
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.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
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.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
Motivation and Overview
Command Line Arguments
Chapter 5 Function Basics
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
A Lecture for the c++ Course
Chapter 5 Function Basics
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Pointer.
group work #hifiTeam
User-defined Functions
Variables with Memory Diagram
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.
Name: Rubaisha Rajpoot
6 Chapter Functions.
Functions.
User-defined Functions
CS150 Introduction to Computer Science 1
Pointers & Functions.
Anatomy of a Function Part 1
More ‘concepts’ on Function
Code::Block vs Visual C++
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
COMS 261 Computer Science I
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
CMSC 202 Lesson 6 Functions II.
CS1201: Programming Language 2
Anatomy of a Function Part 1
Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 2042)
Class rational part2.
More ‘concepts’ on Function
Announcements Exam 2 Lecture Grades Posted on blackboard
Presentation transcript:

Function Defaults C++ permits functions to be declared with default values for some, or all, of its parameters Allows for the function to be called without an argument for the “defaulted” parameter The parameter is then assigned the default value

Example: Function with defaults int sum(int i=2, int j=3); main() { int x=4, y=5, z; z= sum(x,y); z= sum(x); z= sum(); } int sum(int i, int j) { return i+j; Function is defined with default values for for both inputs i and j Defaults appear where the function is defined The function can then be called with or without arguments for the parameters with defaults defined If an argument is missing, the default value gets passed to the function

Defaulting “Rules” Any parameter may be given a default “Defaulted” parameters can be of any type Any number of parameters may be given a default value i.e. defaults can be limited to a subset of parameters Default parameters are defined when the function is declared Usually in the prototype (unless there isn’t one)

Default parameter order A subset of parameters may be assigned defaults, but… All “defaulted” parameters must appear last in the parameter list “Non-defaulted” parameters must be first and there can be no gaps! Examples: func(int a, int b, int c=5); // valid func(int a, int b=10, int c); // invalid !!!! “Non-defaulted” parameter (c) appears after parameter with a default value (b)

Invocation Valid calls: Invalid: When a function containing default parameters is called, defaults are assigned in reverse order – last to first. Parameters with no default assignment must be called with an argument All default assignments are based on parameter order, not type! Example: func(int a, string b=“foo”, int c=5, int d=10); What values do a, b, c, and d get in the valid calls shown to the right? Valid calls: int x=3; func(x); func(x,”bar”); func(x,”bar”,6); func(x,”bar”,6,7); Invalid: func(); func(x,6,7); func(6,6,”bar”);

Some wrinkles… The definitions to the right are valid #include <iostream> using namespace std; int sum(int i=2, int =3); int sum1(int i=6, int j=7) { return i+j; } main() { int x=4, y=5, z; z= sum(x,y); cout << z << endl; z= sum(x, 6); z= sum(); z= sum1(); int sum(int ii, int jj) { return ii+jj; The definitions to the right are valid Note the following: Variable name not given Don’t need the variable name in the prototype (not really “used”) Variable names don’t match No prototype, function declared and defined all at once