Functions
Functions
◦ Functions are used to perform certain actions, and they are important
for reusing code: Define the code once, and use it many times.
Need for User-defined Functions
◦ The function can reduce the repetition of the same
statements in the program- Code reusability
◦ The function makes code readable by providing
modularity to our program.
◦ There is no fixed number of calling functions it can be
called as many times as you want.
◦ The function reduces the size of the program.
◦ Once the function is declared you can just use it
without thinking about the internal working of the
function.
Need for User-defined Functions
◦ Modular programming - Reduction in the amount of work and
development time - Program and function debugging is easier.
◦ Division of work is simplified due to the use of divide-and-
conquer principle.
◦ Reduction in size of the program due to code reusability
◦ Function can be access repeatedly without redevelopment,
which in turn promotes reuse of code.
◦ Library of function can be implemented by combining well
designed, tested and proven function.
Multi-Function Program
A program that uses more than one functions is called a multi-function program.
Int main()
{
int a;
printf(“Enter any number”);
scanf(“%d”,&a);
printf(“%d”,sizeof(a));
return 0;
}
Types of functions
◦ 1. pre-defined/library functions
◦ Function Declaration
◦ Function Definition
◦ Function Calls
Function Declarations
◦ In a function declaration, we must provide the function name, its return type, and the
number and type of its parameters.
◦ A function declaration tells the compiler that there is a function with the given
name defined somewhere else in the program.
◦ Syntax:return type name_of_the_function (parameter_1, parameter_2);
◦ The parameter name is not mandatory while declaring functions. We can also declare
the function without using the name of the data variables.
◦ Example
◦ int sum(int a, int b); // Function declaration with parameter names
◦ int sum(int , int); // Function declaration without parameter names
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).
◦ 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.
Return Values and their Types
int
void
float
char
pointer
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.
◦ We can pass arguments to the C function in two ways:
◦ Pass by Value
◦ Pass by Reference
1.pass by value
◦ Parameter passing in this method copies values from
actual parameters into formal function parameters.
◦ As a result, any changes made inside the functions do
not reflect in the caller’s parameters.
Example pass by value
#include <stdio.h> sum(var1, var2);
void sum(int var1, int var2) //Printf(“sum=%d”,sum); cant access sum in
{ main() function.
int sum=var1+var2; return 0;
printf(“Sum=%d”,sum);
}
}
O/P: Sum=5
// Driver code
int main()
{
int var1 = 3, var2 = 2;
Example pass by value
#include <stdio.h> printf("Before swap Value of var1 and var2
void swap(int var1, int var2) is: %d, %d\n",var1, var2);
{ swap(var1, var2);
int temp = var1; printf("After swap Value of var1 and var2
var1 = var2; is: %d, %d",var1, var2);
var2 = temp; return 0;
}
}
// Driver code
O/P: Before swap Value of var1 and var2
int main()
is: 3, 2
{
After swap Value of var1 and var2 is: 3, 2
int var1 = 3, var2 = 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 call by reference
// C program to show use of printf("Before swap Value of var1 and var2 is: %d, %d\n",
var1, var2);
// call by Reference
swap(&var1, &var2);
#include <stdio.h>
printf("After swap Value of var1 and var2 is: %d, %d",
var1, var2);
void swap(int *var1, int *var2)
return 0;
{
int temp = *var1; }
*var1 = *var2;
O/P:
*var2 = temp;
Before swap Value of var1 and var2 is: 3, 2
}
After swap Value of var1 and var2 is: 2, 3
// Driver code
int main()
{
int var1 = 3, var2 = 2;
Category of Functions:
1.No Arguments and no Return Values
{ return 0;
}
scanf("%d", &arr[i]);
} O/P:Enter no of elements in an array:5
} Enter elements in array: 9
◦ a-address
◦ *a-values at that address
Example on pointers
int main()
{
Int *a,b;
a=&b;
printf(“%d”,a); //will print 100
printf(“%d”,&a); // will print 30
printf(“%d”,*a); // will print 20
return 0;
}
You can go through…..
# include <stdio.h>
O/P:
int main()
-658081364
{ int b=10;
-658081376
int *a=&b;
10
int **c=&a;
printf("%d\n",a);
-658081376
printf("%d\n",&a);
-658081384
printf("%d\n\n",*a);
-658081364
printf("%d\n",c);
10
printf("%d\n",&c);
printf("%d\n",*c);
printf("%d\n",**c);
return 0;
}
Use of pointers
◦ Pointers in C are variables that store the memory address of another variable, which
allows for direct access to the data at that location.
◦ Passing large structures or arrays to a function: Pointers are useful when you need to
pass large structures or arrays to a function.
◦ Modifying a variable's value: Pointers allow a function to modify the value of a
variable.
◦ Iterating over elements in arrays or other data structures: Pointers are used to iterate
over elements in arrays or other data structures.
◦ Creating flexible and reusable code: Function pointers are used to create flexible and
reusable code.
◦ Dynamic memory allocation: The malloc() and free() functions are used to allocate
dynamic memory from the heap.
◦ Storing the address of another pointer: A pointer to a pointer, or double pointer, is used
to store the address of another pointer.
Example on call by reference
// C program to show use of printf("Before swap Values of var1 and var2 are: %d, %d\n",
var1, var2);
// call by Reference
swap(&var1, &var2);
#include <stdio.h>
printf("After swap Values of var1 and var2 are: %d, %d",
var1, var2);
void swap(int *v1, int *v2)
return 0;
{
int temp = *v1; }
*v1 = *v2;
O/P:
*v2 = temp;
Before swap Values of var1 and var2 are: 3, 2
}
After swap Values of var1 and var2 are: 2, 3
// Driver code
int main()
{
int var1 = 3, var2 = 2;
Recursion
◦ 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.
◦ Basic Structure of Recursive Functions
◦ type function_name (args) {
◦ // function statements
◦ // base condition
◦ // recursion case (recursive call)
◦}
Recursive function to find factorial of a number
#include <stdio.h> else if ( n == 1) //base case 2
int fact (int); {
int main() return 1;
{ }
int n,f; else
printf("Enter the number whose factorial {
you want to calculate?");
return n*fact(n-1); //recursive call
scanf("%d",&n);
}
f = fact(n); //function call
}
printf("factorial = %d",f);
}
O/P:
int fact(int n)
Enter the number whose factorial you want
{ to calculate?5
if (n==0) //base case 1 factorial = 120
{
return 1;
}
Recursive Function
◦ A recursive function performs the tasks by dividing it into
the subtasks. There is a termination condition defined in
the function which is satisfied by some specific subtask.
After this, the recursion stops and the final result is
returned from the function.