Add Your Heading Text Here

Functions or methods are the main body part of the program.

Different functions are defined for different behaviours or business logics you want to perform to achieve your goal(i.e, functionality/feature of the program).

In programming languages, a function is a set of statements/instructions, which are processed(executed) to produce the desired output for the user.

A function cannot be written inside function.

We will understand the functions in 3 step process.

  • Function Definition
  • Function Call
  • Function Return

Function Definition

Function definition is the set of instructions written in function which represents which tasks need to be done with what kind of processing.

Syntax :

ReturnType FunctionName(Arguments){
            // set of statements to perform business logic
}

Return type is the data type of the variable, which we want to return at the end of the execution of the function. So that we can use returned variable for other useful purposes.

Function name can be anything as per choice except which conflicts the keyword naming conflicts.

Arguments or parameters can be useful when you want to use them for processing your business logic inside functions.

Inside function you need to write the programming code for your business logic.

For ex :

// void is used if you don’t want to return anything from function.
void main(){
	print(“Hello”);
}

Function Call

Function call is passing the execution flow to the specific function called in program.

If you’ve written the function in the program, but you never call the written function to execute then it will never gets executed. So, function definition is useless without function calling.

For ex :

class Programming{
      Programming(){
            // Function Call
            ExecuteFunction();
     }

     //Function Definition
     ExecuteFunction(){
            print("Program Execution");
     }
}

void main(){
      Programming programming = new Programming();
}

In above program,  ExecuteFunction() gets called from the constructor. If you’ve never called the  ExecuteFunction() then defining that function is useless.

Function Return

Generally, function returns some specific value computed using your business logic. So that returned value can be used for more useful purposes further in the program.

For ex :

class Competition{
  int marks;
  String name;

  Competition(int competitionMarks,String studentName){
    this.marks=competitionMarks;
    this.name=studentName;

//calculateMarks() returns int value so it is assigned to variable of int data type. So, it can be used further to check if user is winner or looser.
    int data=calculateMarks();

//getName() returns String value so it is assigned to variable of String data type. So, it can be used further to get the name of looser or winner.
    String user=getName();

    if(data>33){
      print(user + " is "+ "Winner...");
    }else{
      print(user + " is "+"Looser !!");
    }
  }

// function returning int value
  int calculateMarks(){
    return this.marks+25;
  }
// function returning String value
  String getName(){
    return this.name;
  }
}

void main(){
	Competition c=new Competition(5,"Novak");
	Competition c1=new Competition(10,"Rafael");
	Competition c2=new Competition(17,"Roger");
}

Output :

Novak is Looser !!
Rafael is Winner…
Roger is Winner…

If you don’t want to return any value from function you can simple use “void” keyword. That means “void” is used when function is not returning any value.

Note :

main() function in dart starts the program execution. Which has return type void.

Functions are the core functionality or body of any program. Each class consists of variables, constructors and functions. So, functions are the necessary requirement for any programmer to learn.

To be a better programmer, you must have an upper hand for creating functions as per your requirements. So, practice hard for getting a better hand on creating different functions.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
PREM SAI GANJIKUNTA
PREM SAI GANJIKUNTA
3 years ago

nice info guys !!! go on

Show Buttons
Hide Buttons