Skip to content
Sign upLog in
← Back to Community

can someone pls explain what a function is in java cause im new to coding

Profile icon
MatthewXia

i have no idea. pls help. (but u dont have to if to decide not to)

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
Profile icon
MatthewXia
Comments
hotnewtop
Profile icon
Coder100

A functions a block of code that can be used to repeat code more than one time.

Profile icon
MatthewXia

@Coder100
then why is it not called repeat?

Profile icon
Coder100

because a repeat would be like a loop.

@MatthewXia

Profile icon
MatthewXia

@Coder100
the why is it called function?

Profile icon
Coder100

coding is really just math theory. At the start, functions were literally just to do: f(x) = x in math.

@MatthewXia

Profile icon
MatthewXia

@Coder100
oh ok

Profile icon
Coder100

np

@MatthewXia

Profile icon
Coder100

For example:

String name = "Coder100"; Console.WriteLine("Hello world"); Console.WriteLine("Your name is "+name);

if you would want to do that a lot of times, at the end, it will be pretty tedious. Using a function will simplify that:

void greet(String name) { String name = "name"; Console.WriteLine("Hello world"); Console.WriteLine("Your name is "+name); } greet("Coder100");
Profile icon
MatthewXia

@Coder100
whats greet

Profile icon
Coder100

just an example function

@MatthewXia

Profile icon
Coder100

In case of java, that would be a method.

Profile icon
MatthewXia

@Coder100
whats a method?

Profile icon
Coder100

its the same as a function, but in java, it's part of a class

@MatthewXia

Profile icon
MatthewXia

@Coder100
whats a class. also sry for the hassle

Profile icon
gibbsfreenergy

Have a great day and fun coding! :)

Profile icon
gibbsfreenergy

If this answered your question please mark it as correct!

Profile icon
gibbsfreenergy

*** A JavaScript function is a block of code designed to perform a particular task. A function can be defined using function keyword and can be executed using () operator.***

Profile icon
MatthewXia

@JosephSanthosh
can you pls explain the function keyword?

Profile icon
gibbsfreenergy

@MatthewXia
The function keyword can be used to define a function inside an expression. You can also define functions using the Function constructor and a function declaration.

Profile icon
gibbsfreenergy

@MatthewXia

image

Profile icon
MatthewXia

@JosephSanthosh
can you pls also explain the function constructor and a function declaration. sry for the hassle

Profile icon
MatthewXia

@JosephSanthosh
im still confuzzled on how it works

Profile icon
gibbsfreenergy

@MatthewXia
You shud ask
@Coder100
to help u

Profile icon
MatthewXia

@JosephSanthosh
he already is but thx for the suggestion

Profile icon
gibbsfreenergy

LET ME HELP YOU!