What do you Mean by Function?

The collection of statements is known as a function where the set of codes are grouped to perform a particular operation or task. For example, when we call the “print()” function in our program to print something, the system executes some group of codes or statements to display a message that we want on the console.

What is Function Definition?

A function definition consists of the name of the function with arguments and the code of the function. It tells about the behavior of the function, what is it doing, what will be its inputs, and what output will it return. The definition of a function consists of two parts which include a body and a header.

Syntax

In most languages, the following syntax is used to define functions.

modifier return Type function_Name(parameters)

{

// Function body;

}

Terms Used in Syntax

Modifiers: The access modifiers are used to impose security or restrictions on function. Generally, it should be mentioned while declaring a function in the class (otherwise the default one is selected by the compiler). It emphasizes the accessibility of the function to the outside classes or functions.

Return Type: The return type indicates that a function is a returning type or a non-returning type and also the type of the value that a particular function will return. When any function performs the desired operations without returning a value then we use 'void' keyword to show that function is a non-returning type.

Function Name: This is the actual name that we give to the function during its creation. The patent name or a real name is used to call a function. 

Parameters: A parameter can be defined as a placeholder that is given under the parenthesis. When a function is called, we pass a value to the parameter of the same data type. The value which we give while calling a function is referred to as an actual parameter. The parameters which are recorded in the function signature are defined as formal parameters. Parameters are not mandatory in a function i.e., there may or may not be a parameter.

Function Body: The function body can be defined as the block of instructions that the function executes when we call it.

Example

Here we use the function called max (). This function takes two parameters n1 and n2 and returns the maximum between these two numbers.

// Return the max between two numbers 

public static int max (int n1, int n2)

{

int max; // variable to store the maximum

if (n1 > n2)

max = n1;

else

max = n2;

return max;

}

What is Function Declaration?

The function prototype is another name of the function declaration. A compiler is informed about the name of the function, the number, the type of arguments received, and the type of value that the function will return using this prototype.

Calling A Function

While creating a function, we give a definition that shows what it does. To use a function, we have to call the function by its actual name that has been stated in the function definition. Generally, there exist two ways to call a function. The difference depends on whether the function is returning a value is or not.

When a program calls a function, it starts to execute the instructions given in it. The program control is transferred to the caller function after reaching the end of the function, i.e. the execution of the function is completed. The caller function then executes other statements that are written after this calling statement (if any).

If a value is returned by the function, then the values of the similar data type are passed which is shown in the function signature. Then we store the return value in a variable.

Example

int larger = max (30, 40); // pass the value in the actual variable

When the function returns void, then we directly call the function without storing the return value, like 'println()' returns void.

System.out.println("Welcome to Java!";

The above-mentioned examples depict how a returning function and a non-returning(void) function can be invoked.

Returning Function

The program below shows how we can call a function that returns a value.

Code

public class Returning {

//Main  

public static void main(String[] args) {

int num1 = 7;   // local variables

int num2 = 8;

int k = max (num1, num2);      //function call

System.out.println("The maximum between "+num1+" and "+num2+" is " + k);

}

// Return the max between two numbers 

public static int max(int n1, int n2) {

int maxi;       // variable to store the maximum

if (n1 > n2)

maxi = n1;

else

maxi = n2;

return maxi;

}

}

Output

The maximum between 7 and 8 is 8

Void Function

The program below shows how we can call a function that returns nothing (void).

Code

public class TestVoidFunction {

public static void main(String[] args) {

printGrade(72.1);      //function call

}

public static void printGrade(double marks) {

if (marks >= 90.0) {

System.out.println('A');

}

else if (marks >= 80.0) {

System.out.println('B');

}

else if (marks >= 70.0) {

System.out.println('C');

}

else if (marks >= 60.0) {

System.out.println('D');

}

else {

System.out.println('F');

}

}

}

Output

C

Function Categories Based on Argument and Return Types

1. Function with no argument & no return value

Below is the function of the situation.

Syntax

void function(void);

main()

{

void function()

{

Statement;

}

Example

void me();

main()

{

me();

printf(“in home”);

}

void me()

{

printf(“get on”);

}

Output

get on in home

2. Function with no Argument but Return Value

The function in this scenario is independent and initialized. The calling function does not pass the values. The communication between the called function and the calling function is partly with each other.

Syntax

int fun(void);

main()

{

int r;

r=fun();         //store the value of fun() in r variable

}

int fun()

{

return(exp);

}

Example

C language code:

int sum();

main()

{

int b=sum();

printf(“entered %dn, b”);

}

int sum(){

int a,b,s; 

s=a+b;

return s;}

3. Function with Argument but no Return Value

In this function, an argument is present, thus, the data is sent to the called function from the calling function but the value is not returned over hereby the called function.

Syntax

void fun (int,int);

main()

{

int (a,b);

}

void fun(int x, int y)

{

Statement;

}

4. Function with Argument and Return Value

In this function, the argument is present for the calling function and the returned value is sent by the calling function to the called function.

Syntax

fun(int,int);

main()

{

int r=fun(a,b);

}

int fun(intx,inty)

{

return(exp);

}

Example

main()

{

int fun(int);

int a,num;       //variables

printf(“enter value:n”);

scanf(“%d”,&a);

int num=fun(a);

}

int fun(int x)

{

++x;

return x;

}

Advantages of a Function

The advantages of using a function in a program are as follows.

  • By using functions, large and difficult programs can be divided into subprograms and solved.
  • For a certain task when it is required for the code to be used repeatedly, function comes into play and avoids it.
  • Because of the smaller size, easy modification and testing are easier and quicker.

Common Mistakes

Following are some of the common mistakes students does.

  • In order to calculate the new value of the function, it is important to call the function correctly rather than assuming that the function will be called automatically because many times similar names are present which creates confusion for the program to run.
  • In order to create local variables, unnecessary parameters should be avoided.
  • Remember to put curly braces while defining a function

         main()

        {

         function1()

         }

          function1()

         {

           Statement;

           function2;

         }

          function 2()

          {

          }

Context and Application

This topic is significant in the professional exams for both undergraduate and graduate courses, especially for

  • BCA
  • MCA
  • B.Tech. in Computer Science Engineering
  • M.Tech. in Computer Science

Want more help with your computer science homework?

We've got you covered with step-by-step solutions to millions of textbook problems, subject matter experts on standby 24/7 when you're stumped, and more.
Check out a sample computer science Q&A solution here!

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Algorithms

Functions

Use of Functions Homework Questions from Fellow Students

Browse our recently answered Use of Functions homework questions.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Algorithms

Functions