function in c programming language
function in c programming language

A function is a self-contained block of statements that perform coherent tasks of some kind. Every C program can be thought of as a collection of functions. A C program can contain any number of functions, as there is no upper limit, but it must have at least one function i.e. the main() function. We can call a function any number of times, so, the function helps us to avoid rewriting the same code over and over.

Declaring functions:

Declaration: the point at which a name has a type associated with it.

Definition: also a declaration, but at this point, some storage is reserved for the named object. The rules for what makes a declaration into a definition can be complicated but are easy for functions. You turn a function declaration into a definition by providing a body for the function in the form of a compound statement.

Formal parameters

These are the names used inside a function to refer to its arguments.

Actual arguments

These are the values used as arguments when the function is actually called. In other words, the values that the formal parameters will have on entry to the function.

The terms ‘parameters’ and ‘argument’ do tend to get used as if they are interchangeable, so don’t read too much into it if you see one or the other in the text below.

If you use a function before you declare it, it is implicitly declared to be ‘function returning int’. although this will work and was widely used in Old C, in standard C it is a bad practice that the use of undeclared functions leads to nasty problems to do with the number and type of arguments that are expected for them. All functions should be fully declared before they used.

There are four types of functions;

No argument with no return value

In this type of function there is no formal argument and the function returns no value.

e.g.

main()
{
void test(void);
test();
}
void test(void)
{
printf(“this is an example of no argument no return”)
}

No argument with a return value

#include<stdio.h>
main()
{
int test(void);
int max;
max=test();
printf(“The maximum of the two is : %d”, max);
}
int test(void)
{
int x, y;
printf(“Enter 1st no.:”);
scanf(“%d”, &x);
printf(“Enter 2nd no.:”);
scanf(“%d”, &y);
return(x>y?x:y);
}      

With argument with a return value

#include<stdio.h>
main()
{
int test(int, int);
int x, y, max;
printf(“Enter 1st no.:”);
scanf(“%d”, &x);
printf(“Enter 2nd no.:”);
scanf(“%d”, &y);
max=test(x,y);
printf(“The maximum of the two is %d”, max);
}
int test(int a, int b)
{
return(a>b?a:b);
}

With argument with no return value

#include<stdio.h>
main()
{
void test (int, int);
int x, y;
printf(“Enter 1st no.:”);
scanf(“%d”, &x);
printf(“Enter 2nd no.:”);
scanf(“%d”, &y);
test(x,y);
}
void test(int a, int b)
{
int max;
max=a>b?a:b;
printf(“The maximum of the two is: %d”, max);
}

Calling a function

A function gets called when a semicolon follows the function name. we can call a function in two ways.

Call by value

Whenever we call a function and pass something to it we have always passed the “values” of the variables to the called function. Such function calls are called “call by value”. Even if any changes are done in “called” function it does not affect the actual argument.

#include<stdio.h>
main()
{
void swap(int, int);
int a=10, b=20;
swapv(a, b);
printf(“\n a=%d b=%d”, a, b);
}
void swapv(int x, int y)
{
int t;
t=x;
x=y;
y=t;
printf(“\n x=%d y=%d”, x, y);
}

Note: the values of a and b remains unchanged even after exchanging the values of x and y.

Call by reference

Usually in C programming language we make a call by value. This means in general we cannot alter the value of actual arguments. But, if desired, it can always be achieved through a call by reference. In this method, the address of actual arguments of the calling function is copied into the formal arguments of the called function. Here, we send the address of variables instead of the value of variables and if there are any changes done within the called function it affects the actual arguments also, which are within calling function.

#include<stdio.h>
main()
{
void swapr(int *, int *);
int a=10, b=20;
swapr(&a, &b);
printf(“\n a=%d b=%d”, a, b);
}
void swapr(int *x, int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}

Note: in this program, we manage to exchange the values of a and b using their address stored in x and y.

Using a “call by reference” method intelligently we can make a function return more than one value at a time, which is not possible in case of “call by value”.

#include<stdio.h>
main()
{
void areaperi(int, float *, float *);
int radius;
float area, perimeter;
printf(“Enter radius of a circle:”);
scanf(“%d”, &radius);
areaperi(radius, &area, &perimeter);
printf(“Area=%f”, area);
printf(“\nPerimeter=%f”, perimeter);
}
void areaperi(int r, float *a, float *p)
{
*a=3.14*r*r;
*p=2*3.14*r;
}

Here we are making a mixed call, in the sense; we are passing the value of radius but the addresses of area and perimeter. And since we are passing the addresses, any changes that we make in values stored at addresses contained in the variables a and p would make the change effective in main(). That is why when the control returns from the function areaperi(), we are able to output the values of area and perimeter.

Thus, we have been able to return two values from a called function and hence have overcome the limitation of the “return” statement, which can return only one value at a time from a called function.

Recursion

In C it is possible for a function to call itself within its definition, this is known as recursion. A function is called “recursive” if a statement within the body of the function calls the function itself.

Here is an example of recursive function to calculate the factorial value of a given integer.

#include<stdio.h>
main()
{
int rec(int );
int n, fact;
printf(“Enter an integer to find its factorial value:”);
scanf(“%d”, fact);
fact=rec(n);
printf(“Factorial value=%d”, fact)
}
int rec(int x)
{
int f;
if(x==1)
return(1);
else
f=x*rec(x 1);
return(f);
}