Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C

Learn step by step



C-Function

C functions are the basic building blocks in any program. Most of the C programs are written using functions to improve re-usability, understandability and also to keep track on them.

What is function?

A large C program is divided into basic building blocks these are called functions. C function contains set of instructions enclosed by "{ }" (curly brackets) which performs a specific operation in a C program. We can also say that, Collection of these functions creates a C program.

Uses of C functions:
  • C functions are used to avoid rewriting same code again and again in a program.
  • There is no limit of calling a C function to make use of same functionality wherever required.
  • We can call function/s any number of times in a program and from any place in a program when we require it.
  • A large C program can easily be understandable when it is divided into functions.
  • The basic concept of C functions are, re-usability, dividing a big task into small-small pieces to achieve the functionality and to improve understand-ability of very large C programs.
Function declaration

There are mainly 3 aspects in each C function. They are,

  • Function declaration or prototype - This informs compiler about the function name, function parameters and return value is data type.
  • Function call - This calls the actual function
  • Function definition - This contains all the statements to be executed.
S.no C function aspects syntax
1 function definition return_type function_name ( arguments list )
{ Body of function; }
2 function call function_name ( arguments list );
3 function declaration return_type function_name ( argument list );
Example

As you know, any functions should be declared and defined before calling it in a C program.

In the below program, function "square" is called from main function.

The value of "m" is passed as argument to the function "square". This value is multiplied by itself in this function and multiplied value "p" is returned to main function from function "square".


#include<stdio.h>
// function prototype, also called function declaration
float square ( float x );
// main function, program starts from here
int main( )
{
float m=7, n ;
// function call
n = square ( m ) ;
printf ( "\nSquare of the given number %f is %f",m,n );
}
float square ( float x ) // function definition
{
float p ;
p = x * x ;
return ( p ) ;
} RUN

Call C functions in a program?

There can be two ways by which a C function can be called from a program. They are,

  • Call by value
  • Call by reference
1.Call by value:

In call by value method, we have two parameters actual and formal. The value of the variable is passed to the function as parameter(formal parameter).

The value of formal parameter can not modify the value of actual parameter.

Different Memory is allocated for both actual and formal parameters. Because, value of the actual parameter is copied into formal parameter.

Actual parameter- Argument which is used in function call.

Formal parameter- Argument which is used in function definition

Example (using call by value):

In this program, the values of the variables "m" and "n" are passed to the function "swap".

These values are copied to formal parameters "a" and "b" in swap function and used.


#include<stdio.h>
// function prototype, also called function declaration
void swap(int p, int q);
int main()
{
int p = 12, q = 14;
// calling swap function by value
printf(" values before swap p = %d \nand q = %d", p, q);
swap(p, q);
}
void swap(int m, int n)
{
int tmp;
tmp = m;
m = n;
n = tmp;
printf(" \nvalues after swap p = %d\n and q = %d", m, n);
} RUN

2. Call by reference:

In call by reference, the address of variable is passed to the function as parameter.

So the value of the actual parameter can be modified by formal parameter.

Same memory is used for both actual parameter and formal parameter since only address is used by both parameters.

Example (using call by reference):

In this program, the address of the variables "m" and "n" are passed to the function "swap".

Function arguments are pointers so they do not copy the value they hold the address which is passed in function calling.

This address in '*p' and '*q' is used to access and change the values of the variables.


#include<stdio.h>
// function prototype, also called function declaration
void swap(int *p, int *q);
int main()
{
int m = 12, n = 14;
// calling swap function by reference
printf("values before swap m = %d \n and n = %d",m,n);
swap(&m, &n);
}
void swap(int *p, int *q)
{
int tmp;
tmp = *p;
*p = *q;
*q = tmp;
printf("\n values after swap p = %d \nand q = %d", *p, *q);
} RUN



Related Videos