Share this post on:

Function in C is a object which has certain functionality to perform a particular task. It is block of statement which perform task.

According to parameter and return type a function can be categorized in four types of Function.

  • No arguments no return
  • Accept arguments but no return
  • Accept arguments and also return
  • No arguments but return

No arguments no return : A Functionwill not accept any value as an arguments and also not return any value. Normally in this type for function we write void as a return type and parenthesis will be empty.

Example:

void sum();

void main()

{

sum();

}

Accept arguments but no return : A Function which will accept some value as an argument but not return any value. Normally in this type of function we write void as a return type but parenthesis can’t be empty. we write the data type of argument in the parenthesis.

Example :

void sum(int,int);

void main()

{

int a,b;

printf(“Enter two no”);

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

sum(a,b);

}

We can use same name of arguments in the calling Function and in the called function or we can change it but we can’t change no. of arguments, data types of arguments and sequence of arguments.

Example : Write a Program to define a no. of which will accept a no. and print the factorial?

void Table(int);

void main()
{
int num;
printf(“Enter a no”);
scanf(“%d”,sum);
Table(num);
}

void Table(int,num)
{
int i;
for(i=1;i<=10;i++)
printf(“%d”,(num<=0));
}

Accept argument and also return : A function which will accept sum value as an argument and also return a value. Normally in this type of function we write void as a return type. we write the data type of returned value as a return type.

void sum(int,int);

void main()
{
int a,b,c;
printf(“Enter two no”);
scanf(“%d%d”,&a,&b);
c=sum(a,b);
printf(“%d”,c);
}

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

Although a function may accept more than one value as an argument but it always return only one value.

No argument but return : A function which will not accept any value as an argument but return the result.

Example :

void sum();

void main()
{
int c;
c=sum();
printf(“%d”,c);
}

int sum()
{
int a,b,c;
printf(“Enter two no.”);
scanf(“%d%d”,&a,&b);
c=a+b;
return c;
}

Calling Function

We can call a function with two difference mechanism.

  • Call by value
  • Call by Address (call by reference)

Call by value : When we use call by value mechanism , a carbon copy of argument will be passed in the called function. which is store in separate variable in called function, here any changes occurred in the called function will not change the original variable in calling function.

Example:

void main()
{
int a,b;
a=10;
a=15;
printf(“%d%d”,a,b);
change(a,b);
printf(“%d%d”,a,b);
}
void change(int a,int b)
{
printf(“%d%d”,a,b);
a=a+10;
b=b+20;
printf(“%d%d”,a,b);
}

Call by Address : In call by reference mechanism, we pass the address of variable to the called function, Hence any change occurred in the called function will also change the original value in calling function. To implement the call by address mechanism we use the concept of pointers.

Example:

void change(int *,int *)

void main()
{
int a,b;
a=10;
b=15;

printf(%d%d”,a,b);
change(&a,&b);
printf(“%d%d”,a,b);
}

void change(int a,int b)
{
printf(%d%d”,a,b)
a=a+10;
b=b+20;
printf(“%d%d”,a,b);
}

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

Leave a Comment

Your email address will not be published. Required fields are marked *