Methods and Data Passing

Slides:



Advertisements
Similar presentations
What is output line of the following C++ code? Please trace int i = 1, QP; DATA: 4, B, 3, A double credit, totCredit=0.0; double num = 0.0; string LG;
Advertisements

Functions Prototypes, parameter passing, return values, activation frams.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Programming Functions: Passing Parameters by Reference.
Computer Science 1620 Loops.
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.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
Functions:Passing Parameters by Value Programming.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Programming Functions: Passing Parameters by Reference.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
GE 211 Function in C++ Dr. Ahmed Telba Math Library Functions Defining a Function: The general form of a C++ function definition is as follows:
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
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.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Functions Jordi Cortadella Department of Computer Science.
LESSON 2 Basic of C++.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
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.
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
Looping Examples I.
LESSON 2 Basic of C++.
Chapter 5 Function Basics
Functions and an Introduction to Recursion
Chapter 5 Function Basics
Chapter 5 Functions.
Passing Arguments to a Function
Pointers Psst… over there.
לולאות קרן כליף.
Programming fundamentals 2 Chapter 2:Function
Pointers Psst… over there.
CSC1201: Programming Language 2
Random Number Generation
פונקציות לעיתים קרובות לא נוח להגדיר את כל התוכנה בתוך גוף אחד.
Chapter 5 Function Basics
While Loop Design ENGI 1020 Fall 2018.
Value returning Functions
Chapter 5 Function Basics
הרצאה 03 אבני היסוד של תוכנית ב- C
Counting Loops.
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Functions Pass By Value Pass by Reference
Pass by Reference.
Methods and Data Passing
Functions and an Introduction to Recursion
Methods and Data Passing
Methods and Data Passing
CHAPTER 2 Arrays and Vectors.
Methods and Data Passing
CSC1201: Programming Language 2
CHAPTER 2 Arrays and Vectors.
Using string type variables
Pointers & Functions.
CS1201: Programming Language 2
TOPIC: FUNCTION OVERLOADING
Programming Strings.
C. M. Overstreet Old Dominion University Fall 2007
Methods and Data Passing
CSE Module 1 A Programming Primer
Presentation transcript:

Methods and Data Passing Module 5 Methods and Data Passing 12/13/2019 CSE 1321 Module 5

Why have functions? CREATE average, userNum1, userNum2 PRINT (“Please enter the 2 numbers”) READ userNum1 READ userNum2 average = (userNum1 + userNum2) / 2 // a lot of other code // more code here, then

Header/Body Example // Header is top line int Sum(int num1, int num2) { // Begin the body int sum = 0; for(int i = num1; i <= num2; i++) { sum += i; } return sum; } // End the body 12/13/2019 CSE 1321 Module 4

Example Memory double average (int x, int y) { return ( (x+y) / 2.0); } int main() { int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); // 6 result2 = average (num3, num1); // 4.5 cout<< "Average1 is: "<<result1<< endl; cout<< "Average2 is: "<<result2; Memory num1 num2 num3 5 7 4 result1 result2 6 4.5

Another Quick Example (function falls asleep; main awake) int main () {   int num1, num2, num3;   num1 = 5; num2 = 7; num3 = 4;   printNum (num1); //passing data } Output 5 Memory num1 num2 num3 5 7 4 void printNum (int myNum) { cout<<"Your number is "<<myNum; }

Psuedocode - A Bad Solution (where is the repeated code?) sum ← 0 FOR (i ← 1 i <= 10 i ← i+1) sum ← sum + i ENDFOR PRINT("Sum from 1 to 10 is “, sum) FOR (int i ← 20 i <= 30 i ← i+1) PRINT("Sum from 20 to 30 is “, sum) for (int i ← 35 i <= 45 i ← i+1) PRINT("Sum from 35 to 45 is “, sum) 12/13/2019 CSE 1321 Module 5

METHOD MAIN BEGIN CREATE result ← SUM(arguments: 1,10) PRINT("Sum from 1 to 10 is:”, result) result ← SUM(arguments: 20,30) PRINT("Sum from 20 to 30 is:”, result) result ← SUM(arguments: 35,45) PRINT("Sum from 35 to 45 is:”, result) END MAIN METHOD SUM(parameters: num1, num2) BEGIN CREATE sum ← 0 FOR (i ← num1, i <= num2, i ← i+1 ) sum ← sum + i ENDFOR RETURN sum END SUM Ps 12/13/2019 CSE 1321 Module 5

C++ Example – Method Sum int Sum(int num1, int num2) { int sum = 0; for(int i = num1; i <= num2; i++) sum += i; } return sum; int main() int result = Sum(1, 10); cout<<"Sum from 1 to 10 is " << result<<endl; result = Sum(20, 30); cout<<"Sum from 20 to 30 is " << result<<endl; result = Sum(35, 45); cout<<"Sum from 35 to 45 is " << result<<endl; 12/13/2019 CSE 1321 Module 5

Psuedocode - Method Max METHOD MAIN BEGIN CREATE i ← 5 CREATE j ← 2 CREATE k ← max(i, j) PRINT("The maximum of ", i , " and ” , j ," is ", k) END MAIN METHOD MAX(parameters: num1, num2) BEGIN CREATE result if (num1 > num2) result ← num1 else result ← num2 RETURN result END MAX 9 12/13/2019 CSE 1321 Module 5

C++ – Method Max int main() { int i = 16, j = 2; int k = max(i, j); cout<<"The maximum of "<<i<<" and "<<j<<" is "<< k; } int max(int num1, int num2) { int result; if (num1 > num2) result = num1;} else{ result = num2; return result; } 12/13/2019 CSE 1321 Module 5

#include <iostream> #include<string> using namespace std; int math(int x, int y) { return x+y; } int math (int x) { return x*x; double math(double x, double y) { return x*y; string math (){ return "Why does this work?"; int main() { cout<<math(7.5, 9.5)<<endl; // Prints 71.25 cout<<math(4)<<endl; // Prints 16 cout<<math()<<endl; // Prints ”Why does this work?” 12/13/2019 CSE 1321 Module 4