C UNIT 4 Notes
C UNIT 4 Notes
C UNIT 4 Notes
Function:
Types of function:
Function call :Function can be called from anywhere in the program. The
parameter list must not differ in function calling and function declaration.
We must pass the same number of functions as it is declared in the function
declaration.
function body
User-defined functions:
Functions that are created by the user are known as user-defined functions.
#include <stdio.h>
void functionName()
... .. ...
... .. ...
int main()
... .. ...
... .. ...
functionName();
... .. ...
... .. ...
A function may or may not accept any argument. It may or may not return any
value. Based on these facts,
#include<stdio.h>
void sum();
void main()
{
printf("\nGoing to calculate the sum of two numbers:");
sum();
}
void sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
printf("The sum is %d",a+b);
}
Output
Going to calculate the sum of two numbers:
The sum is 34
A function can return a value without accepting any arguments. Here’s an example
for calculating and returning the area of a rectangle without taking any argument.
#include<stdio.h>
int sum();
void main()
{
printf("Going to calculate the area of the square\n");
float area = square();
printf("The area of the square: %f\n",area);
}
int square()
{
float side;
printf("Enter the length of the side in meters: ");
scanf("%f",&side);
return side * side;
}
Output
The sum is 34
#include<stdio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\nThe sum is : %d",result);
}
int sum(int a, int b)
{
return a+b;
}
Output
#include<stdio.h>
void sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b);
}
void sum(int a, int b)
{
printf("\nThe sum is %d",a+b);
}
Output
The sum is 34
Argument Parameter
Arguments are used while calling Parameters are used during the
the function declaration of the function
Function Arguments:
● A function’s arguments are used to receive the necessary values by the
function call.
● Functions can be invoked in two ways: Call by Value or Call by Reference.
These two ways are generally differentiated by the type of values passed to
them as parameters.
● The parameters passed to function are called actual parameters whereas the
parameters received by function are called formal parameters.
Call by Reference: Both the actual and formal parameters refer to the same
locations, so any changes made inside the function are actually reflected in
actual parameters of the caller.
Difference between Call by Value and Call by Reference
In this method, the value of each In this method, the address of actual
variable in calling function is copied variables in the calling function are
into corresponding dummy variables of copied into the dummy variables of the
the called function. called function.
With this method, the changes made to With this method, using addresses we
the dummy variables in the called would have an access to the actual
function have no effect on the values of variables and hence we would be able to
actual variables in the calling function. manipulate them.
#include <stdio.h>
// Function Prototype
void swapx(int x, int y);
// Main function
int main()
{
int a = 10, b = 20;
// Pass by Values
swapx(a, b);
Output:
x=20 y=10
a=10 b=20
// Call by Reference
#include <stdio.h>
// Function Prototype
void swapx(int*, int*);
// Main function
int main()
{
int a = 10, b = 20;
// Pass reference
swapx(&a, &b);
return 0;
}
// Function to swap two variables
// by references
void swapx(int* x, int* y)
{
int t;
t = *x;
*x = *y;
*y = t;
Output:
x=20 y=10
a=20 b=10
Advantage of functions in C
There are the following advantages of C functions.
● By using functions, we can avoid rewriting same logic/code again and again
in a program.
● We can call C functions any number of times in a program and from any
place in a program.
● We can track a large C program easily when it is divided into multiple
functions.
● Reusability is the main achievement of C functions.
● However, Function calling is always a overhead in a C program.
RECURSION
● Recursion is the technique of making a function call itself.
● Recursion is a routine that calls itself again and again directly or indirectly.
There are two types of recursion in the C language namely, Direct
calling and Indirect calling. The calling refers to the recursive call.
● The recursion is possible in C language by using method and function. The
problems like the Tower of Hanoi, the Fibonacci series, and the nth
derivative can be solved using recursion. The recursion uses a stack to store
its calls in memory.
● The recursion contains two cases in its program body.
i. Base case: When you write a recursive method or function, it keeps calling
itself, so the base case is a specific condition in the function. When it is met,
it terminates the recursion. It is used to make sure that the program will
terminate. Otherwise, it goes into an infinite loop.
ii. Recursive case: The part of code inside the recursive function executed
repeatedly while calling the recursive function is known as the recursive
case.
Basic Syntax of Recursion
The syntax for recursion is :
void recursive_fun() //recursive function
{
Base_case; // Stopping Condition
int main()
{
The function call inside the main function is normal call, it calls
the recursive_fun() function inside which there is another function
call recursive_fun(); which is termed as recursive call and the
whole recursive_fun() function is recursive function. Base_case is the
stopping condition for the recursive function.
Types of Recursion in C
There are two types of recursion in the C language.
1. Direct Recursion
2. Indirect Recursion
1. Direct Recursion
Direct recursion in C occurs when a function calls itself directly from inside.
Such functions are also called direct recursive functions.
Following is the structure of direct recursion.
function_01()
{
//some code
function_01();
//some code
}
In the direct recursion structure, the function_01() executes, and from inside,
it calls itself recursively.
2. Indirect Recursion
Indirect recursion in C occurs when a function calls another function and if
this function calls the first function again. Such functions are also called
indirect recursive functions.
Following is the structure of indirect recursion.
function_01()
{
//some code
function_02();
}
function_02()
{
//some code
function_01();
}
Output:
Enter a digit for fibonacci series: 8
0 1 1 2 3 5 8 13
WHAT IS A POINTER?
DEFINITION:
A pointer is a variable that stores a memory address. Pointers are used to store the
addresses of other variables or memory items. Pointers are essential for dynamic
memory allocation.
INITIALIZATION:
Pointer initialization is nothing but assigning value to the pointer variable. It
contains the address of a variable of the same data type. The ampersand (&)
operator can be used to get the address of a variable.
Example:
#include <stdio.h>
int main ()
{
int var = 5;
int *ptr; // Pointer declaration
ptr = &var; //Pointer initialization
printf ("\n The value at address: %p is: %d\n", ptr, *ptr);
return 0;
}
Output:
The value at address: 0x7ffd33bd5134 is : 5
Fig: Explanation for pointer initialization and declaration
NULL Pointer:
POINTERS ARITHMETIC:
A pointer in c is an address, which is a numeric value. Therefore, you can perform
arithmetic operations on a pointer just as you can on a numeric value. There are
four arithmetic operators that can be used on pointers: ++, --, +, and –
There are only a few operations that are allowed to perform on Pointers in C
language. The operations are slightly different from the ones that we generally use
for mathematical calculations. The operations are:
1. Increment/Decrement of a Pointer
2. Addition of integer to a pointer
3. Subtraction of integer to a pointer
4. Subtracting two pointers of the same type
5. Comparison of pointers of the same type.
1. Increment/Decrement of a Pointer
Increment:
It is a condition that also comes under addition. When a pointer is incremented, it
increments by the number equal to the size of the data type for which it is a pointer.
When a pointer is incremented, it increments by the number equal to the size of the
data type for which it is a pointer.
For Example:
If an integer pointer that stores address 1000 is incremented, then it will
increment by 4(size of an int) and the new address it will points to 1004.
Decrement:
It is a condition that also comes under subtraction. When a pointer is decremented,
it decrements by the number equal to the size of the data type for which it is a
pointer.
For example:
When a pointer is added with a value, the value is first multiplied by the size of
data type and then added to the pointer.
The subtraction of two pointers is possible only when they have the same data
type. The result is generated by calculating the difference between the addresses of
the two pointers and calculating how many bits of data it is according to the pointer
data type. The subtraction of two pointers gives the increments between the two
pointers.
For Example:
Two integer pointers
say ptr1(address:1000) and ptr2(address:1004) are subtracted. The difference
between address is 4 bytes.
Since the size of int is 4 bytes, therefore the increment between ptr1 and ptr2 is
given by (4/4) = 1.
We can compare the two pointers by using the comparison operators in C. We can
implement this by using all operators in C >, >=, <, <=, ==,!=. It returns true for
the valid condition and returns false for the unsatisfied condition.
E.g.) E1[E2]=>*(E1+E2)
Example
#include<stdio.h>
void main()
{
int a[3]={10,15,20};
printf(“Elements are %d %d %d\n”, a[0],a[1],a[2]);
printf(“Elements are %d %d %d\n”, *(a+0),*(a+1),*(a+2);
}
Output:
Elements are 10 15 20
Elements are 10 15 20
It is declared as -: It is declared as -:
1.
*var_name; data_type var_name[size];