Function in C programming

By | May 15, 2019

Definition:

A function is an independent section of C which is a block of statement code that performs a specific task and optionally returns a value to the calling program. Generally, it processes information that is passed to it from the calling portion of the program, and return a single value to the program. information is passed to the function via special identifiers caused arguments also called parameters, and return via the return statement. some function, however, accept information but do not return anything

  • Each function has a unique name. By using that name we can execute the statements present in that function from anywhere in the program. This is called calling the function.
  • A function is an independent part of the program because it can perform its task without interfering with other parts of the program.
  • Any number of function can be used in a program. There is no limit on the number of functions that can be used.
  • After execution of a function body, the control returns to main().

Type of function:

in c programming function are of two types: predefined function and user-defined function.

  1. predefined function
  2. user-defined function

Pre-defined functions are C library function which already has been defined inside the header file. The programmer must include the proper header file for the execution of such a function.


User-defined functions are the functions that are written by the programmers themselves and form the part of the source code. Those are compiled with other functions such as main() function.

Component of function:

There are three component of function: return type, name and argument list.

return type indicates the type of data that function return when it is called from other function

Function name is the unique name provided to function.

The argument list is the list of argument which is supplied to function when a function is called.

Component of a function program:

A function program consists of the following components:

  • Function prototype
  • Function definition
  • Calling function
  • Actual and formal argument

Function prototype: Function prototype informs the compiler about the function name, type of data return, number of parameters,types of parameter and order in which these parameters are expected.

int addition (int, int)

  • The name of the function is addition.
  • The function takes two parameters both integer type.
  • The function returns an integer value.

Function definition: The function definition is the set of statement present inside the curly braces {} which are executed line by line when the function comes into action. The general form of the function definition is:

return_type name_of_function(list of formal arguments)

{

body of function

return value;

}

Function call: When the function is ready, it must be called inside the boundary of the main(). the function never comes into action if it is not called inside the main. The program execution starts with main, then run into the function calling statement. The function is called by using its name followed by the parameters list enclosed in parentheses and terminated by a semicolon.

Actual and formal argument: Anything passed to a function is called its argument. a function always works on the arguments passed to it.

When the function calls another, the calling function provides the data to the calling function called actual arguments.

The called function receives these data in a new variable called formal argument.

main()

{

myfunction(40);//40 is actual argument

}

myfunction(int n)// n as formal argument.

}

Ways of calling a function:

There are two ways of calling a function in c programming.

  • Call by value
  • Call by reference

A function defined outside main must be called in the body of main as function call statement. while calling a function we may pass the value of a variable as an actual argument or address of a variable as an actual argument.

if the function is called by passing the value of a variable as an actual argument. the function call is called call by value. If the function is called by passing the address of variable as an actual argument then such function call is called call by address or call by reference.

programs illustrating use of function in c programming

To find the sum of any two integer numbers using the user-defined function.

#include<stdio.h>
#include<conio.h>

int sum(int x,int y)
{
        int z;
        z=x+y;
        return(z);
}
void main()
{
        int a,b,c;
 
        int sum(int x,int y);      /*Declaration of fx*/
 
        printf(“\nEnter any two numbers”);
        scanf(“%d%d”,&a,&b);
       
c=sum(a,b);                  /* fx call */
        printf(“\n Total is %d”,c);
        getch();
}
 

To convert an input character into uppercase using a user-defined function

#include<stdio.h>
#include<conio.h>
 
char toup(char x)
{
        char y;
        y=(x>=’a’&&x<=’z’)?x-32:x;
        return(y);
}
 
 
void main()
{
        char x,y;
 
char toup(char x);
 
        printf(“\nEnter any character”);
        x=getchar();
       
y=toup(x);
        printf(“\n Total is %c”,y);
        getch();
}

Leave a Reply

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