-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.tex
More file actions
106 lines (71 loc) · 5.42 KB
/
functions.tex
File metadata and controls
106 lines (71 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
\chapter{Functions and How They Work}
This will be an extremely short chapter, but an important one.
We are already going to assume that you know what a function, a method, or procedure is and that you have written them before.
After all, Data Structures is a point continuing your education in programming, not beginning it.
That said it is possible that you missed some subtleties along the way.
That's understandable - programming is a very large topic and there's more than enough concepts that no one who graduates with a degree in computer science can be expected to be an expert in every area any more.
With this in mind, let me take the time to review some subtleties surrounding the vocabulary of functions.
\section{Function vs Method}
You'll often hear programmers use these two terms interchangeably to refer to what essentially amounts to a subprogram.
But what is the difference?
I like to explain it this way: functions are the verbs of the programming language. When we create a new function, we a creating a new verb in the programming language we are working in.
Methods are a special type of function that are closely linked to objects; they are the actions or verbs you want your objects to perform.
Java blurs this a bit with \texttt{static} methods, but for the purposes of this text, when I write \textit{method}, I am talking about Java's \textit{instance methods}. \textit{Function}, in the context of this book, is analogous to \textit{static methods}.
Similarly, if you are coming from Python, when I say function, I am talking about a boring top-level, unindented function such as the ones you've been writing since you first learned Python. Method would refer to the functions you create as part of your classes.
%\section{The Runtime Stack}
\section{Argument vs Parameter}
An argument is the actual value you pass in, the parameter is the variable that accepts it.
\begin{javacode}{Java Parameter vs Argument Example}
public static void doubleThis(int num) { // parameter
return 2*num;
}
public static void main(String[] args){
int x = 7;
int y = doubleThis(x); // argument
}
\end{javacode}
\begin{pycode}{Python Parameter vs Argument Example}
def doubleThis(num): # parameter
return 2*num
x = 7
y = doubleThis(x) # argument
\end{pycode}
In the above examples, \texttt{x} is an argument and \texttt{num} is a parameter.
\subsection{Does anyone actually care?}
I cared enough to look it up, but I also had to look it up to double check that I'm correct and I keep coming back to this page as a reference for myself.
In a casual situation or talking with another programmer, everyone will be able to \textit{grok} your meaning from the context, as you just did with the word \texttt{grok.} I would take care to get it correct for your assignments and exams, much like you would take care to avoid using ``ain't'' in a formal essay. One professor might be a stickler about it and one might not care.
\section{Passing Arguments}
The vast majority of programming languages are \textit{pass by copy} with a huge honking asterisk.
\begin{itemize}
\item Pass by copy means that when something is input as the argument to a function (or method), the function gets a copy of the thing you are passing to it.
\item The \textit{huge honking asterisk} is that you are almost always passing a \textit{reference} or \textit{pointer} to an object, not the object itself. The reason for this is that if we had a super mega huge object, copying it would take up a super mega huge amount of time and memory.
\end{itemize}
\subsection{How it Works in Java}
In Java, we have two broad categories of data types: primitives and objects.
When you pass a primitive, such as an \texttt{int} or \texttt{double}, the value gets copied from where it is stored in memory and copied into the argument.
When you create an object, such as with
\mintinline{Java}{Scanner scan = new Scanner(System.in);},
the variable \texttt{scan} will hold not the Object that was created by the constructor, but the \textit{memory location}, or \textit{reference} of where to find it.
Look at the code below where we are we create an array in \texttt{main} and then pass it to another method, \texttt{setIndexZeroToZero}:
\begin{javacode}{Code to change index 0 to the value 0}
public static void setIndexZeroToZero(int[] array) {
array[0] = 0;
}
public static void main(String[] args) {
int[] arr = {5,5,6,6};
System.out.println(Arrays.toString(arr));
// prints [5, 5, 6, 6]
setIndexZeroToZero(arr);
System.out.println(Arrays.toString(arr));
// prints [0, 5, 6, 6]
}
\end{javacode}
Because the memory location of the array \texttt{arr} was passed to \texttt{array}, the method \texttt{setIndexZeroToZero} was dealing with the same object.
Keep in mind that some objects are immutable, like any \texttt{String}.
This means you can't actually change them.
Operations that seem like they change them like replacing part of a string or converting things from upper case to lower case are all returning a newly generated string.
\subsection{How it works in Python}
%fact check
Practically speaking , everything in Python works the same as in Java.
Everything in Python is an object (including the integers, which are immutable.), so when things are passed or assigned into variables, the variable stores the memory location, or \textit{reference}, to the object.
Thus when you pass in a variable to a function, the function receives the memory location of the object; data is never duplicated.