Do you know,
In this tutorial, I will explain all these points one-by-one with programming examples.
C language is a procedure-oriented programming language.
The program is divided into small blocks known as functions, which enhances the reusability and efficiency of the program.
Table of Contents
A function is a block that contains a well-organized set of instructions that are performed whenever the function is called.
Example:
The best example of a function is the ‘main()’ function.
What is special about the main function?
Other examples of functions would be printf()
, scanf()
, getch()
, etc.
These are the functions that are already defined by the compiler and are stored in libraries like stdio.h
and conio.h
.
However, we can always write user-defined functions as per our requirements.
There are two steps for establishing a function in C.
Function Declaration in C
Function declaration means declaring the function by stating its return type, function name, and parameters.
What is the return type in the function?
The return type specifies the nature of the value returned by the function. The function might return anything like integers, characters, decimals, etc.
The return type void is used whenever the function returns nothing.
The parameters are the list of arguments that are fed as input to the program.
On the other hand, a function declaration means writing the body of the function.
Function declaration syntax-
return_type function_name(parameters);
Function definition syntax-
return_type function_name(parameters) { //body of function; }
For example, let’s consider a simple program that uses a user-defined function called ‘sum’ which add two integers and prints the sum.
#include<stdio.h> void sum(int x, int y); int main() { int a1,b1,a2,b2,a3,b3; printf ("enter three pairs of inetegers"); scanf(“%d%d%d%d%d%d”,&a1,&b1,&a2,&b2,&a3,&b3); printf("sum of the first pair is:"); sum(a1,b1); printf("sum of the second pair is:"); sum(a2,b2); printf("sum of the third pair is:"); sum(a3,b3); return 0; } void sum(int x, int y) { int s; s=x=y; printf(“%d”,&s); }
Further, you can also classify the types of functions based on the number and types of input parameters or return type of the function.
In C programming, to execute a function, you need to call it.
In the case of a parameterized function, arguments need to pass while the function calls.
Now, there are two types of function calls–
In call by value, a copied set of values of arguments is passed to the function.
Hence, all the operations performed on variables do not affect the original set of arguments.
For example: Write a C program to swap the values of variables using Call by Value.
#include<stdio.h> void swap(int a, int b) int main() { int x=2,y=4; printf("Before swap:"); printf("x=%d, y=%d",x,y); swap(x,y) printf("After swap:"); printf("x=%d, y=%d", x,y); return 0; } void swap(int a, int b) { int temp; temp=a; a=b; b=temp; }
The above program passes the arguments x and y to the function called swap by a call-by-value method. The values of x and y are copied to a and b. Later the values of a and b are interchanged with the help of another integer variable called temp. First, the value of a is stored in temp, then the value of b is stored in a, and at last, the value of temp is stored in b.
In call by reference, instead of the copied set, the address of the variables is passed to the function. Hence, the operations performed to change the values in the original set.
Now, whenever a variable is declared or assigned some value, the C compiler allocates a location for it in the memory. The address of this memory cell is a number. So, the data type which can store this address is known as a pointer. While declaring a pointer, the special character ‘*’ is used.
For example:
int *p;
The asterisk means ‘value at the address’.
Hence, *p
means values at the address stored in p. Whereas, the & operator gives the address of a variable.
For example: Write a C program to swap the values of variables using Call by Reference or pointer.
Let’s see the swapping program but with the help of pointers this time. In this program, instead of passing the values of x and y, we pass the pointers which store the addresses of x and y.
#include<stdio.h> void swap(int *p, int *q); int main() { int x=2, y=4; printf("Before swap:"); printf("x=%d, y=%d",x,y); swap(&x,&y); printf("After swap:"); prinytf("x=%d, y=%d",x,y); } void swap(int *a, int *b) { int temp; temp=*a; *a = *b; *b = temp; }
There are many advantages of writing your program with functions. They are very useful as it makes your program more modular.
If you are working on any project, create a separate function for similar logical operations. This will reduce the complexity of your program and it will also be easy for debugging.
This is all about different types of functions in C and the difference between call by value and call by reference. I have also explained it with programming examples.
If you have any specific questions related to the functions in C, write in the comment.
Minor error…
Change
Built in Fucntions in C
to
Built in Functions in C
Thanks, Dale for the correction!
Updated.