Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: http://www.mu.ac.in/myweb_test/MCA study.

Similar presentations


Presentation on theme: "Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: http://www.mu.ac.in/myweb_test/MCA study."— Presentation transcript:

1 Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study material/

2 Data types type: A category or set of data values.
Constrains the operations that can be performed on data Many languages ask the programmer to specify types Examples: integer, real number, string ask them, how might the computer store "hi" using binary digits? (some kind of mapping; ASCII)

3

4 Variables variable: A piece of the computer's memory that is given a name and type, and can store a value. Like preset stations on a car stereo, or cell phone speed dial: Steps for using a variable: Declare it - state its name and type Initialize it - store a value into it Use it - print it or use it as part of an expression a variable is also like the MS / MR buttons on a calculator variables must be declared before they are used, just like methods 4

5 Variable Declaration C requires all the variables to be defined at the beginning of a scope. But c++ allows the declaration of variable anywhere in the scope. Variable Declaration example: float mynumber; int a, b, c;

6 Scope of Variable

7 Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration. The scope of local variables is limited to the block enclosed in braces ({}) where they are declared. For example, if they are declared at the beginning of the body of a function (like in function main) their scope is between its declaration point and the end of that function.

8 Dynamic Initialization of Variable
In c++, a variable can be initialized at run time using expressions at the place of declaration. This is refered to as dynamic initialization. Ex int m = 10; Reference variables A reference variable provides an alias for a previously defined variable. Ex: int sum = 200; int &total = sum;

9 FUNCTION

10 Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming can offer to us in C++. A function is a group of statements that is executed when it is called from some point of the program. Ex: type name ( parameter1, parameter2, ...) { statements…; } type : the data type specifier of the data returned by the function. name is the identifier by which it will be possible to call the function. parameters (as many as needed)

11 Default Values in Parameter
When declaring a function we can specify a default value for each parameter. This value will be used if the corresponding argument is left blank when calling to the function. To do that, we simply have to use the assignment operator and a value for the arguments in the function declaration Ex: int divide (int a, int b=2) { int r; r=a/b; return (r); }

12 Recursivity Recursivity is the property that functions have to be called by themselves. It is useful for many tasks, like sorting or calculate the factorial of numbers. Example: Factorial n! = n * (n-1) * (n-2) * (n-3) ... * 1 5! = 5 * 4 * 3 * 2 * 1 = 120 A recursive function to calculate Factorial in C++ : long factorial (long a) { if (a > 1) return (a * factorial (a-1)); else return (1); }

13 Overloaded Function In C++ two different functions can have the same name if their parameter types or number are different. That means that you can give the same name to more than one function if they have either a different number of parameters or different types in their parameters. Ex: #include <iostream> using namespace std; int operate (int a, int b) { return (a*b); } int operate (float a, float b) { return (a*b); }

14 Message Passing customer.balance(account_number) Information/parameter
OOPs consist of a set of objects that communicate with each other. A message for an object is a request for execution of a procedure & therefore will invoke a function in the receiving object that generates the desired result. customer.balance(account_number) Information/parameter object Message/procedure

15 PARAMETERS PASSED BY VALUE
When calling a function with parameters, what we have passed to the function were copies of their values but never the variables themselves. Example: When the function addition is called, the value of its local variables a and b become 5 and 3 respectively. Any modification to either a or b within the function addition will not have any effect in the values of a and b outside it, because variables a and b were not themselves passed to the function, but only copies of their values at the moment the function was called.

16 PARAMETERS PASSED BY REFERENCE
We are not passing a copy of its value, but we are somehow passing the variable itself to the function and any modification that we do to the local variables will have an effect in their counterpart variables passed as arguments in the call to the function. Example:

17 PARAMETERS PASSED BY REFERENCE (cont)


Download ppt "Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: http://www.mu.ac.in/myweb_test/MCA study."

Similar presentations


Ads by Google