Functions IP Notes
Functions IP Notes
Introduction
A function is a block of code that performs a specific task.
When solving a big problem, it is broken down into smaller sub-problems to make it
easier to manage.
In C programming, functions are used to break a large program into smaller,
manageable parts.
Every C program must have a function called main(). Without the main() function, the
program cannot run.
Functions advantages
Functions allow for modular programming.
Functions make the program more readable and easier to understand.
Functions make program implementation simpler.
Once created, a function can be used multiple times (code reusability).
Functions help in breaking large programs into smaller, manageable modules.
Types of Functions in C
In C Programming Language, based on providing the function definition, functions are divided
into two types. Those are as follows...
System Defined Functions
User Defined Functions
System Defined Functions
A system-defined function is a function whose definition is provided by the system.
These are also known as Library Functions, Standard Functions, or Pre-Defined
Functions.
Their implementation is provided in header files like stdio.h, math.h, etc.
Example: printf() and scanf() are in the stdio.h header file.
Example Program
#include<stdio.h>
#include<math.h>
int main()
{
float x=36;
double d1=3.0,d2=2.0;
printf("%f\n",sqrt(x));
printf("%lf\n",pow(d1,d2));
return 0;
}
User Defined Functions
A user-defined function is a function created by the user.
In C programming, users can create their own functions, called user-defined functions.
For example, the main() function is user-defined because the user writes it.
User-defined functions are classified based on data flow:
Example Program
#include <stdio.h>
void addition(); // Function declaration
int main()
{
addition(); // Calling function
return 0;
}
void addition()// Called function
{
int a, b;
printf("Enter any two integer numbers: ");
scanf("%d%d", &a, &b);
printf("Sum = %d\n", a + b);
}
Example Program
#include <stdio.h>
void addition(int a, int b); // Function declaration
int main()
{
int a, b; // Using variables a and b
printf("Enter any two integer numbers: ");
scanf("%d%d", &a, &b);
addition(a, b); // Calling function
return 0;
}
void addition(int a, int b) // Function definition
{
printf("Sum = %d\n", (a + b));
}
Function without Parameters and with Return value
In this type of function, there is no data transfer from the calling function to the called function
(parameters), but there is data transfer from the called function to the calling function (return
value). The program control jumps from the calling function to the called function, executes the
called function, and then returns to the calling function along with a return value.
Example Program
#include <stdio.h>
int addition(); // Function declaration
int main()
{
I nt result;
result = addition(); // Calling function
printf("Sum = %d\n", result);
return 0;
}
int addition()// Called function
{
int a, b;
printf("Enter any two integer numbers: ");
scanf("%d%d", &a, &b);
return (a + b); // Return the sum of a and b
}
Function with Parameters and with Return value
In this type of function, there is data transfer from the calling function to the called function
(parameters) as well as from the called function to the calling function (return value). The
program control jumps from the calling function to the called function along with the parameters,
executes the called function, and then returns to the calling function with a return value.
Example Program
#include <stdio.h>
int addition(int a, int b); // Function declaration
int main()
{
int a, b, result;
printf("Enter any two integer numbers: ");
scanf("%d%d", &a, &b);
result = addition(a, b); // Calling function
printf("Sum = %d\n", result);
return 0;
}
int addition(int a, int b) // Called function
{
return (a + b); // Return the sum of a and b
}
When a function is executed in a program, control transfers from the calling function to the
called function. This transfer may include data values, known as parameters. In C, there are two
types of parameters:
When a function is executed, copies of the actual parameter values are assigned to the formal
parameters.
1. Call by Value
In this method, a copy of the actual parameter values is made and used in the called
function.
Changes made to the formal parameters do not affect the actual parameters.
Example Program:
#include <stdio.h>
void swap(int a, int b); // Function declaration
int main()
{
int a = 10, b = 20;
printf("\nBefore swap in main: a=%d, b=%d", a, b);
swap(a, b); // Calling function
printf("\nAfter swap in main: a=%d, b=%d", a, b);
return 0;
}
void swap(int a, int b) // Called function
{
int temp;
printf("\nBefore swap in swap: a=%d, b=%d", a, b);
temp = a;
a = b;
b = temp;
printf("\nAfter swap in swap: a=%d, b=%d", a, b);
}
2. Call by Reference
In this method, the memory addresses of the actual parameters are passed to the called
function.
The formal parameters must be pointer variables, allowing direct access to the actual
parameters.
Changes made to the formal parameters will affect the actual parameters.
Example Program:
#include <stdio.h>
void swap(int *a, int *b); // Function declaration
int main()
{
int a = 10, b = 20; // Using variables a and b
printf("\nBefore swap in main: a=%d, b=%d", a, b);
swap(&a, &b); // Calling function
printf("\nAfter swap in main: a=%d, b=%d", a, b);
return 0;
}
void swap(int *a, int *b)
{
int temp;
printf("\nBefore swap in swap: a=%d, b=%d", *a, *b);
temp = *a; // Swap values using dereferencing
*a = *b;
*b = temp;
printf("\nAfter swap in swap: a=%d, b=%d", *a, *b);
}
Scope of Variables
The scope of a variable is the part of the program where it can be accessed. In C programming,
variables can be declared in two places:
1. Local Variables:
Example:
int add()
{
int a = 4; // Local variable
int b = 5; // Local variable
return a + b;
}
Global Variables:
Example:
int a = 4, b = 5; // Global variables
int add()
{
return (a + b);
}
Storage Classes
In the C programming language, storage classes are used to define things such as storage
location, scope, lifetime, and the default value of a variable.
In C, the memory for variables is allocated either in computer memory (RAM) or in CPU
registers. The allocation of memory depends on the storage class.
In C programming, there are four storage classes:
1. Automatic storage class
2. External storage class
3. Static storage class
4. Register storage class
Life time Exists only while the control remains within the block
Example:
#include <stdio.h>
void display()
{
auto int num = 10; // 'auto' is optional
printf("Inside display(): num = %d\n", num);
}
int main()
{
auto int a = 5;
printf("Inside main(): a = %d\n", a);
display();
return 0;
}
Register storage class
The register storage class allocates variable memory in CPU registers, not RAM.
Register variables provide faster access than other storage classes.
Only a limited number of register variables can be used due to the few CPU registers
available.
Property Description
Keyword register
Life time Exists only while the control remains within the block
Example:
#include <stdio.h>
int main()
{
register int count; // Register variable
for (count = 0; count < 3; count++)
{
printf("Count: %d\n", count);
}
return 0;
}
Property Description
Keyword extern
Example:
#include <stdio.h>
int count = 0; // Global variable
void increment()
{
count++;
}
int main()
{
printf("Count before: %d\n", count);
increment();
printf("Count after: %d\n", count);
return 0;
}
The static storage class creates variables that retain their value until the program ends.
Static variables are initialized only once but can be changed multiple times.
Property Description
Property Description
Keyword static
Example:
#include <stdio.h>
void countCalls()
{
static int count = 0;
count++;
printf("Function called %d times\n", count);
}
int main()
{
countCalls();
countCalls();
countCalls();
return 0;
}
Recursive Functions
The process in which a function calls itself directly or indirectly is called recursion and
the corresponding function is called as recursive function.
Using recursion technique, certain mathematical problems can be solved quite easily.
In recursive function, both calling function and called function are represented at one
place.
Syntax:
return_type function_name(parameters)
{
--------
--------
function_name(parameters);
}
Advantages:
Reduce unnecessary calling of function.
Through Recursion one can solve problems in easy way while its iterative solution is very
big and complex.
Program-1: /* Factorial Program using Recursion */
#include<stdio.h>
int factorial(int);
void main()
{
int n, fact;
printf("Enter any number:");
scanf("%d",&n);
fact =factorial(n);
printf("factorial of %d is: %d",n, fact);
}
int factorial(int n)
{
if(n==0)
return 1;
else if(n==1)
return 1;
else
return n*factorial(n-1);
}