Functions in C

C - Functions

Functions in C : 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.

Example:

# include<stdio.h>
void message();
int main()
{

message();
printf(“Cry,and  you stop the monotony!\n”);
return  0;
}

void message()
{
printf (“Smiole,and the world smiles with you..\n”);

}

Why Use Functions


i. Writing a functions avoids rewriting the same code over and over.

ii. By using functions it becomes easier to write programs and keep track of what they are doing. If the operation of a program can be divided into seprate activies, and each activity placed in a different function, then each could be written and checked more or less independently. Separating code into modular functions also makes the program easier to design and understand.


Passing Values Between Functions :


The function that we have used so far haven’t been very flexible.We call  them and they do what they are designed to do.

The mechanism used to convey information to the function is the ‘argument’. You have unknowingly used the arguments in the printf() and scanf() functions; the format string and the list of variables used inside the parantheses in theses fuctions are arguments.These arguments are sometimes also called ‘parameters’.


Example:

# include<stdio.h>
int calsum(int x, int y, int z);
int 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\n”,sum);
return 0;
}

int calsum(int x, int y, int z)
{
int d;

d=x+y+z;
return (d);
}

Calling Convention       

Calling convention indicates two things:

a) The order in which the arguments are passed to the function.
b) Which function(calling function or called function) performs the cleanup of variables when the control returns from the function.

When a function call is encountered and the arguments are to be passed to a function two possibilities exists:

a) Arguments can be passed from left to right.
b) Arguments can be passed from right to left.

The formal arguments and the local variables defined inside a function are created at a place in a memory called ‘Stack’.When the control returns from the function the stack is cleanup.Either the calling function or the called function clears the stack. Which function would do this decided by the calling convention.

There are different calling convention available. The most common amongst them is a convention called standard calling convention. In this convention the arguments are passed to a function in right to left order and the stack is cleaned up by the called function.


Consider the following function call:

fun(a, b, c, d);

Return Type of function

# include <stdio.h>
int main()
{

float a, b;
printf(“Enter any number”);
scanf(“%f”,&a);
b=square(a);
printf(“Square of%f is %f\n,a,b”);
return 0;
}

float square(float x)
{
float y;
y=x*x;
return(y);
}

Call by Value and Call by Reference

By now, we are well familiar withhow to call functions. But, if you observe carefully, whenever we called a function and passes some thing to it we have always passed the values of the variables to the called function. Such function calls are called ‘calls by value’. By this what we mean is, on calling a function, we are passing values of variables to it.

sum=calsum(a, b, c);
f=factr(a);

Recommended Posts

Spread the love

Leave a Comment

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