0% found this document useful (0 votes)
335 views46 pages

CFP Unit5 2024-25

Uploaded by

NAMDEO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
335 views46 pages

CFP Unit5 2024-25

Uploaded by

NAMDEO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Sanjivani Rural Education Society’s

Sanjivani College of Engineering, Kopargaon


(An Autonomous Institute, Affiliated to Savitribai Phule Pune
University) NAAC ‘A’ Grade Accredited, ISO 9001:2005 Certified
Department of Information Technology
(NBA Accredited)

Computer Fundamentals and Programming (ESCO133)

Unit-5
Structure and Functions

Prepared by,

Prof. N. D. Kapale
Topics
•Structure : Definition, Declaration of Structure, Initialization,
Declaration of Structure Variables and Accessing Members.

•Functions: Definition, Prototyping,Function Call,Return


Statement. Call By Value and Call By Reference, Standard Library
Functions and User Defined Functions.
Structure-Concept
• Structure in C is a user-defined data type that enables us to store the
collection of different data types. Each element of a structure is called
a member.
• Structures are used to represent a record. Suppose you want to keep
track of your books in a library. You might want to track the
following attributes about each book −

• Title
• Author
• Subject
• Book ID
Defining a Structure
•The ,struct keyword is used to define the structure. Let's see
the syntax to define the structure in C.
struct structure_name
{
data_type
member1;
data_type
member2;
data_type
memeberN;
};
Keywords in C
•A keyword is a reserved word. You cannot use it as a variable
name, constant name, etc. There are only 32 reserved words
(keywords) in the C language.
•A list of 32 keywords in the c language is given below:
•Let's see the example to define a structure for an entity
employee in c.

struct employee
{ int id;
char name[20];
float salary;
};
Declaring structure variable
•We can declare a variable for the structure so that we can access
the member of the structure easily. There are two ways to declare
structure variable:

1.By struct keyword within main() function


2.By declaring a variable at the time of defining the structure.
•1st way:

struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
};

struct Student S1, S2; //declaring variables of struct Student


• 2nd way:

struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
}s1, s2; //declaring variables of struct Student
•Which approach is good

•If number of variables are not fixed, use the 1st approach. It
provides you the flexibility to declare the structure variable many
times.
•If no. of variables are fixed, use 2nd approach. It saves your code
to declare a variable in main() function.
•How to initialize structure members?

Structure members cannot be initialized with declaration.

struct Point
{
int x = 0; // COMPIL ERRO cannot initialize
here memb
here ER R:
ers
int y = 0; // COMPIL ERRO cannot initialize
memb
ers ER R:
•The reason for above error is simple, when a
datatype is declared, no memory is allocated for it.
Memory is allocated only when variables are
created.
Accessing members of the structure
•There are two ways to access structure members:

1. By . (member access or dot operator)


2. By -> (structure pointer operator)You can access elements of
an array by indices.
Program to access the structure member using structure
pointer and arrow (->) operator
C Functions
•A function is a block of statements that performs a specific task.
•Let’s say you are writing a C program and you need to perform a
same task in that program more than once. In such case you have
two options:
•a) Use the same set of statements every time you want to
perform the task.
b) Create a function to perform that task, and just call it every
time you need to perform that task.
•Using option (b) is a good practice and a good programmer
always uses functions while writing code in C.
•Why we need functions in C
Functions are used because of following reasons –
a) To improve the readability of code.
b) Improves the reusability of the code, same function can be
used in any program rather than writing the same code from
scratch.
c) Debugging of the code would be easier if you use functions,
as errors are easy to be traced.
d) Reduces the size of the code, duplicate set of statements
are replaced by function calls.
Types of functions

1) Predefined standard library functions


•Standard library functions are also known as built-in functions.
Functions such as puts(), gets(), printf(), scanf() etc are standard
library functions. These functions are already defined in header
files (files with .h extensions are called header files such as
stdio.h), so we just call them whenever there is a need to use them.

•For example, printf() function is defined in <stdio.h> header file so


in order to use the printf() function, we need to include the
<stdio.h> header file in our program using #include <stdio.h>.
2) User Defined functions
•The functions that we create in a program are known as user
defined functions or in other words you can say that a function
created by user is known as user defined function.
•Now we will learn how to create user defined functions and how
to use them in C Programming
• Defining a Function:

return_type function_name( parameter list )


{
body of the function
}
• A function definition in C programming consists of a function
header and a function body. Here are all the parts of a function −
• Return Type − A function may return a value. The return_type is the
data type of the value the function returns. Some functions perform the
desired operations without returning a value. In this case, the
return_type is the keyword void.
• Function Name − This is the actual name of the function. The function
name and the parameter list together constitute the function signature.
• Parameters − A parameter is like a placeholder. When a function is
invoked, you pass a value to the parameter. This value is referred to as
actual parameter or argument. The parameter list refers to the type,
order, and number of the parameters of a function. Parameters are
optional; that is, a function may contain no parameters.
• Function Body − The function body contains a collection of statements
that define what the function does.
Example
/* function returning the max between two numbers
*/ int max(int num1, int num2)
{
/* local variable declaration */ int
result;
if (num1 >
num2) result =
num1; else
result = num2;
return result;
}
Function Declarations
• A function declaration tells the compiler about a function name and
how to call the function. The actual body of the function can be
defined separately.
• A function declaration has the following parts −
return_type function_name( parameter list );
• For the above defined function max(), the function declaration is
as follows −
int max(int num1, int num2);
• Parameter names are not important in function declaration only
their type is required, so the following is also a valid declaration −
int max(int, int);
• Function declaration is required when you define a function in one
source file and you call that function in another file. In such case,
you should declare the function at the top of the file calling the
function.
Calling a Function

•While creating a C function, you give a definition of what the


function has to do. To use a function, you will have to call that
function to perform the defined task.
•When a program calls a function, the program control is
transferred to the called function. A called function performs a
defined task and when its return statement is executed or when its
function-ending closing brace is reached, it returns the program
control back to the main program.
•To call a function, you simply need to pass the required
parameters along with the function name, and if the function
returns a value, then you can store the returned value.
Function Arguments

•If a function is to use arguments, it must declare variables that


accept the values of the arguments. These variables are called
the formal parameters of the function.
•Formal parameters behave like other local variables inside the
function and are created upon entry into the function and
destroyed upon exit.
•While calling a function, there are two ways in which arguments
can be passed to a function −
Different aspects of function calling
•A function may or may not accept any argument. It may or may not
return any value. Based on these facts, There are four different
aspects of function calls.

1. function without arguments and without return value


2. function without arguments and with return value
3. function with arguments and without return value
4. function with arguments and with return value
Example for Function without argument and
without return value
Example for Function without argument and
with return value
Example for Function with argument and
without return value
Example for Function with argument and with
return value
Call by value and Call by reference in C
•There are two methods to pass the data into the
function in C language, i.e., call by value and call by
reference.
Call by value in C
•In call by value method, the value of the actual
parameters is copied into the formal parameters. In
other words, we can say that the value of the variable is
used in the function call in the call by value method.
•In call by value method, we can not modify the value of
the actual parameter by the formal parameter.
•In call by value, different memory is allocated for actual
and formal parameters since the value of the actual
parameter is copied into the formal parameter.
•The actual parameter is the argument which is used in
the function call whereas formal parameter is the
argument which is used in the function definition.
• Example of Call by Value

#include<stdio.h>
void print(int a)
{
printf("From print function ...\n");
printf("Address of a = %p\n",&a);
}
int main()
{
int a = 10;
printf("From Main Function ...\n");
printf("Address of a = %p\n",&a);
print(a);
return 0;
}
Call by reference in C
• In call by reference, the address of the variable is passed into the
function call as the actual parameter.
• The value of the actual parameters can be modified by changing the
formal parameters since the address of the actual parameters is passed.
• In call by reference, the memory allocation is similar for both formal
parameters and actual parameters. All the operations in the function
are performed on the value stored at the address of the actual
parameters, and the modified value gets stored at the same address
• In call by reference, we pass the address of a variable.
• So, if we modify the value, it will affect the original value of a variable.
• Example of Call by Reference

#include<stdio.h>
void print(int *a)
{
printf("From print function ...\n");
printf("Address of a = %p\n",a);
}
int main()
{
int a = 10;
printf("From Main Function ...\n");
printf("Address of a = %p\n",&a);
print(&a);
return 0;
}

You might also like