CPF Unit-4 Wordpress
CPF Unit-4 Wordpress
UNIT: 4
User-defined Functions
❖ Introduction to Functions:
Definition of Function: Function is a group of statement to perform a specific task.
Types of functions:
1. Library function
2. User-defined Function
1. By using functions, we can avoid rewriting same logic/code again and again in a program.
2. We can call C functions any number of times in a program and from any place in a program.
3. We can track a large C program easily when it is divided into multiple functions.
User-defined Function:
• The function which is develop by user itself is known as User-defined function.
• Example: main()
• User-defined function is created by the user at the time of writing of program.
Example:
int sum(int x, int y)
{
int total;
total = x + y;
return total;
}
• When a function is called using the value of variables, then it is known as call by value.
• The value of variables, which are to be passed, will be copied from the variables of the calling functions
to the variables of the called functions.
• All the process done on the duplicate variables rather then actual variables.
Example:
#include <stdio.h>
void swap(int , int); // function prototype
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b); // function call
printf("After swapping values in main a = %d, b = %d\n",a,b); //actual parameters a=10, b=20
}
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a=20, b=10
}
• When a function is called using the address of variables, then it is known as call by reference.
• Instead of passing the value of variables from calling function to the called function, addresses of the
variables are passed.
• All the process done on the actual variables.
Example:
#include <stdio.h>
void swap(int *, int *); // function prototype
void main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
❖ Recursion
function1();
}
function1()
{
function1();
Advantages of Recursion:
• The recursion is very flexible in data structure like stacks.
• Using recursion, the length of the program can be reduced.
Disadvantages of Recursion:
• It requires extra storage space.
• The recursion function is not efficient in execution speed and time.
Proper termination is required; otherwise it leads to infinite loop.
Block Scope:
void function()
{
int a = 5; // Block scope within function
{
int b = 10; // Block scope within nested block
}
// b is not accessible here
}
Function Scope:
File Scope:
Visibility
1. Global Variables:
o Declared outside of functions.
o Visible throughout the entire file and accessible from any function within that file.
o Use the extern keyword to access them from other files.
2. Static Variables:
o Declared with the static keyword.
o If declared outside a function, they are visible only within that file (internal linkage).
o If declared inside a function, they retain their value between function calls but are not visible
outside the function.
3. Local Variables:
o Declared within a function or a block.
o Visible only within that function or block.
o Not accessible outside the function or block.
Lifetime
2. Static Variables:
o Have a lifetime from the program's start to its end.
o Retain their value between function calls (for static local variables)
3. Global Variables:
o Declared outside of any function.
o Have a lifetime from program start to program termination.
o Exist for the entire duration of the program.