FOC Unit 4 _ 5 slides
FOC Unit 4 _ 5 slides
•A large C program is divided into basic building blocks called C function. C function contains set
of instructions enclosed by “{ }” which performs specific operation in a C program.
•There are many situations where we might need to write same line of code for more than once in a
program. This may lead to unnecessary repetition of code, bugs and even becomes boring for the
programmer. So, C language provides an approach in which you can declare and define a group of
statements once in the form of a function and it can be called and used whenever required.
•C allows you to define functions according to your need, These functions are known as user-
defined functions.
•Library functions are those functions which are already defined in C library,
example printf(), scanf(), strcat() etc. You just need to include appropriate header files to use these
functions. These are already declared and defined in C libraries.
•A User-defined functions on the other hand, are those functions which are defined by the user at
the time of writing program. These functions are made for code reusability and for saving time and
space.
•It makes your code reusable. You just have to call the function by its name to use it, wherever
required.
•In case of large programs with thousands of code lines, debugging and editing becomes easier if
you use functions.
•It is used to avoid rewriting same logic/code again and again in a program.
Function Declaration: Like variable declaration, a function declaration specifies the type of the
function.
Function Definition: A function needs to be defined in order to be used. It is the actual function that
we are going to work on.
Function parameters: They contain values or arguments that are sent by the calling function.
Function call: It is how we are going to call the function to get executed.
Department of Computer Science and Engineering
Function Declaration: •It is usually done at the top of the
program and indicates how the function
return_type function_name(parameters); shall be called.
• The actual function definition can be
A function declaration specifies: present anywhere in the program.
•The type of the function or return type,
•The function name and
•Its parameters.
•Function return type: It indicates what type of value the function will return. Eg: int, double, float, char, etc.
A function with void return type does not return any value.
•Function name: A function name can be anything the user decides. Usually, a function name is kept
according to the function it performs.
•Function Parameter: It contains arguments and may or may not take values from the calling function.
•Formal Parameter: The parameter which is written at the function definition is known as a formal
parameter.
•Actual Parameter: The parameter that is written at the function call is known as the actual parameter.
•When a function is declared to perform some sort of calculation or any operation and is expected to
provide with some result at the end, in such cases, a return statement is added at the end of function body.
•Return type specifies the type of value(int, float, char, double) that function is expected to return to
the program which called the function.
•Note: In case your function doesn't return any value, the return type would be void.
•A function may or may not return a result. But if it does, we must use the return statement to output the
result. return statement also ends the function execution, hence it must be the last statement of any
function. If you write any statement after the return statement, it won't be executed.
A function needs to be called by the calling function in order to get executed. There are two ways in which a
function can be called:
Call by reference: In this method, the address of the parameter or argument is put in the formal
parameter and then the values are accessed using the address in the actual parameter. Call by reference is
done as follows:
int sum(int *x,*y)// *x is the address which points to the value of the variable x stored in the address.
Call by value: In this method, the actual values are passed as an argument in the formal parameter and are
accessed in the actual parameter.
int sum(int x,int y) // x is the variable which contains the value so the value is directly passed to the
function
Note:- The difference between the above two is that, when we pass by reference then no new copy of the value is created i.e. the passed
variable is used and even if we do not return anything from the called function then also the changes will reflect inside the calling
function.
function call:
function ( a ); Function with argument
1. With arguments
and with return values function definition:
int function( int a )
{
statements;
return a; Function with return value
}
function call:
function( a ); Function with argument
2. With arguments and
Without return values
function definition:
void function( int a )
{
statements; Function without return value
}
function declaration:
void function();
function call:
function(); Function without argument
3. Without arguments and
without return values
function definition:
void function()
{
statements; Function without return value
}
function declaration:
int function ( );
function call:
function ( ); Function without argument
4. Without arguments and
With return values function definition:
int function( )
{
statements;
return a; Function with return value
}
•The lifetime of a variable is the period of time in which the variable is allocated a space (i.e., the
period of time for which it “lives”).
The scope of a variable is the part of the program within which the variable can be used. So, the
scope describes the visibility of an identifier within the program.
The lifetime of a variable or function is the time duration for which memory is allocated to store it, and
when that memory is released. It also refered as extent of a variable.
•A variable declared outside all functions is located into the global scope.
•A variable declared inside a block (part of code enclosed in curly brackets) belongs to the local
scope.
•Such a variable is not visible (and therefore not available) outside the block, in which it is declared.
•The most common case of local declaration is a variable declared within a function. A variable
declared locally, is located on the stack, and the lifetime of such a variable is equal to the lifetime of
the function.
Register variables: belong to the register storage class and are stored in the CPU registers.
The scope of the register variables is local to the block in which the variables are defined.
The variables which are used for more number of times in a program are declared as register
variables for faster access.
void main()
{
number = 10;
printf("I am in main function. My value is %d\n", number);
fun1();
fun2(); /* This is function 1 */
fun2()
}
{
/* This is function 1 */ printf("\nI am in function fun2.
fun1() My value is %d", number);
{
number = 20; // local variable }
printf("I am in function fun1.
My value is %d", number); I am in function main. My value is 10
I am in function fun1. My value is 20
} I am in function fun2. My value is 20
#include<stdio.h> #include<stdio.h>
} }
OUTPUT: 1 2 3
•A storage class represents the visibility and a location of a variable. It tells from what part of code we can
access a variable.
•There are total four types of standard storage classes. The table below represents the storage classes in 'C'.
•Arrays allow to define type of variables that can hold several data items of the same kind.
Similarly structure is another user defined data type available in C that allows to combine data items
of different kinds.
•struct keyword is used to declare the structure in C.
•Variables inside the structure are called members of the structure.
Syntax: Examples:
struct struct_name struct employee struct student
{ { {
DataType member1_name; char name[50]; char name[60];
DataType member2_name; DataType int age; int roll_no;
member3_name; float salary; float marks;
… }; }
};
Declaring Structure Variables
Example1 :
struct student Name of the structure
{
char name[60];
int roll_no; members of the structure
float marks;
Declaring Structure variables
} s1,s2,s3….sn; s1,s2,s3….sn
at the time of definition
Example 2:
#include <stdio.h>
void main()
{
struct student
struct student s1,s2,s3........sn; // Declaring Structure variables
{ within a main function.
char name[60];
int roll_no;
float marks;
}; }
Defining a structure called “ student ” before the main function
Accessing Structure Members
How to access data members of a structure using a struct variable?
/*Assigning the values of each struct member here*/ // Accessing members of point p1
p1.x = 20;
s1. name = “ramesh”; P2.y=10;
s1. roll_no = 101; printf ("x = %d, y = %d", p1.x, p1.y);
s1. marks = 25.0
return 0;
int main()
#include <stdio.h> {
printf("Enter the following information:\n");
struct student
{ printf("Enter student name: ");
char name[60]; scanf( “%s”, s1.name)
int roll_no;
float marks; printf("Enter student roll number: ");
} s1; scanf("%d", & s1. roll_no);
struct Point
{
int x, y, z;
};
int main()
{
// Examples of initialization
int main()
{
struct student s1 = { “ramesh”, 101, 23 }; Structure member (S1 and S2)
struct student s2 = { “suresh”, 102, 27 }; initialization
printf ("x = %s, y = %d, z = %f\n", s1.name, s1.roll_no, s1.marks);
printf ("x = %s, y = %d, z = %f\n", s2.name, s2.roll_no, s2.marks);
}
Copying and Comparing Structure variables
struct Employee
{
int age;
char name[10];
int salary;
} Employees[4] = { {25, "Suresh", 25000}, {24, "Tutorial", 28000}, {22, "Gateway", 35000}, {27, "Mike", 20000} };
}
struct student
{ Structure Definition
int id;
char name[30];
float percentage;
int main() };
{
int i;
struct student record[2]; Array of Structure, Declaration
} }
Thank you