Function in C Language

Ad Code

Function in C Language


Function in C

Function is slice of statements that together perform a specific task. You can divide up your code into separate functions. A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. Actually functions are used for divide a large code into modules, so you can easily debug and maintain the code. A function can also be referred as a method or a sub-routine or a procedure, etc.

Advantage of Function:
* Code Re-usability
* Develop an application in module format.
* Easily to debug the program.
Syntax of a Function:
A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.
return_type function_name( parameter list )
{
body of the function
}

Calling a function.:
When you call any function control goes to function body and execute entire code. For call any function just write name of function and if any parameter is required then pass parameter. At the time of function calling function must be terminated with ';'.
You can use function either with argument or without argument. If a function take any arguments, it must declare variables that accept the values as a arguments. These variables are called the formal parameters of the function.

function_name(); or variable=function_name(argument);

Example: Function without argument.

#include<stdio.h>

#include<conio.h>

void sum(); // declaring a function

clrscr();

int a=20,b=30, c;


void sum()  // defining function

{

c=a+b;

printf("Sum: %d", c);

}

int main()

{

sum();  // calling function

getch();

return 0;

}

Output
Sum: 50

Example: Function with argument.

#include<stdio.h>

#include<conio.h>

void swap(int , int);

int main()

{

int m,n;

     printf("\nEnter Two value:");

     scanf("%d%d",&m,&n);

     printf("\nValues before swap m=%d   n=%d",m,n);

      swap(m,n);

getch();

return 0;

}

void swap(int a,int b)

{

    int c;

    c=a;

    a=b;

     b=c;

   printf("\nValues after swap m=%d   n=%d",a,b);

There are two ways to pass value or data to function. By default, C uses call by value to pass arguments. Above example is call by value.
1. Call by value
2. Call by reference

Difference between call by value and call by reference.

Call by valueCall by reference
1) The argument is either variable or constant or an expression.1) The argument is address of variable.
2) The parameter is variable which can store the argument.2) The parameter must be a pointer variable.
3) The argument is copied within the parameter. Hence, arguments cannot be modified by the function.3) As the addres of variable is stored in the parameter. Hence the arguments can be changed by the function.

Explain call by value and call by reference along with example.:
A function can be called by value or by reference. In call by value method the value of the arguments are copied into the formal parameters of the function. In this case changes made to the parameters have no effect on the arguments.
Example Call by value:

#include<stdio.h>
#include<conio.h>
int sqr(int);
int main()
{
 int n=0;
     printf("\nEnter a number:");
     scanf("%d",&n);
     printf("%d is the square of %d",sqr(n),n);
getch();
return 0;
}
int sqr(int x)
{
    x=x*x;
    return x;
}

In the above example the value of the argument is copied into the parameter of the function sqr(), that is the value of n is copied into x when the assignment x=x*x takes place, only the local variable x is modified, but the variable n remains unchanged. Here n is a variable declared in main() function and x is a variable declared in sqr() function. These variables reside in different memory locations. Whenever the function sqr() is called, the variable x is created and is intialised with the value of n, this type of parameter passing is called call by value since we are only supplying the value of actual parameters to the calling function. In call by reference method the addresses of the arguments are copied into the parameters. Inside the function, the address is used to access the actual argument used in the function call. This means that the changes made to the parameters affect the argument.
Example: Call by reference
#include<stdio.h>
#include<conio.h>
void swap(int *a, int *b);
int main()
{
 int m,n;
     printf("\nEnter Two value:");
     scanf("%d%d",&m,&n);
     printf("\nValues before swap m=%d   n=%d",m,n);
      swap(&m,&n);
      printf("\nValues after swap m=%d   n=%d",m,n);

getch();
return 0;
}
void swap(int *a,int *b)
{
    int c;
    c=*a;
    *a=*b;
     *b=c;
}

Difference between formal and actual argument.
The terms formal argument and actual argument are sometimes used for the same distinction." So don't get confused if you find the question like difference between actual and formal arguments or difference between actual and formal parameters. They both are the same. The major difference between actual and formal arguments is that actual arguments are the source of information; calling programs pass actual arguments to called functions. The called functions access the information using corresponding formal arguments.

Example:

#include<stdio.h>

#include<conio.h>

int addNo(int, int);

int main()

{

int n1 = 10, n2 = 20, sum;

 

  sum = addNo(n1, n2); 

  printf("Sum of %d and %d is: %d \n", n1, n2, sum);


getch();

return 0;

}

int addNo(int a, int b)

{

  return (a + b);

}

In above the variables n1 and n2 are called actual arguments, whereas the variables a and b are called formal arguments. When values are passed to a called function the values present in actual arguments are copied to the formal arguments. In case of above program the values of n1 and n2 are 10 and 20 respectively. The main() would call addTwoInts with n1 and n2 as its actual arguments, and addTwoIntswill send back the computed sum to the main(). Conclusively, when a function is called all actual arguments (those supplied by the caller) are evaluated and each formal argument is initialized with its corresponding actual argument.

Recursive Function in C:
It is a programming technique that allows the programmer to express operations in terms of themselves. When Function is call within same function is called Recursion. The function which call same function is called recursive function. That means when a function call itself then that function is called Recursive function.
Syntax

void recursion() {
recursion(); /* function calls itself */
}
int main() {
recursion();
}

Example: Find the Factorial of any number using recursion

#include<stdio.h>

#include<conio.h>

int fact(int);

int main()

{

int i,f,num;

clrscr();

printf("Enter any number: ");

scanf("%d",&num);

f=fact(num);

printf("Factorial of %d = %d",num,f);

getch();

return 0;

}

int fact(int n)

{

if(n<0)

{

return (-1);

}

if(n==0)

{

return 1;

}

else

{

return(n*fact(n-1));

}

}




Post a Comment

0 Comments

Ad Code