What is Function or a Method in any programming language?

A Function or a method in any programming language is something that will do some kind of operations by taking in some values and process those values and return the result when it is called.
It has different syntax for creating and calling in different programming languages for example in c# it is like:

Example:

Creating a function:

public int Add(int firstnumber, int lastnumber)
{

      return firstnumber+lastnumber;
}

So here:

Public: This is the access specifier
int: Is the return type
int firstnumber, int lastnumber: Are the two input parameters of int data type
firstnumber+lastnumber : This is the operation done for the input values
return : This is the keyword for returning the result which must be the same type as of the function return type.

Calling a function:

int resultA = Add(1,2);

This will call the function Add by passing 1 and 2 as input parameters and the returned result will be stored in the variable resultA.

No comments:

Post a Comment

Popular Posts