Course Content
History of C Language
0/2
Basic Structure of C Program
0/1
Types of Errors
0/1
Language Fundamentals
0/1
Data Types and Modifiers
0/1
Programming with C Language
About Lesson

Types of function definitions

Defining a function is depends upon the logic in developer’s mind.  We also understood a function can accept zero to many number of arguments and can returns zero to one return value.  Based on this we can say, a function can be defined in four ways.

  1. No arguments and No return value
  2. No arguments but with return value
  3. with arguments and no return value
  4. with arguments and with return value

//Example of function with No arguments and No return value

#include

void add(void)  //definition of the function

{

    int x,y,sum;

    printf(“Enter any two numbers:”);

    scanf(“%d%d”,&x,&y);

    sum=x+y;

    printf(“sum of your numbers is :%d”,sum);

}

void main()

{

    add();  //invocation of function

}

//Example of function with No arguments but with return value

#include

int add(void)  //definition of the function

{

    int x,y;

    printf(“Enter any two numbers:”);

    scanf(“%d%d”,&x,&y);

    return(x+y);

}

void main()

{

    int sum;

    sum=add();  //invocation of function

    printf(“sum of your numbers is :%d”,sum);

}

//Example of function with arguments but no return value

#include

void add(int x,int y)  //definition of the function

{

    printf(“sum of your numbers is :%d”,x+y);

}

void main()

{

    int a,b;

    printf(“Enter any two numbers:”);

    scanf(“%d%d”,&a,&b);

    add(a,b);  //invocation of function

}

//Example of function with arguments and with return value

#include

int add(int x,int y)  //definition of the function

{

    return x+y;

}

void main()

{

    int a,b;

    printf(“Enter any two numbers:”);

    scanf(“%d%d”,&a,&b);

    printf(“sum of your numbers is %d”,add(a,b));

}

You cannot copy content of this page