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.

Slides:



Advertisements
Similar presentations
1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva.
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
Starting Out with C++, 3 rd Edition 1 Chapter 5. Looping.
Writing and Testing Programs Drivers and Stubs Supplement to text.
CS Sept 2006 Pick ups from chapters 4 and 5.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 1400 Pick ups from chapters 4 and 5. if-else-if Chained if statements can be useful for menus… Enter savings plan choice: (A) economy rate (B) saver.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Functions:Passing Parameters by Value Programming.
CS 1400 Pick ups from chapters 4 and 5. if-else-if Chained if statements can be useful for menus… Enter savings plan choice: (A) economy rate (B) saver.
Function Part II: Some ‘advanced’ concepts on functions.
Chapter 5: Control Structures II (Repetition)
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Functions g g Data Flow g Scope local global part 4 part 4.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 4.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
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.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
4.3 Functions. Functions Last class we talked about the idea and organization of a function. Today we talk about how to program them.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Fundamental Programming Fundamental Programming Introduction to Functions.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Chapter 4 Repetition Structures
Review 1.
C++ Programming: CS150 For.
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Controlling execution - iteration
CS150 Introduction to Computer Science 1
More ‘concepts’ on Function
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Statements and flow control
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.
Fundamental Programming
CS150 Introduction to Computer Science 1
More ‘concepts’ on Function
Presentation transcript:

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 header? 3.At will the output of the following program be if the user enters 10?

#include using namespace std; void func1() { cout << “Able was I.” << endl; } void func2() { cout << “I saw Elba.” << endl; } int main() { int input; cout << “Enter a number:” << endl; cin >> input; if (input < 10) { func1(); func2(); } else { func2(); func1(); } return 0; }

4) To qualify for a credit card at a certain bank you need: 1.To have been working at your current job for at least two years. And 2.To make at least $50,000. The following code skeleton determines if a person qualifies for a credit card. There are two functions that are unfinished 1.qualify() and 2.noQualify(); Write the definition of these two methods qualify should explain that the applicant qualifies and that the annual interest is 12% noQualify should explain that the applicant does not qualify and give a general explanation why.

#include using namespace std; int main() { float salary; int years; cout << “This program will determine if you qualify” << endl; cout << “for our credit card.” << endl; cout << “What is your annual salary?” << endl; cin >> salary; cout << “How many years have you worked at your current job?” << endl; cin >> years; if (salary >= 50,000 && years >= 2) qualify(); else noQualify(); return 0; }

5) Which of the following is the function prototype, the function header, and the function call. void showNum(float num) void showNum(float); showNum(45.67); 6)Write a function called timesTen that takes an int parameter and displays the number times times ten on the screen.

7)Write a function prototype for the timesTen() function. 8) What is the output of the following program? #include using namespace std; void showDouble(int); int main() { int num; for (num=0; num<10; num++) showDouble(num); return 0; } void showDouble(int value) { cout << value << “\t” << (value * 2) << endl; }

9) What is the output of the following program? #include using namespace std; void func1(float, int); int main() { int x = 0; float y = 1.5; cout << x << “ “ << y << endl; func1(y,x); cout << x << “ “ << y << endl; return 0; } void func1(float a, int b) { cout << a << “ ” << b << endl; a = 0.0; b=10; cout << a << “ ” << b << endl; }

10)How many return values may a function have? 11) Write a header for a function named distance. The function should return a float and have two float parameters, rate and time. 12) Write a header for a function named days. The function should return an int and have three int parameters, years, months (with a default value of 12) and weeks. 13) Write a header for a function called getKey. The function should return a char and use no parameters. 14) Whats the difference between a static local variable and a global variable?

15) What is the output of the following program? #include using namespace std; void myFunc(); int main() { int var = 100; cout << var << endl; myFunc(); cout << var << endl; return 0; } void myFunc() { int var = 50; cout << var << endl; }

16) What is the output of the following program? #include using namespace std; Void showVar(); int main() { for (int count=0; count < 10; count++) showVar(); return 0; } void showVar() { static int var = 10; cout << var << endl; var++; }

17) Write the prototype and header for a function called compute. The function should have three parameters: an int a float and a long (not necessarily in that order). The int parameter should have a default value of 5, and the long parameter should have a default value of The float parameter should not have a default value.

18)What is the output of the following code? #include using namespace std; void test(int=2, int=4, int=6); int main() { test(); test(6); test(3,9); test(1,5,7); return 0; } void test(int first, int second, int third) { first +=3; second+=6; third+=9; cout << first << “ “ << second << “ “ << third << endl; }

Looping Review C++ has the following looping looping constructs. for loop – This is good when you know the exact number of iterations ahead of time. while loop – a pretest loop. This is good for cases where you don’t want the loop to run if the condition is false from the beginning. do-while loop – a posttest loop. This is good for cases where you want the loop always to run the first time.

for loop for (int i=0; i<100; i++) { cout << i*i << endl; } You can also do this: int i=0; for (i=0; i<100; i++) { cout << i*i << endl; }

for loop You can also do this: int i=0; for (; i<100; i++) { cout << i*i << endl; } You can also do this: for (int i=0; i<100; i++) cout << i*i << endl;

Nested for loops for (int i=0; i<100; i++) { for (int j=0; j<10; j++) { cout << i*j << endl; }

while loop int i=0; while (i<10) { cout << i << endl; i++; }

while loop int i=0; while (i>0) { cout << i << endl; i++; }

do-while loop int i=0; do { cout << i << endl; i++; } while (i>0);

do-while loop int i=-1; do { cout << i << endl; i++; } while (i>0);