Functions in C Programming



Let us suppose that we want to write a program that will perform all the operations required for banking. So we will have to write a program that will have following steps:
• Accept account number and other personal details of the customer as a input
• Accept the details of the transactions to be performed
• If the customer wants to withdraw the money, then program should fetch the balance amount and perform eligibility check.
• After performing eligibility check, the money is deducted from that person’s account and the details of the transaction are updated in the database.
If we write the code for the above mentioned steps, it becomes very lengthy code. The solution to get rid of such lengthy code is the use of functions.
A function is a self contained block of statements that perform a particular task. For the above example we can write following functions:
• Accept the personal details from the customer
• Verify the details
• Withdraw the amount
Thus, function increases the readability of the program. Also, in case if we need to modify particular part of the program it becomes very easy to identify that part and modify it.

Example 1:

void main(){
fnCallingFunction1();
}
void fnCallingFunction1(){
printf(“You are in fnCallingFunction1”);
}
Output:
You are in fnCallingFunction1

As we all know, the execution of the program always starts from the main() function of the program.
Inside the main() function there is a function call to function fnCallingFunction1(). When the statement fnCallingFunction1() is encountered, the control is transferred to the function fnCallingFunction1 written just after the main() function. Now, all the statements written inside the fnCallingFunction1() is executed. There is a printf statement inside the body of fnCallingFunction1(). Hence the output “You are in fnCallingFunction1”.

A function cannot be defined inside the body of another function.

Example:
float fnGetAverage(int, int); //function prototype
void main(){
int x,y;
float z;
printf(“\nEnter the values for x and y”);
scanf(“%d %d”, &x, &y);
z = fnGetAverage(x,y); //function call
printf(“\nThe average is %d”, z);
}
float fnGetAverage(int a, int b){ //function definition
float c;
c = (a+b)/2; //calculating average
return c; //returning the value
}

Here, the value of x and y is taken as input from the user. This value is then passed to the function fnGetAverage as shown below:
fnGetAverage(x,y);
Now, the control is transferred to the function definition. The value of x and y are taken in the variables a and b respectively. The average of a and b is performed in the function body and the result is stored in the variable c. This value is then returned back using the statement return c. Now the control is transferred to the main() function and the value is assigned to the variable z.

The variables x and y are called ‘actual arguments/actual parameters’ and the variables a and b are called ‘formal arguments/formal parameters’. We can pass n numbers of arguments. But the type, order and the number of arguments must be same.

In the above program, the value of x and y is not known to the function "fnGetAverage".

When return statement is encountered the control is transferred back to the calling program.
It returns the value present in the parentheses or the variable.
Example:
return 8;
return (8);


Responses

0 Respones to "Functions in C Programming"

Post a Comment

 

Total Pageviews

Return to top of page Copyright © 2011 | Kuppam Engineering College Converted into Blogger Template by Mohan Murthy