C Functions
C Functions
Function Declaration
Note: A function in C must always be declared globally
before calling it.
Function Definition
The function definition consists of actual statements which are
executed when the function is called (i.e. when the program
control comes to the function).
A C function is generally defined and declared in a single step
because the function definition always starts with the function
declaration so we do not need to declare it explicitly. The
below example serves as both a function definition and a
declaration.
return_type function_name (para1_type
para1_name, para2_type para2_name)
{
// body of the function
}
Function Definition in C
Function Call
A function call is a statement that instructs the compiler to
execute the function. We use the function name and
parameters in the function call.
In the below example, the first sum function is called and
10,30 are passed to the sum function. After the function call
sum of a and b is returned and control is also returned back to
the main function of the program.
Working of function in C
Note: Function call is neccessary to bring the program control
to the function definition. If not called, the function
statements will not be executed.
Example of C Function
// C program to show function
// call and definition
#include <stdio.h>
// Driver code
int main()
{
// Calling sum function and
// storing its value in add variable
int add = sum(10, 30);
Output
Sum is: 40
As we noticed, we have not used explicit function declaration.
We simply defined and called the function.
Function Return Type
Function return type tells what type of value is returned after
all function is executed. When we don’t want to return a
value, we can use the void data type.
Example:
int func(parameter_1,parameter_2);
The above function will return an integer value after running
statements inside the function.
Note: Only one value can be returned from a C function. To
return multiple values, we have to use pointers or structures.
Function Arguments
Function Arguments (also known as Function Parameters) are
the data that is passed to a function.
Example:
int function_name(int var1, int var2);
// Driver code
int main()
{
double Number;
Number = 49;
Output
The Square root of 49.00 = 7.00
// Driver code
int main()
{
int a = 30, b = 40;
// function call
int res = sum(a, b);
Output
Sum is: 70
Passing Parameters to Functions
The data passed when the function is being invoked is known
as the Actual parameters. In the below program, 10 and 30 are
known as actual parameters. Formal Parameters are the
variable and the data type as mentioned in the function
declaration. In the below program, a and b are known as formal
parameters.
// Driver code
int main()
{
int var1 = 3, var2 = 2;
printf("Before swap Value of var1 and
var2 is: %d, %d\n",
var1, var2);
swap(var1, var2);
printf("After swap Value of var1 and
var2 is: %d, %d",
var1, var2);
return 0;
}
Output
Before swap Value of var1 and var2 is: 3,
2
After swap Value of var1 and var2 is: 3,
2
2. Pass by Reference
The caller’s actual parameters and the function’s actual
parameters refer to the same locations, so any changes made
inside the function are reflected in the caller’s actual
parameters.
Example:
// C program to show use of
// call by Reference
#include <stdio.h>
// Driver code
int main()
{
int var1 = 3, var2 = 2;
printf("Before swap Value of var1 and
var2 is: %d, %d\n",
var1, var2);
swap(&var1, &var2);
printf("After swap Value of var1 and
var2 is: %d, %d",
var1, var2);
return 0;
}
Output
Before swap Value of var1 and var2 is: 3,
2
After swap Value of var1 and var2 is: 2,
3
Advantages of Functions in C
Functions in C is a highly useful feature of C with many
advantages as mentioned below:
1. The function can reduce the repetition of the same
statements in the program.
2. The function makes code readable by providing
modularity to our program.
3. There is no fixed number of calling functions it can be
called as many times as you want.
4. The function reduces the size of the program.
5. Once the function is declared you can just use it without
thinking about the internal working of the function.
Disadvantages of Functions in C
The following are the major disadvantages of functions in C:
1. Cannot return multiple values.
2. Memory and time overhead due to stack frame allocation
and transfer of program control.
Conclusion
In this article, we discussed the following points about the
function as mentioned below:
1. The function is the block of code that can be reused as
many times as we want inside a program.
2. To use a function we need to call a function.
3. Function declaration includes function_name, return
type, and parameters.
4. Function definition includes the body of the function.
5. The function is of two types user-defined function and
library function.
6. In function, we can according to two types call by value
and call by reference according to the values passed.
What is Recursion in C?
First, let’s start with the recursion definition,
Recursion is the process of a function calling itself repeatedly
till the given condition is satisfied. A function that calls itself
directly or indirectly is called a recursive function and such
kind of function calls are called recursive calls.
In C, recursion is used to solve complex problems by breaking
them down into simpler sub-problems. We can solve large
numbers of problems using recursion in C. For example,
factorial of a number, generating Fibonacci series, generating
subsets, etc.
Let’s discuss some basic terminologies and fundamentals of
recursion before going into working and implementation.
Recursive Functions in C
In C, a function that calls itself is called Recursive Function.
The recursive functions contain a call to themselves
somewhere in the function body. Moreover, such functions
can contain multiple recursive calls.
Basic Structure of Recursive Functions
The basic syntax structure of the recursive functions is:
type function_name (args) {
// function statements
// base condition
// recursion case (recursive call)
}
Basic Structure of Recursive Function in C
Result:
Element found at index : 3
where
ptr is the name of the pointer.
2. Pointer Initialization
3. Pointer Dereferencing
1. Pointer Declaration
In pointer declaration, we only declare the pointer but do not
initialize it. To declare a pointer, we use the ( * ) dereference
operator before its name.
Example
int *ptr;
The pointer declared here will point to some random memory
address as it is not initialized. Such pointers are called wild
pointers.
2. Pointer Initialization
Pointer initialization is the process where we assign some
initial value to the pointer variable. We generally use the ( & )
addressof operator to get the memory address of a variable
and then store it in the pointer variable.
Example
int var = 10;
int * ptr;
ptr = &var;
void geeks()
{
int var = 10;
// Driver program
int main()
{
geeks();
return 0;
}
Output
Value at ptr = 0x7ffca84068dc
Value at var = 10
Value at *ptr = 10
Types of Pointers in C
Pointers in C can be classified into many different types based
on the parameter on which we are defining their types. If we
consider the type of variable stored in the memory location
pointed by the pointer, then the pointers can be classified into
the following types:
1. Integer Pointers
As the name suggests, these are the pointers that point to the
integer values.
Syntax
int *ptr;
These pointers are pronounced as Pointer to Integer.
Similarly, a pointer can point to any primitive data type. It can
point also point to derived data types such as arrays and user-
defined data types such as structures.
2. Array Pointer
Pointers and Array are closely related to each other. Even the
array name is the pointer to its first element. They are also
known as Pointer to Arrays. We can create a pointer to an
array using the given syntax.
Syntax
char *ptr = &array_name;
Pointer to Arrays exhibits some interesting properties which
we discussed later in this article.
3. Structure Pointer
The pointer pointing to the structure type is called Structure
Pointer or Pointer to Structure. It can be declared in the same
way as we declare the other primitive data types.
Syntax
struct struct_name *ptr;
In C, structure pointers are used in data structures such as
linked lists, trees, etc.
4. Function Pointers
Function pointers point to the functions. They are different
from the rest of the pointers in the sense that instead of
pointing to the data, they point to the code. Let’s consider a
function prototype – int func (int, char), the function pointer
for this function will be
Syntax
int (*ptr)(int, char);
Note: The syntax of the function pointers changes according
to the function prototype.
5. Double Pointers
In C language, we can define a pointer that stores the memory
address of another pointer. Such pointers are called double-
pointers or pointers-to-pointer. Instead of pointing to a data
value, they point to another pointer.
Syntax
datatype ** pointer_name;
9. Constant Pointers
In constant pointers, the memory address stored inside the
pointer is constant and cannot be modified once it is defined.
It will always point to the same memory address.
Syntax
data_type * const pointer_name;
// dummy structure
struct str {
};
// dummy function
void func(int a, int b){};
int main()
{
// dummy variables definitions
int a = 10;
char c = 'G';
struct str x;
// printing sizes
printf("Size of Integer Pointer
\t:\t%d bytes\n",
sizeof(ptr_int));
printf("Size of Character
Pointer\t:\t%d bytes\n",
sizeof(ptr_char));
printf("Size of Structure
Pointer\t:\t%d bytes\n",
sizeof(ptr_str));
printf("Size of Function
Pointer\t:\t%d bytes\n",
sizeof(ptr_func));
printf("Size of NULL Void
Pointer\t:\t%d bytes",
sizeof(ptr_vn));
return 0;
}
Output
Size of Integer Pointer : 8 bytes
Size of Character Pointer : 8 bytes
Size of Structure Pointer : 8 bytes
Size of Function Pointer : 8 bytes
Size of NULL Void Pointer : 8 bytes
As we can see, no matter what the type of pointer it is, the size
of each and every pointer is the same.
Now, one may wonder that if the size of all the pointers is the
same, then why do we need to declare the pointer type in the
declaration? The type declaration is needed in the pointer
for dereferencing and pointer arithmetic purposes.
C Pointer Arithmetic
The Pointer Arithmetic refers to the legal or valid arithmetic
operations that can be performed on a pointer. It is slightly
different from the ones that we generally use for mathematical
calculations as only a limited set of operations can be
performed on pointers. These operations include:
Increment in a Pointer
Decrement in a Pointer
#include <stdio.h>
int main()
{
// Declare an array
int v[3] = { 10, 100, 200 };
Output
Value of *ptr = 10
Value of ptr = 0x7ffcfe7a77a0
void geeks()
{
// Declare an array
int val[3] = { 5, 10, 15 };
return;
}
// Driver program
int main()
{
geeks();
return 0;
}
Output
Elements of the array are: 5, 10, 15
int main()
{
// defining array
int arr[5] = { 1, 2, 3, 4, 5 };
Output
1 2 3 4 5
This concept is not limited to the one-dimensional array, we
can refer to a multidimensional array element as well using
pointers.
To know more about pointers to an array, refer to this article –
Pointer to an Array
Uses of Pointers in C
The C pointer is a very powerful tool that is widely used in C
programming to perform various useful operations. It is used
to achieve the following functionalities in C:
1. Pass Arguments by Reference
Conclusion
In conclusion, pointers in C are very capable tools and
provide C language with its distinguishing features, such as
low-level memory access, referencing, etc. But as powerful as
they are, they should be used with responsibility as they are
one of the most vulnerable parts of the language.