100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Summary

Summary CS114 - 2.1 Defining Functions

Rating
-
Sold
-
Pages
6
Uploaded on
23-06-2021
Written in
2021/2022

Summaries based on Chapter 2.1 from the "Introduction to Programming in Java" textbook to assist in revising for the CS114 tests & exams. For much lower prices, have a look at the bundle of notes for several chapters. More summaries will be added soon :)

Show more Read less









Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Summarized whole book?
No
Which chapters are summarized?
Chapter 2.1 (defining functions)
Uploaded on
June 23, 2021
Number of pages
6
Written in
2021/2022
Type
Summary

Subjects

Content preview

Static methods
• A function is a static method
• Implement mathematical functions
• Can have strings & other data types as their range/domain &
can produce side effects (e.g. printing output)
• Whenever you can separate tasks within programs (into
methods), you should do so
• Methods control flow
• Define static methods in a .java file by specifiying
o Method signature
o Sequence of statements that constitute the method

Control flow
• Harmonic has 2 static methods → harmonic() and main()
• First statement that Java executes is the first statement in main()
• “Call” = harmonic(arg)
• Next few statements operate as usual, except that the code
harmonic(arg) causes a “transfer of control” to the first line of
code in harmonic() each time it’s encountered
• Parameter variable n in harmonic() initialized to value of arg in
main() at the time of the call
• Executes statements in harmonic() until it reaches a return
statement → control back to main()
• Return value assigned to variable value


Function-call trace
• Approaches to following control flow
o Imagine each function prints its name &
argument value(s) when it’s called
o Added indentation exposes flow of control &
helps us check each function has the effect
we expect
• Adding calls on StdOut.println() helps understand
what a program is doing (debugging)
• If return value match expectations, we don’t need to
tract the function code in detail → saves us work




1

, Terminology




Static method definition
• Signature = first line of definition
• Also specifies type of each parameter variable & return type of
method
• Return statement transfers control back to point where static
method was called & returns result
• Body may declare local variables (used only in the method
they’re declared in)


Function calls
• Method call = method name + arguments → separated by commas & in
parentheses (same form for math functions)
• Method call is an expression (can use it to build more complicated
expressions)
• Argument is an expression

Multiple arguments
• Static method can take more than one argument
& therefore more than one parameter variable
• Parameter variables are generally different Multiple return statements
types • Control goes back to calling program as soon as
• Type & name of each parameter variable are first return statement is reached
declared in the function signature (declarations • May be multiple return statements → any static
for each variable separated by commas) method returns a single value each time it’s
invoked (value after first return statement
Multiple methods encountered)
• Some programmers insist only having one
• Can have many static methods in a .java file
return statement per method (but not strict in
• Each method has a body with sequence of
this textbook)
statements enclosed in curly braces
• Methods are independent & can be in any order
in the file
• Static method can call any other static method Single return value
in the file/in a Java library • Java method provides only 1 return value to the
caller of type declared in method signature
Overloading • Java data types can contain more info than the
• Static methods with different signatures are value of a single primitive type
different static methods
• Overloading = using same name for different
methods → common practice in Java programs
• Another use = define 2 versions of a method (1
takes an argument & 1 uses a default value for
that argument)
2

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
notesfromaCSmajor Stellenbosch University
View profile
Follow You need to be logged in order to follow users or courses
Sold
351
Member since
6 year
Number of followers
278
Documents
0
Last sold
1 month ago
Summaries for Grades 10 - 12 (various subjects)

I am an A-aggregate student, and making summaries is one of my favourite studying techniques. Stuvia gives me the opportunity to share my notes with other learners who may need them!

4,3

68 reviews

5
34
4
25
3
6
2
2
1
1

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card or EFT and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions