Programming Concepts and Database

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

Computer Science 2 Data Structures V section 2 Recitation 1.
Lesson 4: Formatting Input Data for Arithmetic
9b. Column/Bar Charts CSCI N207 Data Analysis Using Spreadsheet Department of Computer and Information Science, IUPUI Lingma Acheson
Introduction to Python
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 1.
THE BIG PICTURE. How does JavaScript interact with the browser?
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Hardware vs Software Hardware: A physical part of the computer which you can touch. Software: A set of instructions which is run to perform tasks on your.
ITBP 119 Algorithms and Problem Solving Section 2.1 Installing software Section 2.2 First Programs Section 2.3 Variables.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
9c. Line Charts CSCI N207 Data Analysis Using Spreadsheet Department of Computer and Information Science, IUPUI Lingma Acheson
Lesson 6. Python 3.3 Objectives. In this lesson students will learn how to output data to the screen and request input from the user. Students will also.
1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI.
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
CSCI-100 Introduction to Computing
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
2d – CheckBox Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
File Handling in QBASIC
Python Let’s get started!.
2e – RadioButtons Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
7. Data Import Export Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis Using Spreadsheets 1.
6b – Sub Procedure With Parameters Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
2c – Textboxes and Buttons Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Higher Computing Science 2016 Prelim Revision. Topics to revise Computational Constructs parameter passing (value and reference, formal and actual) sub-programs/routines,
5b – For Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Dayu Zhang 9/10/2014 Lab03. Outline Brief Review of the 4 Steps in Hello.cpp Example Understand endl and \n Understand Comment Programming Exercise -
Fundamental Programming Fundamental Programming Introduction to Functions.
9d. Pie Charts CSCI N207 Data Analysis Using Spreadsheet Department of Computer and Information Science, IUPUI Lingma Acheson
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Input, Output and Variables GCSE Computer Science – Python.
Whatcha doin'? Aims: To start using Python. To understand loops.
Multiplication
Python Let’s get started!.
Key Ideas from day 1 slides
Web Design II Flat Rock Community Schools
COMP 170 – Introduction to Object Oriented Programming
Implementing Functions from a Detailed Design Quick Tips
Multiplication
Computer Organization
Implementing Functions from a Detailed Design Quick Tips
Using local variable without initialization is an error.
Review.
Introduction to Programming
Learning to Program in Python
Coding Concepts (Sub- Programs)
Topics Introduction to Functions Defining and Calling a Void Function
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
G. Pullaiah College of Engineering and Technology
CSCI N207 Data Analysis Using Spreadsheet
Programming Concepts and Database
Functions continued.
CSCI N207 Data Analysis Using Spreadsheet
2g – ComboBox Lingma Acheson CSCI N331 VB .NET Programming
7 – Variables, Input and Output
4 – History of Programming Languages
6 – Miracle And “Hello World”
Introduction to Python
Basic Mr. Husch.
Hint idea 2 Split into shorter tasks like this.
Algorithms For use in Unit 2 Exam.
4a- If And Else Lingma Acheson CSCI N331 VB .NET Programming
4d – Program Design Lingma Acheson CSCI N331 VB .NET Programming
Intro to Programming (in JavaScript)
Programming Techniques
Python Creating a calculator.
Presentation transcript:

Programming Concepts and Database CSCI N201 Programming Concepts and Database 10 – Functions Lingma Acheson linglu@iupui.edu Department of Computer and Information Science, IUPUI

Task: Create a program called “UserFriendlyAddition” that does the following job: Greet the user Add two user inputs and output the result Say goodbye to the user

Algorithm Design Design your algorithm! Create a new program Greet the user Create a new variable, name Ask user to input his name, and store in the new variable Output “Hello, (user input)!” Add two integers Create three variables, n1, n2, n3 Ask user to input two integers, store in the two variables, and parse the user input to integers Add two integers and store the result in n3 Output “The result is “ + n3 Say goodbye to the user Output “Thanks for using this program. Good bye!” End the program

Use Miracle http://www.cs.iupui.edu/~aharris/MirJS.html

How to Structure Your Program Better? Function – a group of instructions that perform a specific task Each function has a name The group of instructions are executed by calling the function name

New Structure of the “UserFriendlyAddition” Algorithm Main program Create a new program greetUser(); addition(); goodbye(); End the main program Function greetUser() Create a new variable, name Ask user to input his name, and store in the new variable Output “Hello, (user input)!” Function addition() Create three variables, n1, n2, n3 Ask user to input two integers, store in the two variables, and parse the user input to integers Add the two integers and store the result in n3 Output “The result is “ + n3 Function goodbye() Output “Thanks for using this program. Good bye!”

Use Miracle Create a function Functions -> New Function Enter detailed task of the function Function -> End Function Code structure with functions function main(){ … }// end of main function functionName1(){ } // end of functionName1 function functionName2(){ } //end of functionName2

Lab Create a program that can add and multiply two integers. You program should have two functions add() and multiply(). Each function should take two user inputs, perform addition or multiplication on the two inputs, then output the result. Note: save the code into a file to be used for next lab!

Function with Parameters Task: Create a program that can add and multiply two integers. You program should have two functions add() and multiply(). User will be asked to input two integers. Each function will use the same user inputs, perform addition or multiplication on the two inputs, then output the result.

Function with Parameters Algorithm Main program Create a new program Create two variables, n1, n2, to be shared by two functions Ask user to input two integers, store in the two variables, and parse the user inputs to integers add() using n1 and n2; multiply() using n1 and n2; End the main program Function add() using n1 and n2 Create a variable, n3 n3 = n1 + n2; Output “The result of addition is “ + n3 Function multiply() using n1 and n2 n3 = n1 * n2; Output “The result of multiplication is “ + n3

Function with Parameters Question: How to use the same user input for two different functions? Answer: pass values into functions The variable(s) that holds the passing values are called “parameters”

Function with Parameters Code structure for functions with parameters function main(){ … add(n1, n2); //n1 and n2 are parameters of function “add” multiply(n1, n2); // n1 and n2 are parameters of function “multiply” }// end of main function add(value1, value2){ n3 = value1 + value2; } // end of add function multiply(value1, value2){ n3 = value1 * value2; } //end of multiply

Practice Use Miracle to make the code work!

Function that Return a Value Question: values can be passed into a function, can a function pass a value out? Answer: use a function that returns a value Task: Create a program that can add two integers. User will be asked to input two integers and store them in n1 and n2. The functions will perform the addition and pass the result out.

Function that Return a Value Code Structure function main(){ … var n1 = 0; var n2 = 0; var n3 = 0; n3 = add(n1, n2); alert(“The addition result is: “ + n3); } function add(value1, value2){ var n4=0; n4 = value1 + value2; return n4;

Practice Create a function that returns a value.

Lab Create two functions that both returns a value. n1 and n2 will be used to store user inputs and passed into the two functions. n3 will be created to store the returned value, and will be shared by the two functions. Hint: value of n3 can be replaced by a new value.

Global Variables Question about previous code: Can functions share variables without having to pass the value? Answer: using global variables Global Variable – a variable that is created outside of the main() function a variable that can be used directly by all functions include main()

Global Variables Code structure var n1 = 0; var n2 = 0; var n3 = 0; function main(){ … add(); //no need to pass n1 and n2 into the function alert(“The result for addition is: “ +n3); //use n3 directly }// end of main function add(){ n3 = n1+ n2; //use n1, n2 and n3 directly } // end of add

Lab Create a program that illustrates the idea of the previous slide.

Global Variable vs. Local Variable var n1 = 2; //global variable var n2 = 4; //global variable var n3 = 0; //global variable function main(){ var n4 = 0; //local variable var n3 = 1; //local variable add(); //no need to pass n1 and n2 into the function alert(“n3 in main is: “ +n3); //look for local variable first, output is 1 }// end of main function add(){ n3 = n1+ n2; //will change the global variable n3 alert(“n3 in add is: “ +n3); //use the global variable n3, output is 6 alert(“n4 is: “ +n4); //ERROR! n4 is local to main(), cannot be //found here } // end of add

Global Variables Use with caution – Too many global variables cause confusion. You might not know the value of a global variable is changed somewhere in a function.