Download presentation
Presentation is loading. Please wait.
1
Subprograms Parameters
2
Objectives Learn about parameters used within code blocks
3
What is a Parameter A parameter is a local, temporary variable that stores a COPY of the data given to it when its code block is called to be executed We see parameters inside brackets when we call a code block E.g. System.out.println(String text); println is a code block that requires one parameter, a String of text. We could use a variable or a literal text value: E.g. System.out.println(“Hello World!”); E.g. Math.pow(double base, double exponent); pow is a code block that requires two parameters, a base and exponent that are both double values
4
Go on… A parameter is a piece of data required for that code block to do its job println and sqrt would not be able to do their task without that given data Do all code blocks need parameters? No, many do not E.g. input.nextLine() Notice we still need the brackets
5
What if? What if you call a code block but do not give it the needed data? The program will not compile, it will be a syntax error or it will crash while running What if the data is given but in the wrong order? This depends, if the swapped values are the same type you may just end up with bad results E.g. Math.pow(2,3) gives 8, but Math.pow(3,2) gives 9 However, if the swapped values were of different types, like a String and an int the program will crash while running when it gets to that point in the code.
6
Conclusion When a code block requests data for a parameter it is like a contract between the programmer and the code block The code block promises to do its task perfectly, but ONLY if the programmer gives all the data in the expected order REMEMBER: The data given to the parameter when the code block is called is a COPY of the original data, so if the code block changes the parameter’s value, the original data remains UNCHANGED.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.