November 21, 2024

FUNCTION IN C PROGRAMMING

Function

A function is a self-contained block of codes or sub-programs with a set of statements that perform some specific task or coherent task when it is called.

A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.

Function Declaration OR Function Prototype:

  1. It is also known as a function prototype.
  2. It informs the computer about three things
    a) the Name of the function
    b) the Number and type of arguments received by the function.
    c) Type of value returned by the function
    Syntax :
    return_type function_name (type1 arg1 , type2 arg2);
    OR
    return_type function_name (type1 type2);
  3. The calling function needs information about the called function. If called function is placed before the calling function then the declaration is not needed.

Function Definition (Actual Arguments)

A function definition consists of the whole description and code of the function. It tells about what the function is doing what are its inputs and what is its output.

  1. It consists of code description and code of a function.
    It consists of two parts
    a) Function header
    b) Function coding

    Function definition tells what are the I/O function and what is going to do.
    Syntax:
    return_type function_name (type1 arg1 , type2 arg2)
    {
    local variable;
    statements ;
    return (expression);
    }
  2. A function definition can be placed anywhere in the program but is generally placed after the main function.
  3. The local variable declared inside the function is local to that function. It cannot be used anywhere in the program and its existence is only within the function.
  4. A function definition cannot be nested.
  5. Return type denotes the type of value that the function will return and return type is optional if omitted it is assumed to be an integer by default.

Function Call (Actual Arguments)

When the function gets called by the calling function then that is called, a function call. The compiler executes these functions when the semicolon is followed by the
function name.
Example:-
function(arg1,arg2,arg3);
The arguments that are used inside the function call are called actual argument
Ex:-
int S=sum(a, b); //actual argument

Advantages and Disadvantages of Function

AdvanatagesDisadvantages
Reusabilitycomplexity
modularityPerformance overhead
Abstractioncode Maintainability
ScalabilityDifficult in Debugging
code organizationCode Reusability

User-defined functions and Standard Function

User Define function:-

A function that is declared, called, and defined by the user is called a user-defined function. Every user-defined function has three parts:

  1. Prototype or Declaration
  2. Calling
  3. Definition

Syntax:-
Return type name of function (type 1 arg 1, type2 arg2, type3 arg3)
Return type function name argument list of the above syntax

Standard function

The C standard library is a standardized collection of header files and library routines used to implement common operations, such as input/output and character string handling. Unlike other languages (such as COBOL, FORTRAN, and PL/I) C does not include built-in keywords for these tasks, so nearly all C programs rely on the standard library to function.

Return Type:-

It is used to return value to the calling function. It can be used in two ways as return Or return(expression);


Ex:- return (a);
return (a*b); 
return (a*b+c);
Here the 1st return statement used to terminate the function without returning any
value

Ex:- /*summation of two values*/
int sum (int a1, int a2);
main()
{
  int a,b;
  printf(“enter two no”);
  scanf(“%d%d”,&a,&b);
  int S=sum(a,b);
  printf(“summation is = %d”,s);
}
   int sum(intx1,int y1)
{
   int z=x1+y1;
   Return z;
}

Category of Function based on argument and return type

There are four main categories of the functions these are as follows:

  1. Function with no arguments and no return values.
  2. Function with no arguments and a return value.
  3. Function with arguments and no return values.
  4. Function with arguments and return values.

1. Function with no arguments and no return values

NOTE: There is no communication between calling and called function. Functions are
executed independently, they read data & print results in the same block.

syntax:
void funct (void);
main ( )
{
funct ( );
}
void funct ( void );
{
}
Example:-
void me();
main()
{
  me();
  printf(“in main”);
}
  void me()
{
  printf(“come on”);
}


Output: come on
inn main


2. Function with no arguments and a return value

This type of function has no arguments but a return value

Syntax:-
int fun(void);
main()
{
  int r;
  r=fun();
}
  int fun()
{
  reurn(exp);
}
Example:-
int sum();
main()
{
int b=sum();
printf(“entered %d\n, b”);
}
int sum()
{
int a,b,s
s=a+b;
return s;
}


Here called function is independent and is initialized. The values aren’t passed by
the calling function. Here the calling function and called function are
communicated partly with each other.

3. Function with arguments and no return values

Here functions have arguments so, the calling function sends data to the called function but the called function does not return value. such functions are partly dependent on the calling function and the result obtained is utilized by the called function.

Syntax:-

void fun (int,int);
main()
{
int (a,b);
}
void fun(int x, int y);
{
Statement;
}
Example:
void msg ( int , int );
int main ( )
{
int a,b;
a= 2; b=3;
msg( a, b);
}
void msg ( int a , int b)
{
int s :
sum = a+b;
printf (“sum = %d” , s ) ;
}

Function with arguments and return value

Here calling function of arguments passes to the called function and the called function returns value to the calling function.

Syntax:-
fun(int,int);
main()
{
int r=fun(a,b);
}
int fun(intx,inty)
{
return(exp);
}
Example:
main()
{
int fun(int);
int a, num;
printf(“enter value:\n”);
scanf(“%d”,&a)
int num=fun(a);
}
int fun(int x)
{
++x;
return x;
}

Call by value and Call by reference

  1. Call By Value:- In the call by value copy of the actual argument is passed to the formal argument and the operation is done on the formal argument.


Since formal arguments are photocopies of actual arguments, any change in the formal argument does not affect the actual arguments


Changes made to the formal argument t are local to the block of the called function, so when control back to the calling function changes made vanish.

Example:-


main()
{
int x,y;
change(int,int);
printf(“enter two values:\n”);
scanf(“%d%d”,&x,&y);
change(x ,y);
printf(“value of x=%d and y=%d\n”,x ,y);
}
change(int a,int b);
{
int k;
k=a;
a=b;
b=k;
}

Output: enter two values: 12,23
Value of x=12 and y=23

2. Call By Reference

Here instead of passing value address or reference are passed. Function operators or address rather than values. Here formal arguments are the pointers to the actual arguments.

Example

Example:-
void main()
{
int a,b;
change(int ,int);
printf(“enter two values:\n”);
scanf(“%d%d”,&a,&b);
change(&a,&b);
printf(“after changing two value of a=%d and b=%d\n:”a,b);
}
change(int *a, int *b)
{
int k;
k=*a;
*a=*b;
*b= k;
printf(“value in this function a=%d and b=%d\n”,a,b);
}


Output: enter two values: 12,32
Value in this function a=32 and b=12
After changing two values of a=32 and b=12

Recursion

When a function calls itself (inside the function body) again and again then it is called a recursive function. In recursion calling function and called function are the same. It is a powerful technique for writing complicated algorithms in the easiest way.
According to recursion problem is defined in terms of itself. Here statement within in body of the function calls the same function and same time it is called a circular definition.

Syntax:-

main ()
{
rec(); /function call/
rec();
rec();

Example:- /calculate factorial of a no. using recursion/

int fact(int);
void main()
{
int num;
printf(“enter a number”);
scanf(“%d”,&num);
f=fact(num);
printf(“factorial is =%d\n”f);
}
fact (int num)
{
If (num==0||num==1)
return 1;
else
return(num*fact(num-1));
}

Leave a Reply

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