Table of Contents
Functions in C
Let’s suppose we want to perform an additional operation on two variables and we write a code for it.
Now in further code again we want to add two numbers then we have to write the same code logic which we have already written.
To avoid such redundancy/repetition we can use the “functions”.
Functions are nothing but the block of code which is written separately and we can access this block of code throughout the program with the help of the function’s name.
We can divide the complete code into separate functions.
Function must be defined in such a way that they will perform one specific task.
There is at least one program in C and which is the most important function i.e. main() function.
In C programming we also have inbuilt functions like strings functions e.g.
strlen(), strcat(), etc.
C programming has its own libraries of function, libraries which includes input/output functions, math function, string function, etc.
Declaration of function in C
Following is a general syntax for function deceleration:
return_type function_name(parameter_list);
Return_type :
Many functions return a specific value or output of operation which it has performed.
We mention the data type of value which has to be returned by function and this is known as return type.
E.g. if the function is returning int value then the return type will be int.
Some functions will not return any value then the return type will be void.
Function_name :
This actual name of the function. We have to use it with parentheses to call a function.
Parameters :
Sometimes we pass some values to function as arguments and those values are collected by parameters.
Parameters are mentioned with their data type, at the time of declaration.
We can add a list of parameters even of different types.
Parameters specify which type of data and amount of data which function will accept.
E.g. int sum(int a, int b); // this function will accept 2 int values first value will be saved in parameter variable ‘a’ and second in parameter variable ‘b’.
Sometimes functions don’t require any parameter to perform its operations that time parameters are kept blank.
We can see that we have not defined the body for our function. Actually we can define it separately.
If we want to declare a function then first we must declare it before the main() function.
We can define function after the main() function in code but we have to declare the function before main().
We can also define complete function before main(), for this we will also assign body to function.
Syntax :
return_type function_name(parameter_list)
{
body_of_function;
}
Body_of_function :
Here we write logic or operation which has to be performed by function.
Calling a function in C
If we want to call a function then we have to write its name and if it accepts any arguments then we must pass arguments to it.
During execution once program control encounters a function call then the program counter will transfer to the called function.
After the function’s operation ends then the program counter will return back to the main program.
E.g. We can see the movement of the program counter in screenshots given below.
#1 In image below we can see that variable x and y are assigned garbage values.
Program counter is on line number 7

#2 In image below we can see that variable x and y are assigned with the value which specified.
Here x and y are pass in sum() function as a argument.
Program counter is on line number 8

#3 In image below we can see that parameter variable a and b are assigned with the value of x and y respectively.
Program counter moves to line number 4. i.e. it is transfer to the sum function.


#4 Operation of sum() will be performed.
Program counter is on line 5 which end of function, now it will return to main program.


#5 Program counter will return to main program.
We can see that value of variable rsl has been assigned.
Program counter will return to main program at line 9.


#6 Finally it will print value of rsl.
Program counter will move to line number 10.



Example of function in C
#include <stdio.h>
int sum(int a, int b) // a and b are parameters of the function
{
return a+b;
}
int main() {
int x=5,y=8,rsl;
rsl=sum(x,y); // here x and y are arguments of the function
printf("%d",rsl);
return 0;
}
Arguments and Parameter in function
Arguments :
Arguments are the values or variables which are passed in function parentheses when function is called.
E.g. in above example x and y are passed in function parentheses as arguments.
Parameters :
These act like local variables for the function.
Their task is to accept values from arguments and store that value so it can be used in function operation.
E.g. in above program a and b are parameters of the function which will accept values from x and y respectively.
Advantages of function in C
It avoids redundancy in the program.
Reduces the LOC(Line Of Code).
It will modularise the program which will help us in managing the code.
Help to reduce error count.
Divide large and complex problems into small and simple parts.
It increases the readability of the program.
CHECKOUT OTHER RELATED TOPICS
CHECKOUT OTHER QUIZZES
Quizzes on Operator |
Easy Quiz on Operators in C part 1 |
Easy quiz on operators in C part 2 |
Easy quiz on operators in C part 3 |
Quizzes on Loops |
Easy quiz on loops in C part 1 |
Easy quiz on loops in C part 2 |
Easy quiz on loops in C part 3 |
Quizzes on Decision Making Statement |
Easy Quiz On Decision Making Statements Part 1 |
Easy Quiz On Decision Making Statements Part 2 |
Quizzes on String |
Easy quiz on String in C programming part 1 |
Quizzes on Array |
Easy quiz on Array in C programming – part 1 |
5 – Best Java program to convert decimal to binary |
New Quizzes Coming soon.. |
PRACTICALS IN C PROGRAM

