Functions In C Programming

 Functions In C Programming


What is Function in C?


A function in C is a block of code which is executed only when it is called in a program. We can pass data to functions as a parameter to functions. Self contained block rescued in program to perform the tasks. Function once it is defined it can be used in a program many times to perform a task. 

In another term, large programs are divided into small building blocks known as functions. The set of statements are enclosed within opening and closing curly braces({ }). 


Advantages of Functions:

1. Reusability of Code: 

We can avoid rewriting program code or logic again and again.  

2. Function calling:

We can call functions in the program many times at any place.

3.  Divide Large program:

We can divide large program into smaller blocks called functions to avoid complexity of program


Types of Functions in C:

Functions are of two types

1. Standard Library Functions / Built-In Functions:

The predefined and precompiled functions which are written in header files called library functions. For example, 

scan(), printf(), getch(), main(), clrscr(), gets(), puts() etc. 


2. User Defined Function:

The user defined functions are defined by the C programmer, name is given by the programmer and the programmer can use these functions in his / her program to avoid the complexity of the problem and it helps to optimise the code. 


Aspects of Functions:

Function Declaration:

Function declaration helps the compiler to know how many parameters it will take, what parameters it has to return and what type of data it has to take. 

Syntax:

return_type Function_name(List of parameters);


return_type  is the type of data that the compiler has to return to function, function_name is the name of a user defined function, list of parameters are the arguments list which is used to store data and perform tasks.


For example following is function declared to perform sum of two integers


int sum(int n1,  int n2);

 Here int is return type of function sum, sum is user defined function, n1 and n2 are two parameters which have integer data type. Function declaration is terminated with a semicolon. 

The parameter names are not mandatory in the function declaration. We can declare our functions in following ways also,

int sum(int, int);


2. Function Definition:

A function definition consists of a function header and function body.

Syntax:

return_type function_name(list of parameters)

{

//body of function

}


return_type : 

Every function starts with return_type. If you do not have return type then you can declare and define your function with void type. 

function_name: 

function_name  is the name defined by the programmer and it should be unique.

List of parameters:

 list of parameters are the values that are passed during function calling


  For example;

int sum(int n1,int n2)

{

return n1+n2;

}


int sum() is the function name  defined by the programmer, n1 and n2 two parameters are used to perform addition.  Pair of parentheses consist of a body of function where addition of n1 and n2 parameters are returned to function sum().


3. Calling Function:

The user defined functions are not executed immediately.  First they are called in the program and these are executed.

Syntax:

sum( 10, 10);

Sum is a function which is called in the main() program. 10, 10 are the integer values passed in the function call.


For example

#include<stdio.h>

int sum(int n1,int n2); //Function prototype, Function declaration


int sum(int n1,int n2) // Function definition

{

    

    return n1+n2;

}

void main()

{

     

    int add = sum(10,30);// Function calling

    printf("The sum of two integers is : %d",add);

}


The sum of two integers is : 40


In the above example sum function is declared with integer type. It has two parameters n1 and n2 which have integer data type. Function is defined and the sum of two integers is returned. Function is called in main(). Two integer values 10 and 30 are passed in the function call and the result is stored in the add variable. 




A program to to take user input at run time and pass the values to calling function.

#include<stdio.h>

int sum(int n1,int n2); //Function prototype, Function declaration


int sum(int n1,int n2) // Function definition

{

    

    return n1+n2;

}

void main()

{

     int a,b;

    printf("Enter any two integer values\n");

    scanf("%d%d",&a,&b);

    int add = sum(a,b);// Function calling

    printf("The sum of two integers is : %d",add);

}

Enter any two integer values

50 70

The sum of two integers is : 120




 Function Arguments in C:

In C there are two types of arguments used in functions

1.     Formal argument:

The variable which are declared in function prototype or function declaration is called formal arguments. The formal arguments are defined in function declaration.

For example:

int sum(int n1,int n2);

n1 and n2 are formal arguments which have integer type and these arguments are declared or defined in function declaration

2.    Actual arguments:

The values that are passed to the functions which are called in main() function is called actual arguments. Actual arguments are defined in calling function.

The formal arguments and actual arguments must match with sequence, type and number.

Int multi=sum(10,20);

10 and 20 are actual arguments.

 

Parameter passing in C:

In C the parameter are passed to function by following ways.     

 

  1. Call by Value:

In the call by value method the value of the actual parameter is copied to formal parameters.  The value of the variable is used in function calls.  In this method we cannot modify the value of actual parameters in formal parameters. Different memory is allocated to both actual and formal parameters. 

/****************************************************************

 C program to demonstrate call by value

 ***********************************************************/

 

#include<stdio.h>  

void call_value(int n);

int main() {    

    int x=200;    

    printf(" The value of X Before function call %d \n", x);    

    call_value(x);//passing value in function    

    printf("The value of X After function call %d \n", x);    

return 0;  

}    


void call_value(int n) 

{    

    printf("Before adding value inside function %d \n",n);    

    n=n+200;    

    printf("After adding value inside function %d \n", n);    

}    

 The value of X Before function call 200 

Before adding value inside function 200 

After adding value inside function 400 

The value of X After function call 200 

  1. Call By Reference:

In the call by reference method the address of the variable is passed in the function call as the actual parameter. The value of actual parameters can be modified by changing formal parameters since the address of actual parameters are passed. Memory allocation is the same for both the actual and formal parameters. All the operations are performed on the value that is stored at the address of actual parameters. 

****************************************************************

 C program to demonstrate call by reference

 ***********************************************************/

 

#include<stdio.h>  

void call_ref(int *n);

int main() {    

    int x=100;    

    printf("The value of X before function call =%d \n", x);    

    call_ref(&x);//passing reference in function    

    printf("The vallue of X after function call %d \n", x);    

return 0;  

}    

void call_ref(int *n)

{    

    printf("Before adding value inside function n= %d \n",*n);    

    (*n) += 100;    

    printf("After adding value inside function nu %d \n", *n);    

}      

The value of X before function call =100 

Before adding value inside function n= 100 

After adding value inside function nu 200 

The vallue of X after function call 200 




Types of User Defined Functions

 

The user defined functions are defined as follows

1. Function with no argument and no return type
2. Function with no arguments but returns a value
3. Function with argument and no return type
4. Function with argument and with return type

 

1. Function with no argument and no return type :

Programmer can declare, define and call functions with no argument and no return types. This type of functions will not return any values when thes are called in program.

Syntax:

void function_name(); // function declaration

void function_name() // function definition

{

//body of functions

}

function_name(); // function call

 

For example

#include<stdio.h>

void sum(); //function declaration

void main()

{

printf(The addition is\n);

sum(); // function call

}

void sum() // function definitin

{

int add, a=20,b=20;

add=a+b;

printf(The sum of a and b is%d, add);

}

 

Output:

The sum of a and b is 40

 

2. Function with no arguments but returns a value :

In some cases we wont pass any arguments to the function while declaring, defining and calling. But this type of functions return values when we call the functions.

Syntax:

int function_name(); //function declaration

int function_name()  //function definition

{

  // statements

            Return x;

}

Function_name();  // function call

 

For example,

#include<stdio.h>

int multiplication();

int main()

{

int multi;

multi=multiplication();

printf(“multiplication is: %d”, multi);

return 0;

}

int multiplication()

{

int a=20, b=30, multi;

multi=a*b;

return multi;

}

Output:

The multiplication is 600

 

3.         Function with argument and No Return value:

In above two programs we have fixed values in program so whenever we executes our program output will remain same because values are fixed. In real time we deal with dynamic data means we have to allow user to enter his own values rather than fixed values.  So the function with arguments and no return value method allows us to pass arguments to the functions while calling the function. But this type of functions will not return any value.

For example,

#include<stdio.h>

void addition(int,int);

void main()

{

Int a,b;

printf(“Enter two integers\n);

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

addition(a,b);

}

void addition(int a,int b)

{

Int sum;

sum=a+b;

printf(“The sum is %d”,sum);

}

 

4.         Function with argument and Return Value:

This method allows us to pass the arguments to the function while calling the function. This type of functions will return some value when we call the function from main(). Data type of return value will depend up on the return type of function declaration.  For example if return type is int then return value will be int.

Example,

#include<stdio.h>

int multiplication(int,int);

int main()

{

Int a,b,multi;

Printf(“Enter two values\n”);

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

Multi=multiplication(a,b);

Printf(“The uultiplication is: %d”,multi);

Return 0;

}

Int multiplication(int a,int b)

{

Int multi;

Multi=a*b;

Return multi;

}

 

 Recursion Function in C:

A function that calls itself is called a recursive function and this technique is called recursion. Recursion is the process by which a function calls itself repeatedly until some specified condition has been satisfied. Function calling itself is called recursion.

Syntax:

void sum()

{

statements;

sum();  // Recursive function, function calls itself

statements;

}

int main()

{

statement;

sum();

}

In order to solve a problem recursively two conditions are there,

the problem must be written in recursive form and the problem statement must include a stopping condition.

For example

/****************************************************************

 C program to find factorial using recursive Function

 ***********************************************************/

 

#include<stdio.h>

int factorial(int n); // Function declaration

int factorial(int n) // Function definition

{

    if (n>=1)

        return n*factorial(n-1); // Recursive function call

    else

        return 1;

}


int main() {

    int n;

    printf("Enter a number to get factorial: ");

    scanf("%d",&n);

    printf("Factorial of number %d is %d", n, factorial(n)); // Function call

    return 0;

}


Enter a number to get factorial: 5

Factorial of number 5 is 120


At the beginning execution starts from main() where user input i.e. argument is passed as 5 to function factorial. Then this value is passed in recursive function factorial(). In each recursive call the value of n is decreased by 1. When the value of n is less than 1 then there will be no recursion and factorial is returned to main()


 

 

 




टिप्पणी पोस्ट करा

0 टिप्पण्या