Functions in C
Let us understand the concept of functions in C language with the help of day to day scenario.
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.
What are C functions?
Functions are self contained block of statements that perform a particular task.
For the above example we can write following functions in C:
- Function to accept the personal details from the customer
- Function to verify the details
- Function to withdraw the amount
Thus, the C functions 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.
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”.
Note: A function in C 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);
Also see:
How does using functions in C make writing large programs easier?
Coding Standards for Writing Functions in C
How can a function return a constant value? If a constant value needs to be returned why to write a function??
Hi,
Function can return a constant value as shown in the example.
Sometime, we need to return a constant. In the C Programming we might return a constant 0 or 1 to indicate the successful execution of the program. If there is some kind of error we might return constant ‘0’ to the calling program to indicate there was some kind of error. Similarly there are various types of error and we can use ‘n’ number of constants to indicate the type of error.
This is just an example. we might want to return constant value based on the logic used for programming.
Thanks,
Learnconline.com
Sir,
In formal parameters can i take x and y instead of a and b.
Yes you can use x and y instead of a and b.
what is the need to use a function call,we can simply calculate the average in the main program itself,then what is the need of function call?
which is calling function and which is called function in programme?
printf(
is it possible to return more than one value from function ??? if yes then how ???
Yes is it possible to return two variables/values from a function??
No… its not possible to return more than one value from a function
how is it possible ?? if yes….
this example is too hard i can’t undrestand pls. do easy it..as like normal function declaration
couldn`t understand this page!
Unable to understand :/ plz make this easier
i don understand when to write the printf and scanf.WhY IN THE ABOVE PROGRAM THEY WRITTEN ONE AFTER ANOTHER?
when we run fnGetAverage() for x=2 and y=3, we get 2.000000. But why don’t we obtain 2.500000 (i mean the exact value) if we work with floats?
here x and y or the parameters passed in the function are declared as ”int” hence the result is will also in “int”[ (a+b)/2 ,getting ‘2’ only as they are int] after that you are assigning the value ‘2’ to ‘c’, which is float. so when you print ‘c’ it shows in float 2.000000.
If you want 2.500000. you should declare any one of the value (either x or y) as float.
this is very helpful to new c learners!!!!!
very helpful, everything explained very clearly. could you please explain the reason behind including the ‘function prototype’ . thank you
Please refer to the below link for more information:
https://www.learnconline.com/2010/03/prototype.html
Alternatively, you can refer to the below link as well:
http://www.learncpponline.com/function-prototypes-in-c-plus-plus/
Thanks,
LearnCOnline Team
@Marie Curie – great question. It seems that we need to ‘cast’ the integer result of the addition of (a + b) to be a float, before doing the division.
Like this: c = ((float)(a + b)) / 2;
You should then get 2.500000 when you are using “2 and 3”.
its necessary to take function ,argument & return datatypes as same, the problem seems to be solved. i.e if we take float instead of int in functn prototype
=>fnGetAverage(int, int); //function prototype
void main()
{
float x,y,z;
printf(
program is not working properly taking input from user but there is no output for average is showing .
Im using Dev c++ and when i use void main() i get this erro:
[Error] ‘::main’ must return ‘int’
but when i change it to int i get zero for the average please help
good
WAP for swapping of two arrays using a function
exactly why we should use function?? i am not getting will you please explain ??
Instead of calling function n all why we cant directly define our operation in main block itself??