CS150 Introduction to Computer Science 1

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Functions:Passing Parameters by Value Programming.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Programmer-defined functions Development of simple functions using value parameters.
#include using namespace std; void main() { cout
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
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&
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 CS161 Introduction to Computer Science Topic #10.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
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 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.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
User-Defined Functions (cont’d) - Reference Parameters.
13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29,  Tutor will be in the lab on Tuesday evening,
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Dr. Shady Yehia Elmashad
Function Topic 4.
Chapter 5 Function Basics
Functions and an Introduction to Recursion
CO1401 Programming Design and Implementation
School of EECS, Peking University
Arrays Part-1 Armen Keshishian.
C++ Arrays.
לולאות קרן כליף.
Function Basics.
Chapter 5 Function Basics
Value returning Functions
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.
Pointers & Functions.
CS150 Introduction to Computer Science 1
Pass by Reference.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Functions and an Introduction to Recursion
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
Arithmetic Operations
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.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
COMS 261 Computer Science I
CS150 Introduction to Computer Science 1
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
Introduction to Functions
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
Programming Fundamental
Presentation transcript:

CS150 Introduction to Computer Science 1 Summary Exam: Friday, October 17, 2003. Last time we talked about functions: Implement top down design Determine major steps of program. Write functions to implement each step. Program is more modular--each step is clearly defined. Details of steps are hidden . A function consists of: Function Prototype Function Definition Function Call 12/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example #include <iostream> char isEven(int); int getVal(); int main() { using namespace std; int num; while ((num = getVal()) != -999) if (isEven(num) == ‘E’) cout << “EVEN” << endl; else cout << “ODD” << endl; } return 0; 12/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example (cont.) char isEven(int number) { const char even = ‘E’; const char odd = ‘O’; char answer; if (number % 2 == 0) answer = even; else answer = odd; return answer; } 12/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example (cont.) int getVal() { int number; cout << “Enter an integer number: “; cin >> number; return number; } 12/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Program Write a program that calculates the area of a circle Write a program that calculates the area of a circle using three functions, getradius, calcarea and outputarea. 12/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Program Write a program that draws the following stick figure using functions and top down design. * * * * * / \ / \ / \ ------- 12/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Program Write a function that takes as input two numbers and returns the larger Write a function that takes as input two numbers and returns the smaller Write a function that computes the average given a sum and number of elements 12/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example #include <iostream.h> void larger(int, int, int&); int main() { int num1, num2, large; cin >> num1 >> num2; larger(num1, num2, large); cout << “Larger number is “ << large << endl; } void larger(int num1, int num2, int & large) if (num1 > num2) large = num1; else large = num2; 12/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Passing Arguments Pass by value Values are passed into the function Any changes made in the function are not returned Pass by reference Any changes made in the function are returned 12/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example #include <iostream> void swap(int &, int &); int main() { int i, j; cin >> i >> j; swap(i,j); cout << i << j; return 0; } void swap(int & num1, int & num2) int temp; temp = num1; num1 = num2; num2 = temp; 12/2/2018 CS150 Introduction to Computer Science 1

Rules for Parameter Lists There must be the same number of actual and formal arguments The correspondence between actual and formal arguments is by position only Corresponding actual and formal arguments must match in type The names of the actual and formal arguments may be the same or different For reference arguments only, the actual argument must be a single, simple variable 12/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example Given the following function prototype: void checkit(float &, float &, int, int, char &); And declarations in main: float x, y; int m; char next; Which are legal? checkit(x, y, m+3, 10, next); checkit(m, x, 30, 10, ’c’); checkit(x, y, m, 10); checkit(35.0, y, m, 12, next); checkit(x, y, m, m, c); 12/2/2018 CS150 Introduction to Computer Science 1