C Unit 4
C Unit 4
User defined functions: User can define one or more functions within a
program to perform special tasks. They are written for the requirement of
its creator. They contain the block of statements to perform a task. The
execution always starts from the main (), all other functions are invoked or called
from the main().
Need for user defined functions
User defined functions are used for the following reasons
1) To improve the readability of code.
2) Improves the reusability of the code.
3) Debygging of the code would be easier.
4) Reduces the overall complexity of the program.
General Syntax of Function Definition : A function definition consists of
a function header and function body. The function header contains function's
name, return type and parameters. The general form of function definition
is as follows
R_type function name (parameter list)
{
Local variable declaration;
Statement 1;
Statement n;
return (value computed);
}
R_Type🡪 the data type of the return value. If nothing is returned the data type
is void.
Parameter list 🡪 list of variables that receives value from calling function.
return 🡪 is a keyword used to return a value from sub function to calling
function.
Components of user defined functions: The user defined function has
multiple parts
Function declaration or prototype.
Function call.
Function definition.
Return statement.
Function prototype is the declaration of a function with return type, function
name and argument list. Function prototype is optional in c while
compulsory in c++.
R_type is the data type of the value the function returns. Some functions
perform the operations without returning a value then the return type is void.
0 1 1 2 3 5 8
Advantages of Recursion:
(i) Reduce unnecessary calling of function.
(ii) When the iterative solution is complex, recursion solve the problem in an easy
way
Program to find the factorial of the given number using recursive
function.
#include<stdio.h>
main()
{
int n;
printf(“Enter any nonnegative integer\n”); scanf(“%d”,&n);
printf(“Factorial of %d is = %d\n”,n, fact(n));
getch();
}
int fact(int n)
{
if(n==0)
return(1);
else
return(n*fact(n-1));
}
Program to generate N Fibonacci numbers using recursive function
#include<stdio.h>
main()
{
int i,n;
printf(“Enter number of elements \n”); scanf(“%d”,&n);
printf(“\nFibonacci number\n\n”);
for(i=1;i<=n;i++)
printf(“%d\t”,fibo(i));
getch();
}
int fibo(int k)
{
if(k==1)
return(0);
if(k==2)
return(1);
else
return(fibo(k-1)+fibo(k-2));
}
Types of functions based on passing parameters:
The communication between calling function and called function is done through
parameters. Based on passing parameter from calling function to called function,
there are 2 types of functions
1. Call by value (pass by value)
2. Call by address (pass by address)
Call by value: The process of calling the function by passing the value is
called call by value.
Program to illustrate the function using call by value.
#include<stdio.h>
void main()
{
int a=10,b=20;
clrscr();
printf( “Before interchange\n “a=%d\t b==%d\t”,a,b);
swap(a,b);
printf( “After interchange\n “a=%d\t b==%d\t”,a,b);
getch();
}
void swap(int x,int y)
{
int temp
temp=x;
x=y;
y=temp;
}
In call by value, any change made to the formal parameters will not change the
value of actual parameters.
Syntax
In C, we have three ways to pass an array as a parameter to the function. In the function
definition, use the following syntax:
( array_type array_name[size], ...);
return_type foo
Mentioning the size of the array is optional. So the syntax can be written as:
return_type foo ( array_type array_name[], ...);
In both of the above syntax, even though we are defining the argument as
array it will still be passed as a pointer. So we can also write the syntax as:
return_type foo( array_type* array_name, ...);
But passing an array to function results in array decay due to which the
array loses information about its size. It means that the size of the array or
the number of elements of the array cannot be determined anymore.
#include <stdio.h>
#include <stdlib.h>
{
printf("Size of arr[] in func(): %d bytes",
sizeof(arr));
// Drive code
int main()
int arr[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
sizeof(arr));
func(arr);
return 0;
Output
Size of arr[] in main(): 32bytes
Size of arr[] in func(): 8 bytes
As we can see,
The size of the arr[] in the main() function (where arr[] is declared) is 32 bytes which is =
sizeof(int) * 8 = 4 * 8 = 32 bytes.
But in the function where the arr[] is passed as a parameter, the size of arr[] is shown as
8 bytes (which is the size of a pointer in C).
It is due to the fact that the array decays into a pointer after being passed as a parameter.
One way to deal with this problem is to pass the size of the array as another parameter to
the function. This is demonstrated in the below example.
STRUCTURES
A structure is a meaningful collection of items of different data types. A structure
allows the user to create and manipulate a set of different types of data items.
The reserve word struct is used to construct structure. Structures are
considered as user-defined data types or derived data types or record types.
Note: Arrays are used to identify a group of similar data items using a common
name. Structures are used to identify a group of dissimilar data items using a
common name.
Definition and declaration of structure: The syntax of structure definition is
struct structure_name
{
type1 data1;
type2 data2;
……
type n data n;
};
The definition of structure does not create memory space in which values can be
stored. This is achieved by declaring the structure.
Syntax of structure declaration:
The syntax of structure declaration is
26 bytes
Program to illustrate the use of structures
#include<stdio.h>
main() enter the value of a,b and c
{ #
struct example 25
{ 15.7
char a; value of a=#
int b; value of b=25
float c; value of c=15.700000
};
struct example e;
clrscr();
printf(“enter the value of a,b and c\n”);
scanf(“%c%d%f”, &e.a,& e.b,&e.c);
printf(“ value of a=%c\n value of b=%d\n value of c=%f\n”, e.a,e.b,e.c);
getch()
}
main ()
{
struct student Enter the 2nd student name, age and fee
{ Raja 35 500.00
char name[20]; st
The 1 student details are
int age; Kumar 25 300.75
float fee; The 2nd student details are
};
Raja 35 500.00
struct student s1, s2;
s1.name=strcpy(name, “Kumar”);
s1.age=25;
s1.fee=300.75;
printf(“Enter the 2nd student name, age and fee”);
scanf(“%s%d%f”,s2.name,& s2.age,& s2.fee);
printf(“The 1st student details are\n”);
printf(“name=%s \t Age= %d\t Fees= %f ”,s1.name, s1.age, s1.fee);
printf(“The 2nd student details are\n”);
printf(“name=%s \t Age= %d\t Fees= %f ”, s2.name, s2.age, s2.fee);
getch()
}
Copying and Comparing Structure Variables
Two variables of the same structure type can be copied the same way as ordinary
variables.
If e1 and e2 belong to the same type, then the following statement is valid. e1 = e2, and
e2 = e1;
However, the statements that are shown here:
e1 < e2; and e1 != e2; are not permitted.
C language doesn't permit any logical operations on structure variables.
We can compare two structure variables but comparison of members of a structure can
only be done individually.
C program to compare the structure variables
datatype member1;
struct tagname{
datatype member2;
datatype member n;
};
Here,
struct is a keyword.
tagname specifies the name of structure.
member1, member2 specifies the data items that make up structure.
For example,
struct book{
int pages;
char author [30];
float price;
};
Structure variables
There are three methods of declaring structure variables, which are as follows −
First method
struct book{
int pages;
char author[30];
float price;
}b;
Second method
struct{
int pages;
char author[30];
float price;
}b;
Third method
struct book{
int pages;
char author[30];
float price;
};
struct book b;
Initialization and accessing of structures
The link between a member and a structure variable is established by using a member
operator (or) a dot operator.
First method
struct book{
int pages;
char author[30];
float price;
} b = {100, “balu”, 325.75};
Second method
struct book{
int pages;
char author[30];
float price;
};
struct book b = {100, “balu”, 325.75};
Third method by using a member operator
struct book{
int pages;
char author[30];
float price;
};
struct book b;
b. pages = 100;
strcpy (b.author, “balu”);
b.price = 325.75;
Example
Live Demo
struct class{
int number;
char name[20];
float marks;
};
main(){
int x;
struct class student1 = {001,"Hari",172.50};
struct class student2 = {002,"Bobby", 167.00};
struct class student3;
student3 = student2;
x = ((student3.number == student2.number) &&
(student3.marks == student2.marks)) ? 1 : 0;
if(x == 1){
printf("
student2 and student3 are same
");
printf("%d %s %f
", student3.number,
student3.name,
student3.marks);
}
else
printf("
student2 and student3 are different
");
}
Output
#include <stdio.h>
struct Point {
int x;
int y;
};
int main()
getchar();
return 0;
Program 2
#include <stdio.h>
struct Point {
int x;
int y;
};
int main()
// whole structures
getchar();
return 0;
****************