Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions (Methods) Pascal C, C++ Java Scripting Languages

Similar presentations


Presentation on theme: "Functions (Methods) Pascal C, C++ Java Scripting Languages"— 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; 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 < stdio.h> void main() { int i = 4; i = addTwo(i); } int addTwo(int x) x = x + 2; return (x);

8 C, C++ void function (aka procedure)
#include < stdio.h> void main() { char[] c = “hello”; sayTwice(s); } void sayTwice(char[] x) printf(x);

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

10 C++ (not Java) passing by reference.
#include <stdio.h> void swap(float *x, float *y) { float t; t = *x; /* *x is value pointed to by x */ *x = *y; *y = t; } void main() float a=1, b=2; swap(&a, &b); /* address of x, y */ printf("Values AFTER 'swap' %f, %f\n", a, b); return 0;

11 Java Methods Java refers 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

12 Java passes primitives by value only
public void addTwo(int x) { x = x+2; } Will not add 2 to the variable passed in to addTwo

13 This swap will not work in Java
public void badSwap(Person x, Person y) { Person temp = x; x = y; y = temp; } Non-primitives are passed as references to copies of the opject. 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"

Similar presentations


Ads by Google