0% found this document useful (0 votes)
7 views29 pages

Funciones

The document discusses modular programming and functions. Some key points: - Modular programming involves breaking programs into smaller subproblems or modules. This makes programs easier to understand, design, test, and debug. - Functions are reusable blocks of code that perform tasks. They allow for code reuse and automating common tasks. Functions can take parameters, perform operations, and return values. - Modules and functions promote project independence as different programmers can work on separate modules. They also make programs easier to modify and maintain. - Parameters can be passed to functions by value, meaning the function receives a copy, or by reference, meaning any changes made in the function persist outside of it.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views29 pages

Funciones

The document discusses modular programming and functions. Some key points: - Modular programming involves breaking programs into smaller subproblems or modules. This makes programs easier to understand, design, test, and debug. - Functions are reusable blocks of code that perform tasks. They allow for code reuse and automating common tasks. Functions can take parameters, perform operations, and return values. - Modules and functions promote project independence as different programmers can work on separate modules. They also make programs easier to modify and maintain. - Parameters can be passed to functions by value, meaning the function receives a copy, or by reference, meaning any changes made in the function persist outside of it.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Guevara

functions

Fausto A. Salazar Fierro


Modular Programming

The best-known method of


solving a problem is to break it
into smaller problems called
subproblems. This technique is
widely used in programming
since programming is nothing
more than solving problems, and
it is often called top-down
design, divide-and-conquer
methodology, or top-down
programming. It is a
programming paradigm.

Fausto A. Salazar Fierro


Modular Programming Features

Modularity
A program can contain other functions and can also refer to functions
that are in other files. These functions or modules are sets of
statements that are used to perform an operation or calculate a value.

In order to maintain simplicity and readability in large, complex


programs, a short main function is used in conjunction with other
functions rather than using a long main function

By separating a program into groups of modules, each module will be


easier to understand.

Fausto A. Salazar Fierro


What is a function?
A function is a piece of code that is called by name. Data can be passed to operate
(i.e., the parameters), and optionally data can be returned (the return value). All
data passed to a function is passed explicitly.

A function has a name to identify it.


• When we need to execute the instructions contained in the function, we do it
simply by using its name.
• A method is a piece of code that is called with a name that is associated with an
object. In most respects, it is identical to a function, except for two key
differences:

A method is implicitly passed the object on which it was called.

A method can operate on data that is contained within the class (remembering that
an object is an instance of a class - the class is the definition, and the object is an
instance of that data).

Fausto A. Salazar Fierro


In general: methods are functions that belong to a class, functions can be in any
other scope of code, so it could indicate that all methods are functions, but not all
functions are methods.

A method is on an object.

A function is independent of an object'suse of functions

We can build modular programs.

Fausto A. Salazar Fierro


Code reuse is achieved. Instead of writing the same code repeated
when needed, for example, to validate a date, you make a function
call that does it.

Functions allow you to automate tasks that we frequently require


and that can also be generalized through parameters or arguments.
it can receive parameters or arguments (some receive nothing), it
makes use of those received values as necessary, and returns a
value using the return statement; if it doesn't return something,
then it's not a function. In Java, functions use the static modifier.

Fausto A. Salazar Fierro


Variable Scope

Variables that are declared inside the main function or in user-defined


functions are called local variables.

These have a value when the function is executed, but their value is not
retained on completion of the function

Variables that are declared external to the main program or user-


defined functions are called global variables. These variables can be
taken from any function found within the program.

Modular programming is quite procedural since it is entirely oriented


toward writing code for functions without taking data into account

Fausto A. Salazar Fierro


Advantage

Project independent programming

• Several programmers can work on the same project if it is


divided into modules, which is required for a large and complex
program.

Efficient program development

• Programs can be developed more quickly since small, self-


contained modules are easier to understand, design, and test
than large programs.

Fausto A. Salazar Fierro


Advantage

• Multiple uses of modules


The code is relocatable; saved modules can be linked to any
other program that meets the input and output requirements.

Ease of debugging and modification

Each module can be written and tested separately from the rest
of the program. After a module has been tested, it can be used in
a new program without having to test it again.

Fausto A. Salazar Fierro


Disadvantages

• Modules too small

There are a lot of modules that only contain a very small function.
Using many small modules will add overhead to the build system,
increasing the size of the package.

Break down for no reason

Some codes almost never change. In those cases, there may not
be much point in trying to make it look cleaner or with abstract
logic if just using it already works fine.

Fausto A. Salazar Fierro


Implementation of functions
The general structure of a function is:

function_name(parameter declaration)
{
sentences;
commands;
return expression;
}
The declaration of the parameters represents the information
that is being passed to the function.

The additional variables used specifically by the function are


defined in the statements.– All functions must include a return
statement.

Fausto A. Salazar Fierro


Implementation of functions

Steps to implement a function:

1. Describe what the function should do.


2. Determine the inputs of the function, that is, what the function receives.
3. Determine the data types of the inputs.
4. Determine what the function should return and the type of return value.
5. Write the instructions that form the body of the function.
6. Function test: design different test cases.

Fausto A. Salazar Fierro


Some functions/methods that are used:
Function/method call

function call

Algunas funciones/métodos que se utilizan:


Math.pow()
Math.sqrt()
Character.isDigit()
System.out.println();

Llamada a la función/método Math.pow

Fausto A. Salazar Fierro


function call

Fausto A. Salazar Fierro


function call

Fausto A. Salazar Fierro


function call
The general structure of a Java method is as follows:

The parameter list (optional):


Items in brackets are optional. After the method name and always between parentheses,
Specifiers (optional): determine the type of a list of parameters, also called arguments, can appear
access to the method.return separated by commas.
Type: Indicates the type of the value
These parameters are the input data that the method
returned by the method.
receives to operate on them.
The data is returned using the return
statement. A method can take zero or more arguments. You must
If the method does not return any value, this specify for each argument its type.
type will be void.method The parentheses following the method name are required
Name: is the name given to the method. even if they are empty.throws exceptionList (optional):

Indicates the exceptions that the method can throw and


handle.Return – Used to return a value.

Fausto A. Salazar Fierro


function call

Fausto A. Salazar Fierro


Passing parameters to a function: by value and by reference
The term parameter is often used to refer to the variable in the function
declaration, while argument refers to the value that is sent.parameters by
value

Parameters by value have that name because what the subprogram receives
are nothing more than copies of the data values that the calling program
passes to it. Therefore, if we modify any of these values in the procedure,
the original data will remain unchanged.

Parameters by reference

On the other hand, in parameters by reference, what is passed to the


procedure is the data itself. And if it modifies them, the changes will remain
once execution returns to the module that is called the procedure. They are
used to obtain values from calculations made by an applet.

Fausto A. Salazar Fierro


Passing parameters to a function: by value and by reference

Example when changing a value of an array.

public class MyClass {


Without the "change" method, the value of
public static void main(String args[]) {
the Array is NULL, but with the method, it String[] array = new String[1];
will print "Hello world.“
cambiar(array);
Any change to the Array's content through System.out.println("El valor del array es: " + array[0]);
that reference will affect the original Array. }
But changing the reference to point to a new
public static void cambiar(String[] array) {
Array will not change the existing reference
in the original method.
array[0] = "Hola Mundo";
}
}

Fausto A. Salazar Fierro


Passing parameters to a function: by value and by reference

Ejemplo cambiando referencia.


public class MyClass {
Using that method does not make any public static void main(String args[]) {
changes to the original array and will print String[] array = new String[1];
NULL. While in other languages, it would
change the reference and update the cambiarReferencia(array);
main array, in Java, it would not, as it System.out.println("El valor del array es: " + array[0]);
doesn't handle a real reference. }

public static void cambiarReferencia(String[] array) {


array = new String[1];
array[0] = "Hola Mundo";
}
}

Fausto A. Salazar Fierro


Declaration of variables

A variable is a name given to a memory location. It is the basic unit of storage in a


program. The value stored in a variable can be changed during program execution. In
Java, all variables must be declared before they can be used.

Fausto A. Salazar Fierro


Variable Types
Hay tres tipos de variables en Java:
Instance variables
local variables They are non-static variables and are declared in a class
It is a variable defined within a block, outside of any method, constructor, or block.
method, or constructor. The scope of these
variables only exists within the method in Because instance variables are declared in a class, these
which the variable is declared, i.e., we can variables are created when an object of the class is created
access these variables only within that and destroyed when the object is destroyed.
method.
Unlike local variables, we can use access specifiers, for
static variables instance, variables. If we do not specify any access specifier,
the default access specifier will be used.
These are known as class variables.These
variables are declared in a similar way to
instance variables; the difference is that
static variables are declared using the
[java]static[/java] keyword inside a class
outside of any constructor or method block.To
access static variables, we don't need to
create any object of that class.

Fausto A. Salazar Fierro


Aplication in c++

Fausto A. Salazar Fierro


Aplication in c++

Fausto A. Salazar Fierro


Aplication en c#

Fausto A. Salazar Fierro


Fausto A. Salazar Fierro
Python

Fausto A. Salazar Fierro


References
http://dalila.sip.ucm.es/~manuel/JSW1/Slides/Librerias.pdf

https://users.dcc.uchile.cl/~lmateu/CC10A/Apuntes/bibstd/ind
ex.html

https://interpolados.wordpress.com/2017/03/07/bibliotecas-
de-java/

Fausto A. Salazar Fierro


Fausto A. Salazar Fierro

You might also like