Value returning Functions

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Chapter 6: User-Defined Functions I
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
User-Defined Functions (cont’d) - Reference Parameters.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
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 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 1.2 Introduction to C++ Programming
Popping Items Off a Stack Using a Function Lesson xx
Chapter 6: User-Defined Functions I
Dr. Shady Yehia Elmashad
Two-Dimensional Arrays Lesson xx
Objectives Identify the built-in data types in C++
CO1401 Programming Design and Implementation
Variables A piece of memory set aside to store data
CSC113: Computer Programming (Theory = 03, Lab = 01)
Dr. Shady Yehia Elmashad
User-Defined Functions
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
void Pointers Lesson xx
Dr. Shady Yehia Elmashad
Arrays & Functions Lesson xx
Structures Lesson xx In this module, we’ll introduce you to structures.
توابع در C++ قسمت اول اصول كامپيوتر 1.
Returning Structures Lesson xx
CSC1201: Programming Language 2
One-Dimensional Array Introduction Lesson xx
File I/O with Records Lesson xx
Passing Structures Lesson xx
Chapter 5 Function Basics
Popping Items Off a Stack Lesson xx
Functions Pass By Value Pass by Reference
CS150 Introduction to Computer Science 1
Chapter 6: User-Defined Functions I
Fundamental Programming
CSC1201: Programming Language 2
Presentation transcript:

Value returning Functions Lesson xx In this module, we’ll talk about functions that return a value.

Objectives Review void returning functions Write a program that returns a value. We plan to do the following this module: 1) Review void returning functions 2) Write a program that demonstrates value returning functions.

void Returning Function void prtStar ( int num) { int i; cout << endl; for (i = 0; i < num; i++) cout << "*"; } This is the prtStar() function from the previous module which prints num # of*s. In other words, if num is 11 this code prints 11 *s. If num is 66, 66 *s will be printed. Let’s examine the header, we have void prtStar(int num). The keyword void means that the function returns no value. There are times when you want the function to compute a value and send it back to the calling module. This is called a value returning function.

Syntax of a Value Returning Function return type function name ( arguments) { //body } This is the syntax of a value returning function. You need a header and the body of the code goes inside a set of {}s. The header has 3 parts: the return type, the function name and the arguments inside a set of ()s.

Example of a Value Returning Function Return type Function name Arguments int computeXvalue (float a, char c) { int x; //body return x; } Here is an example of how you write a value returning function. Our function name is computeXvalue. The function takes 2 arguments, the 1st is a float and the 2nd is a char. When the function is done, it returns an int back to the calling module. Just a note here, the last statement of the function is return x; the data type of x is an int and that is what you use for the return type. If the data type of x is a long, you put long for the return type of the function. Enough talking, let’s look at an example in real life.

Program Definition Write a program that: Reads in a minimum and maximum value in main( ). Send minimum and maximum into a function as arguments. The function should generate a random number between minimum and maximum. Return the random number back to main() where it is printed Let’s write the following program to demonstrate value returning functions: 1) Read in a minimum and maximum value in main( ). 2) Send minimum and maximum into a function as arguments. 3) The function should generate a random number between minimum and maximum. 4) Return the random number back to main() where it is printed

Program Output Here is the output to this program that uses a value returning function.

Code int randomnum ( int min, int max ) { int numbtween; #include<iostream> using std::endl; using std::cin; using std::cout; int main() { int randomnum ( int min, int max ); int small, big, rn; cout << "Enter 2 #s & I will generate a “; cout <<“random # from min to max "; cin >> small >> big; rn = randomnum ( small, big); cout << "The number in between is " << rn << endl; return 0; } int randomnum ( int min, int max ) { int numbtween; int diff = max - min;   numbtween = rand () % diff + min; return numbtween; } Here is the entire program using a value returning function. Let’s go through the code now.

Function Prototype #include<iostream> using std::endl; using std::cin; using std::cout; int main() { int randomnum ( int min, int max ); int small, big, rn; cout << "Enter 2 #s & I will generate a “; cout <<“random # from min to max "; cin >> small >> big; rn = randomnum ( small, big); cout << "The number in between is " << rn << endl; return 0; } Let’s start from the beginning and look at the code in main( ). If we are going to call a function, we need a function declaration which is highlighted in blue. Notice that in this example, the prototype is inside of main( ) whereas in the previous modules, it was outside of main( ). You can write the prototype in either location. If you write it outside of main( ), it is a global declaration. Inside of main ( ) it is a local declaration

Declaration #include<iostream> using std::endl; using std::cin; using std::cout; int main() { int randomnum ( int min, int max ); int small, big, rn; cout << "Enter 2 #s & I will generate a “; cout <<“random # from min to max "; cin >> small >> big; rn = randomnum ( small, big); cout << "The number in between is " << rn << endl; return 0; } The code in red is the declaration of our variables. small is used to store the smaller #, and big is used to store the larger #. rn is where we store the random # generated by the function.

Input #include<iostream> using std::endl; using std::cin; using std::cout; int main() { int randomnum ( int min, int max ); int small, big, rn; cout << "Enter 2 #s & I will generate a “; cout <<“random # from min to max "; cin >> small >> big; rn = randomnum ( small, big); cout << "The number in between is " << rn << endl; return 0; } This code in red prompts the user for 2 #s and reads them into the variable small and big.

Function Call #include<iostream> using std::endl; using std::cin; using std::cout; int main() { int randomnum ( int min, int max ); int small, big, rn; cout << "Enter 2 #s & I will generate a “; cout <<“random # from min to max "; cin >> small >> big; rn = randomnum ( small, big); cout << "The number in between is " << rn << endl; return 0; } rn = randomnum (small, big ); is how you call the function. This is how you interpret the statement: call the randomnum function and send in small and big as arguments. When the function returns the random number will be stored in rn. The cout statement prints out the random number.

Value Returning Function rn = randomnum ( small, big); int randomnum ( int min, int max ) { int numbtween; int diff = max - min;   numbtween = rand () % diff + min; return numbtween; } Here is the code for the randomnum( ) function. The code in red is the call to the function from main( ). The blue arrows show you what happens when the function is called. The contents of small is stored in the local variable min and the # in big is stored in max. The statements in the function are executed. When the computer encounters the statement: return numbtween; the value in the variable numbtween is transferred back to main () into the variable rn.

randomnum( ) Function int randomnum ( int min, int max ) { int numbtween; int diff = max - min;   numbtween = rand () % diff + min; return numbtween; } Now that you know the mechanics of how a function works, let’s look at the code in the randomnum( ) function. The 1st line of the function declares a local variable called numbtween. We use this variable to store the random number between min and max. The 2nd line of the function subtracts min from max and stores the result in diff. diff represents the number of different random numbers from min to max. Here is an example, suppose min = 3 and max = 7 , the random numbers that are possible between 3 and 7 are 3,4,5,6 and there are 4 of them.

Built-in rand ( ) Function int randomnum ( int min, int max ) { int numbtween; int diff = max - min;   numbtween = rand () % diff + min; return numbtween; } The built-in rand() function generates a random number from 0-32,767. In our program, we want a random # from min to max. By doing the calculations rand () % diff + min, we can get a random # in the correct range. We store this number in numbtween and in the last line of the function we return this value back to main. There we have it, a function that returns a value.

Summary Review void returning functions Write a program that returns a value In this module, we reviewed void retuning function and then went on to show you value returning functions. In the next module, we’ll do some more work with functions. We’ll talk about pass by value and pass by reference.