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.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
CHAPTER:09 METHODS(FUNCTIONS) IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
C++ Function 1. Function allow to structure programs in segment of code to perform individual tasks. In C++, a function is a group of statements that.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Methods.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Constructors and Destructors
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
User-Written Functions
Functions.
C Functions -Continue…-.
Lecture 4: Expressions and Variables
Building Java Programs
Lecture 2: Operations and Data Types
Principles of programming languages 4: Parameter passing, Scope rules
Functions and an Introduction to Recursion
Primitive Data, Variables, Loops (Maybe)
School of EECS, Peking University
JavaScript: Functions
Function There are two types of Function User Defined Function
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Building Java Programs Chapter 2
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.
Procedural Programming
Name: Rubaisha Rajpoot
Lecture 3: Expressions and Variables
Building Java Programs
Constructors and Destructors
Building Java Programs
Building Java Programs Chapter 2
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.
Lecture 5: Basic Java Syntax AP Computer Science Principles
Chapter 9: Value-Returning Functions
Functions and an Introduction to Recursion
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 4: Expressions and Variables
Building Java Programs
Functions Imran Rashid CTO at ManiWeber Technologies.
Building Java Programs Chapter 2
CS1201: Programming Language 2
Building Java Programs
Parameters and Arguments
Presentation transcript:

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 material/

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)

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

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;

Scope of Variable

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.

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;

FUNCTION

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)

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); }

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); }

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); }

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

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.

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:

PARAMETERS PASSED BY REFERENCE (cont)