Download presentation
Presentation is loading. Please wait.
1
Programming Concepts and Database
CSCI N201 Programming Concepts and Database 10 – Functions Lingma Acheson Department of Computer and Information Science, IUPUI
2
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
3
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
4
Use Miracle
5
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
6
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!”
7
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
8
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!
9
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.
10
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
11
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”
12
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
13
Practice Use Miracle to make the code work!
14
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.
15
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;
16
Practice Create a function that returns a value.
17
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.
18
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()
19
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
20
Lab Create a program that illustrates the idea of the previous slide.
21
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
22
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.