Presentation is loading. Please wait.

Presentation is loading. Please wait.

FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.

Similar presentations


Presentation on theme: "FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types."— Presentation transcript:

1 FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types

2 Functions in Mathematics F(X) = X + 2; Mathematics Question) What is F(4)? Answer) 6 Computer Programming question X = 4; X = F(X); Output (X); Question) What is the output of this program? Answer) 6

3 Functions in Pascal program functionExample var i: Integer; function addTwo(x:Integer): Integer; begin addTwo = x + 2; end; begin i = 4; i = addTwo(i); writeln(i); end;

4 Procedures in Pascal program procedureExample procedure sayTwice(s: String); var i: integer; begin for i := 1 to 2 do Writeln(s); end; begin sayTwice(“Hello”); end;

5 Pascal passing by value program procedureExample var i:Integer; procedure addTwo(x: Integer); begin x := x + 2; end; begin i:=4: addTwo(i); Writeln(i); end; The output will be: 4 (not 6) because x and i are not referring to the same variable.

6 Pascal passing by reference program procedureExample var i:Integer; procedure addTwo(var x: Integer); begin x := x + 2; end; begin i:=4: addTwo(i); Writeln(i); end; The output will be: 6 (not 4) because x and i are referring to the same variable because “var” was added in front of x.

7 C, C++ functions #include void main() { int i = 4; addTwo(i); cout << i; } void addTwo(int x) { x = x + 2; }

8 C, C++ void function (procedure) #include void main() { String s = “hello”; sayTwice(s); } void sayTwice(String x) { printf(x); }

9 C, C++ passing by value #include void main(void) { int i = 4; addTwo(i); printf(i); } void addTwo(int x) { x = x + 2; }

10 C++ (not C) passing by reference. #include void swap(int &i, int &j) { int temp = i; i = j; j = temp; } int main(void) { int a = 10; int b = 20; swap(a, b); printf("A is %d and B is %d\n", a, b); return 0; }

11 Java Methods Java refer to functions as Methods. All Methods must belong to a class (unlike C, C++) Return types including void are the same as C, C++ Parameters are passed by value only for Primitive Types. By reference but to temporary storgage.

12 Java passes parameters by value only public void addTwo(int x) { x = x+2; } Will not swap the values passed into it.

13 This swap will not work in Java public void badSwap(Person x, Person y) { Person temp = x; x = y; y = temp; } Although x and y are references, they refer to a copy of the object that the calling parameter is referring to, not the same object.

14 C# static void SquareIt(int x) // The parameter x is passed by value. // Changes to x will not affect the original value of myInt. static void SquareIt(ref int x) // The parameter x is passed by reference. // Changes to x will affect the original value of myInt.


Download ppt "FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types."

Similar presentations


Ads by Google