Functions, sometimes called methods, contain statements which can process data. The statements can or cannot process data.Other statements are also can access the Methods. This action is called as calling a function or making a function call.

Functions may look different in different programming languages, but the way they work is mostly the same. The usual pattern is taking in data and using logic to manipulate that data. Functions may also be referred to by other names, for example, methods.

The major differences come from the different ways
the languages use syntax. Syntax is basically the use of spaces or tabs, operators, or keywords. In the end, all you’re doing is telling the compiler how to convert your
instructions into computer-interpreted commands.

Variables and functions make up the bulk of programming. Any bit of data you want to remember is stored in a variable. Variables are manipulated by your functions. In general, when you group variables and functions together in one place, you call that a class.

Example:

public void PrintNum ()
{
System.out.println (anotherNum);
}

When writing a new function, it’s good practice to fill in the entirety of the function’s layout before continuing on to another task. This puts the compiler at ease; leaving a function in the form void MyFunction and then moving on to another function leaves the compiler confused as to what you’re planning on doing.

The integrated development environment, in this case MonoDevelop, is constantly reading and interpreting
what you are writing, somewhat like a spell checker in a word processor. When it comes across a statement that has no conclusive form, like a variable, function, or class definition, its interpretation of the code you’re writing will raise a warning or an error. MonoDevelop might seem a bit fussy, but it’s doing its best to help.

Writing a Functions:

A function consists of a declaration and a body. Some programmers like to call these methods, but semantics aside, a function is basically a container for a collection of statements. Let’s continue with the example:

void MyFunction ()
{
}

Here is a basic function called MyFunction. We can add in additional keywords to modify the function’s visibility. One common modifier we’ll be seeing is public.

public void MyFunction ()
{
}

The public keyword needs to appear before the return type of the function. In this case, it’s void, which means that the function doesn’t return anything. but functions can act as a value in a few different ways.

For reference, a function that returns an int would look like this. A return statement of some kind must always be present in a function that has a return type.

public int MyFunction ()
{
return 1;
}

The public modifier isn’t always necessary, unless you need to make this method available to other classes. If
this point doesn’t make sense, it will soon. The last part of the function that is always required is the parameter list. It’s valid to leave it empty, but to get a feeling for what an arg, or argument in the parameter list, looks like, move on to the next example.

public void MyFunction (int i)
{
}

For the moment, we’ll hold off on using the parameter list, but it’s important to know what you’re looking at later on so it doesn’t come as a surprise. Parameter
lists are used to pass information from outside of the function to the inside of the function. This is how classes are able to send information from one to
another.

Function declaration is similar to class declaration. Functions are the meat of where logic is done to handle variables.

Function Scope:

Variables often live an ephemeral life. Some variables exist only over a few lines of code. Variables may come into existence only for the moment a function starts and then disappear when the function is done. Variables in the class scope exist for as long as the class exists. The life of the variable depends on where it’s created.

Example:

public class Classscopm{
int ClassInt;
void first()
{
int firsttint;
}
void second()
{
int secondint;
}
}

If we look at the above figure we can visualize how scope is divided. The outer box represents who can see
ClassInt. Within the first () we have a firsttint that only exists within the first () . The same is repeated for the secondint, found only in the second
() . This means that first () can use both ClassInt and firsttint but not secondint. Likewise, second () can see ClassInt and secondint but not
firsttint.

So far you are confused about function then there are some simple programs In which we use the function for different operations like addition of numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top