Declaring a Function - Devbhoomi

FREE JOB ALERT & ONLINE TUTORIALS

Hot

Post Top Ad

Sunday 5 November 2017

Declaring a Function

A function declaration tells the compiler about a function name. The actual body of the function can be defined separately.
Example :

#include <stdio.h>

/* function declaration */
int sum(int num1, int num2);

int main ()
{
    /* local variable definition */
    int a = 10;
    int b = 20;
    int s;

    /* calling a function to get sum of the numbers */
    s = sum(a, b);

    printf( "Sum of the numbers is : %d\n", s );

    return 0;
}

/* function returning the sum of the two numbers */
int sum(int num1, int num2)
{
    /* local variable declaration */
    int result;

    result = num1 + num2;

    return result;
}

Output :
Sum of the numbers is : 30
    
Explanation :
The function is declared first and then it is defined. The main() calls the function sum() and passes two arguments so that called function can perform its defined task and return the result.

No comments:

Post a Comment

Post Top Ad