100% de satisfacción garantizada Inmediatamente disponible después del pago Tanto en línea como en PDF No estas atado a nada 4.2 TrustPilot
logo-home
Resumen

Summary CS114 - 2.1 Defining Functions

Puntuación
-
Vendido
-
Páginas
6
Subido en
23-06-2021
Escrito en
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 :)

Mostrar más Leer menos
Institución
Grado









Ups! No podemos cargar tu documento ahora. Inténtalo de nuevo o contacta con soporte.

Libro relacionado

Escuela, estudio y materia

Institución
Grado

Información del documento

¿Un libro?
No
¿Qué capítulos están resumidos?
Chapter 2.1 (defining functions)
Subido en
23 de junio de 2021
Número de páginas
6
Escrito en
2021/2022
Tipo
Resumen

Temas

Vista previa del contenido

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
$3.07
Accede al documento completo:

100% de satisfacción garantizada
Inmediatamente disponible después del pago
Tanto en línea como en PDF
No estas atado a nada


Documento también disponible en un lote

Conoce al vendedor

Seller avatar
Los indicadores de reputación están sujetos a la cantidad de artículos vendidos por una tarifa y las reseñas que ha recibido por esos documentos. Hay tres niveles: Bronce, Plata y Oro. Cuanto mayor reputación, más podrás confiar en la calidad del trabajo del vendedor.
notesfromaCSmajor Stellenbosch University
Seguir Necesitas iniciar sesión para seguir a otros usuarios o asignaturas
Vendido
351
Miembro desde
6 año
Número de seguidores
278
Documentos
0
Última venta
1 mes hace
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 reseñas

5
34
4
25
3
6
2
2
1
1

Recientemente visto por ti

Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes