Function definition

In the function definition, we will implement the actual task of a function using a group of statements.

Example

Write a function to add two numbers and return their sum.

Example

int sum(int a, int b)
{
    int ans;
    ans = a + b;
    
    return ans;
}




What is the difference between function declaration and definition?




Function Declaration

In the function declaration, we are giving information about the function to the compiler.

Example

Example

int sum(int,int);

fuction sum will return a interger value.

And it will take two parameters both of them are type integer.





Function Definition

In the function definition, we are actually implementing the function.

In other words, we are defining the task using a group of statements.

Example

Example

int sum(int a, int b)
{
    int ans;
    ans = a + b;
    
    return ans;
}

Here, we are implementing addition task using a set of c statements.

Note

We can also write the above function without using temporary variable ans like below,


Example

int sum(int a, int b)
{   
    return a+b;
}


Topics You Might Like