Learn Functions in C in easy way – part 1

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

what-is-function-in-c-itvoyagers-1

#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

functions-in-c-itvoyagers-6

#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.

functions-in-c-itvoyagers-3

#4 Operation of sum() will be performed.

Program counter is on line 5 which end of function, now it will return to main program.

functions-in-c-itvoyagers-4

#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.

functions-in-c-itvoyagers-5

#6 Finally it will print value of rsl.

Program counter will move to line number 10.

functions-in-c-itvoyagers-6

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

PRACTICALS IN C PROGRAM

Programs to understand the basic data types and I/O.
Programs on Operators and Expressions.
Programs on decision statements.
Programs on looping.
Programs on arrays.
Programs on functions.
Programs on structures and unions.
Programs on pointers.
Programs on string manipulations.
Practice Programs
1 – C program for leap year, easy and simple code
2 – Best C program to find sum of all digits
3 – Best C program to check Palindrome Number
4 – Best C program to check prime number
5 – Best C program to print Fibonacci series
6 – Best C program to reverse a number
7 – Best C program to reverse an array
8 – Best C program to find average of array
9 – Best C program to find frequency of array element
10 – Best C program to sort array in ascending order
11 – Best C program to find factorial using recursion
12 – Best C program to find sum of all numbers in array
13 – Best C program to find largest element in array
14 – Best C program to swap two numbers without third variable
15 – C Program to check if a number is even or odd
16 – C program to find Simple interest
17 – Best C program to add two matrices
18 – Best C program to count vowels and consonants in a string
19 – Best C program to find LCM
20 – Best C program to find HCF
21 – Best C program to swap two numbers using pointers
22 – Best C program to add two numbers using pointers
23 – Best C program to print Multiplication Table
24 – Best C program to check Armstrong number
25 – Best C program to print half pyramid of star(*)
26 – Best C program to print half pyramid of numbers
27 – Best C program to print reverse half pyramid of star
28 – Best C program to print reverse half pyramid of number
More coming soon…
We are aiming to explain all concepts of C in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment