---Advertisement---
Learn C

Learn C Language : Functions

By Smart Answer

Updated on:

---Advertisement---

What is a Function

A function is a self-contained block of statements that perform a coherent task of some kind.Every c program can be thought of as a collection of these functions.As we noted earlier, using a function is something like hiring a person to do a specific job for you.Sometimes the interaction with this person is very simple; sometimes it’s complex.

Syntax of function:

type function_name (arg1, arg2, arg3)
{
statement1;
statement2;
statement3;
statement4;
}

A simple program which demonstrates the use of function:

main()
{
message();
printf(“message function is executed”);
}

void message()
{
printf(“Your in the message function”);
}

Output
Your in the message function
message function is executed

 

Here, through main() we are calling the function message().The control passes to the function message().The activity of main() is temporarily suspended.When the message() function is runs out of statements to execute, the control return to the main().Thus, main() becomes the ‘calling’ function, whereas message() becomes the ‘called’ function.

Use of Functions

Why write separate functions at all?
Why not squeeze the entire logic into one function, main()?

Two Reasons:

(1) Writing functions avoids rewriting the same code over and over.Suppose you have a section of code in your program that calculates area of triangle.If, later in the program, you want to calculate the area of different triangle, you won’t like it if you required to write the same instructions all over again.Instead, you would prefer to jump to a ‘section of code’ that calculates area and jump back to the place from where you left off.This section of code is nothing but a funtion.

(2) Using functions it become easier to write program and keep track of what they are doing.If the operation of a program can be divided into separate activities, and each activity placed in a different function, then each could be written and checked more or less independently.Separating the code into modular functions also makes the program easier to design and understand.

Call By Value

A simple program which demonstrate use of call by value:

/* Sending and receiving values between functions */

main()
{
int a,b,c,sum;
printf(“Enter any three numbers”);
scanf(“%d %d %d”,a,b,c);
sum = calsum(a, b,c);
printf(“Sum = %d”,sum);
}

int casum(int x, int y, int z)
{
int d;
d =x + y + z;
return d;
}

Output
Enter any three numbers 10 20 30
Sum = 60

 

In above program, in main() we receive the values of a,b and c through the keyword and then output the sum of a, b and c.However, the calculation of sum is done in a different function called calsum().If sum is to be calculated in calsum() and values of a, b and c are received in main(), then we must pass on these values to calsum(), and once calsum() calculates the sum we must return it from calsum() back to main().

There are a number of things to note about this program:

(1) In this program, from the function main() the values of a, b and c are passed on to the function calsum(), by making a call to the function calsum() and mentioning a, b and c in the parentheses.
(2) The variables a, b and c are called “actual arguments”, whereas the variables x, y and z are called “formal arguments”.Any number of arguments can be passed to a function being called.
(3) The moment closing brace (}) of the called function was encountered the control return to the calling function.No separate return statement was necessary to send back the control.

The return statement serve two purposes:

(1) On executing the return statement it immediately transfers the control back to the calling program.
(2) It returns the value present in the parentheses after return, to the calling function.In the above program the value of sum of three numbers is being returned.
(3) There is no restriction on the number of return statements that may be present in the function.Also, the return statement need not always be present at the end of the called function.

Call By Reference

Using a call by reference intelligently we can make a function return more than one value at a time.

A simple program which demonstrates the use of call by reference:

/* Calculate area and perimeter */

main()
{
int radius;
float area, perimeter;
printf(“ënter the radius of circle”);
scanf(“%d”,&radius);

      // Function Call
areaperi(radius,&area, &perimeter);

printf(“Area = %f”,area);
printf(“Perimeter = %f”,perimeter);
}

void areaperi(int r, float *a, float *p)
{
*a = 3.14*r*r;
*p = 1*3.14*r;
}

Output
enter the radius of a circle 5
Area = 78.500000
Perimeter = 31.400000;

 

Here, we are making mixed call, in the sense, we are passing the value of radius but, addresses of area and perimeter.And since we are passing the addresses, any change that we make in value stored at addresses contained in the variable a and p, would make the change effective in main().This is why when the control return 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 from a function at a time.

Recursion

In C, it is possible for the functions to call themselves.A function is called ‘recursive’ if a statement within the body of a function calls the same function.Sometimes called ‘circular defnition’, recursion is thus process of defining something in terms of itself.

A simple program which demonstrates the use of recursion function:

/* Calculate the factorial value */

main()
{
int fact, a;
printf(“Enter any number”);
scanf(“%d”,&a);

      // Function Call
fact = rec(a);

printf(“Factorial value = %d”,fact);
}

int rec(int x)
{
int f;
if(x == 1
return (1);
else
// Function calling itself
f = x * rec(x-1);

return (f);
}

Output
Enter any number 1
Factorial value = 1
Enter any number 2
Factorial value = 2
Enter any number 3
Factorial value = 6

 

Smart Answer

---Advertisement---

Related Post

Learn C Language : String

‹ previous What Is String The way a group of integer can be stored in an integer array, similarly a group of character can be stored in a ...

Learn C Language : Array

‹ previous Next › What are Arrays For understanding the arrays properly, consider the following program: main(){     int x;      x = 5;      x ...

Learn C Language : Data Type

‹ previous Next › Integer, long and short Sometimes, we know in advance that the value stored in a given integer variable will always be positive.When it is ...

Learn C Language : Switch Case Statement (Case Control Structure)

‹ previous Next › Switch Case Statement The control statement which allows us to make a decision from the number of choices is called a switch, or more ...

Leave a Comment