Functions in C: 1. What Is C Function?
Functions in C: 1. What Is C Function?
C functions are basic building blocks in a program. All C programs are written using
functions to improve re-usability, understandability and to keep track on them. You can learn
below concepts of C functions in this section in detail.
1. What is C function?
A large C program is divided into basic building blocks called C function. C function
contains set of instructions enclosed by “{ }” which performs specific operation in a C
program. Actually, Collection of these functions creates a C program.
2. Uses of C functions:
• C functions are used to avoid rewriting same logic/code again and again in a program.
• There is no limit in calling C functions to make use of same functionality wherever
required.
• We can call functions any number of times in a program and from any place in a
program.
• A large C program can easily be tracked when it is divided into functions.
• The core concept of C functions are, re-usability, dividing a big task into small pieces
to achieve the functionality and to improve understandability of very large C
programs.
• Function declaration or prototype – This informs compiler about the function name,
function parameters and return value’s data type.
• Function call – This calls the actual function
• Function definition – This contains all the statements to be executed.
• As you know, functions should be declared and defined before calling in a C program.
• In the below program, function “square” is called from main function.
• The value of “m” is passed as argument to the function “square”. This value is
multiplied by itself in this function and multiplied value “p” is returned to main
function from function “square”.
include<stdio.h>
// function prototype, also called function declaration
float square ( float x );
// main function, program starts from here
int main( )
{
float m, n ;
printf ( "\nEnter some number for finding square \n");
scanf ( "%f", &m ) ;
// function call
n = square ( m ) ;
printf ( "\nSquare of the given number %f is %f",m,n );
}
1. Call by value
2. Call by reference
1. Call by value:
• In call by value method, the value of the variable is passed to the function as
parameter.
• The value of the actual parameter can not be modified by formal parameter.
• Different Memory is allocated for both actual and formal parameters. Because, value
of actual parameter is copied to formal parameter.
Note:
• In this program, the values of the variables “m” and “n” are passed to the function
“swap”.
• These values are copied to formal parameters “a” and “b” in swap function and used.
1
#include<stdio.h>
2
// function prototype, also called function declaration
3
void swap(int a, int b);
4
5
int main()
6
{
7
int m = 22, n = 44;
8
// calling swap function by value
9
printf(" values before swap m = %d \nand n = %d", m, n);
10
swap(m, n);
11
}
12
13
void swap(int a, int b)
14
{
15
int tmp;
16
tmp = a;
17
a = b;
18
b = tmp;
19
printf(" \nvalues after swap m = %d\n and n = %d", a, b);
20
Output:
2. Call by reference:
• In call by reference method, the address of the variable is passed to the function as
parameter.
• The value of the actual parameter can be modified by formal parameter.
• Same memory is used for both actual and formal parameters since only address is
used by both parameters.
• In this program, the address of the variables “m” and “n” are passed to the function
“swap”.
• These values are not copied to formal parameters “a” and “b” in swap function.
• Because, they are just holding the address of those variables.
• This address is used to access and change the values of the variables.
1 #include<stdio.h>
2 // function prototype, also called function declaration
3 void swap(int *a, int *b);
4
5 int main()
6 {
7 int m = 22, n = 44;
8 // calling swap function by reference
9 printf("values before swap m = %d \n and n = %d",m,n);
10 swap(&m, &n);
11 }
12
13 void swap(int *a, int *b)
14 {
15 int tmp;
16 tmp = *a;
17 *a = *b;
18 *b = tmp;
19 printf("\n values after swap a = %d \nand b = %d", *a, *b);
20 }
Output:
All C functions can be called either with arguments or without arguments in a C program.
These functions may or may not return values to the calling function. Now, we will see
simple example C programs for each one of the below.
function definition:
4. Without arguments and with
int function( )
return values
{
statements;
return a;
}
Note:
• If the return data type of a function is “void”, then, it can’t return any values to the
calling function.
• If the return data type of the function is other than void such as “int, float, double etc”,
then, it can return values to the calling function.
In this program, integer, array and string are passed as arguments to the function. The return
type of this function is “int” and value of the variable “a” is returned from the function. The
values for array and string are modified inside the function itself.
1 #include<stdio.h>
2 #include<string.h>
3 int function(int, int[], char[]);
4
5 int main()
6 {
7 int i, a = 20;
8 int arr[5] = {10,20,30,40,50};
9 char str[30] = "\"fresh2refresh\"";
10
11 printf(" ***values before modification***\n");
12 printf("value of a is %d\n",a);
13
14 for (i=0;i<5;i++)
15 {
16 // Accessing each variable
17 printf("value of arr[%d] is %d\n",i,arr[i]);
18 }
19 printf("value of str is %s\n",str);
20 printf("\n ***values after modification***\n");
21 a= function(a, &arr[0], &str[0]);
22 printf("value of a is %d\n",a);
23 for (i=0;i<5;i++)
24 {
25 // Accessing each variable
26 printf("value of arr[%d] is %d\n",i,arr[i]);
27 }
28 printf("value of str is %s\n",str);
29 return 0;
30 }
31
32 int function(int a, int *arr, char *str)
33 {
34 int i;
35
36 a = a+20;
37 arr[0] = arr[0]+50;
38 arr[1] = arr[1]+50;
39 arr[2] = arr[2]+50;
40 arr[3] = arr[3]+50;
41 arr[4] = arr[4]+50;
42 strcpy(str,"\"modified string\"");
43
44 return a;
45
46 }
In this program, integer, array and string are passed as arguments to the function. The return
type of this function is “void” and no values can be returned from the function. All the values
of integer, array and string are manipulated and displayed inside the function itself.
1 #include<stdio.h>
2
3 void function(int, int[], char[]);
4
5 int main()
6 {
7 int a = 20;
8 int arr[5] = {10,20,30,40,50};
9 char str[30] = "\"fresh2refresh\"";
10
11 function(a, &arr[0], &str[0]);
12 return 0;
13 }
14
15 void function(int a, int *arr, char *str)
16 {
17 int i;
18 printf("value of a is %d\n\n",a);
19 for (i=0;i<5;i++)
20 {
21 // Accessing each variable
22 printf("value of arr[%d] is %d\n",i,arr[i]);
23 }
24 printf("\nvalue of str is %s\n",str);
25 }
COMPILE & RUN
Output:
value of a is 20
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50
In this program, no values are passed to the function “test” and no values are returned from
this function to main function.
1 #include<stdio.h>
2 void test();
3
4 int main()
5 {
6 test();
7 return 0;
8 }
9
10 void test()
11 {
12 int a = 50, b = 80;
13 printf("\nvalues : a = %d and b = %d", a, b);
14 }
Output:
values : a = 50 and b = 80
In this program, no arguments are passed to the function “sum”. But, values are returned from
this function to main function. Values of the variable a and b are summed up in the function
“sum” and the sum of these value is returned to the main function.
1 #include<stdio.h>
2
3 int sum();
4
5 int main()
6 {
7 int addition;
8 addition = sum();
9 printf("\nSum of two given values = %d", addition);
10 return 0;
11 }
12
13 int sum()
14 {
15 int a = 50, b = 80, sum;
16 sum = a + b;
17 return sum;
18 }
Output:
• If you want to check source code for all header files, you can check inside “include”
directory after C compiler is installed in your machine.
• For example, if you install DevC++ compiler in C directory in your machine,
“C:\Dev-Cpp\include” is the path where all header files will be available.
• Check the below table to know all the C library functions and header files in which
they are declared.
• Click on the each header file name below to know the list of inbuilt functions declared
inside them.
string.h All string related functions are defined in this header file
math.h All maths related functions are defined in this header file
time.h This header file contains time and clock related functions
ctype.h All character handling functions are defined in this header file
stdarg.h Variable argument functions are declared in this header file