Functions
• A function is a sub program, which performs a
particular task when called.
• A C program is a collection of some functions.
• main() is the predefined function from where the
execution of a program starts.
• C program = Main() + user defined functions
..continued
• Uses of functions:
• Re-useability
• Modularity
• Re-usability: C functions are used to avoid rewriting same code again and
again in a program.
• We can call functions any number of times in a program and from any place in
a program for the same task to be performed.
• Modularity: Dividing a big task into small pieces improves understandability
of very large C programs.
• A large C program can easily be tracked when it is divided into functions.
Defining a Function
• Syntax:
function definition function
declaration
return_type function_name(List of
parameters)
{
//set of statements
function body
..continued
• Function definition – This contains all the
statements to be executed.
Function definition = function declaration +function body
A function body is a group of statements enclosed
in flower brackets.
eg: void main() function declaration
{
int a=10; function definition
function body
printf(“a = %d”,a);
}
Function terminology
• Function prototype
• Function definition
• Function call
Function prototype or declaration - This informs
compiler about the function name, function
parameters and return value’s data type.
Syntax: return_type function_name(parameter list);
This is written before main() function. To inform the
compiler that there exists a function in the program.
Function Call
• This statement actually calls the function.
• Syntax: function_name( list of parameter
values);
• This statement is written in the calling
function.
• With this statement, the control is
transferred to the function definition.
• Eg: sum(10,20);
Function calls
• Functions can be defined in any one of the
following 4 different ways.
 No arguments , No return value .
 No arguments , With return value .
 With arguments , No return value .
 With arguments , With return value .
• Arguments:-
These values are passed from main()
function to the invoked function . Generally
Ex:- Program to print the sum of given two numbers using
functions?
i)No arguments , No return value
void add ( ) ; //function
prototype
void main()
{
add(); //function calling
}
void add() //function definition
{
int a,b,c;
ii)No arguments , With return value
int add ( ) ; //function prototype
void main()
{
printf(“nsum is=%d”,add()); //
function call
}
int add() ; //function definition
{
int a,b,c;
iii)With arguments , No return value
void add ( int , int ) ; //function
prototype
void main()
{
int a , b ;
printf(“enter any two numbers”);
scanf(“%d%d”,&a,&b);
add(a,b); //function call
}
iv)With arguments , With return value
int add ( int , int ) ; //function
prototype
void main()
{
int a , b ;
printf(“enter any two numbers”);
scanf(“%d%d”,&a,&b);
printf(“sum is=%d”,add(a,b));
//function call
Methods of Function calls
There are two methods
that a C function can
be called from a
program. They are,
Call by value Call by reference
Arguments
• Actual Argument: This is the argument
which is used in the function call.
– Eg: sum(a,b); // a and b are actual arguments
• Formal Argument: This is the argument
which is used in the function definition.
– Eg: void sum(int m, int n) //m and n are
formal arguments.
{
.
.
}
Call/pass by value
• In this method, the values only values of
actual arguments are passed into formal
arguments.
• Different Memory is allocated for both
actual and formal parameters.
• The value of the actual parameter can not
be modified by formal parameter.
Call/pass by reference
– In this method, the address of the variable, not value ,is
passed to the function as parameter.
– Same memory is used for both actual and formal
parameters since only address is used by both
parameters.
– The value of the actual parameter can be modified by
formal parameter.
Recursion
• It is possible in c for functions to call
themselves.
• Recursion is a technique in which a
function calls itself.
• Eg: int fact(int a) //function definition
{
int f;
if(a==1)
return 1;
else
f=a*fact(a-1); //function call
Scope of the variable
• Two kinds of variables: Local and Global
– Local - A variable declared within the function
is called as local variable. This can be used
within that function only.
• Eg: void main()
{
int x,y;
.
.
}
X and y are local variables. Their scope is upto main function.
They can be used in only main function.
Scope of the variables
• Global variable: A variable declared global
section of the program. This can be used
anywhere in the program.
• Eg: int x; // global variable
void main()
{
int y; // local variable
.
}
void sum()
{
int a,b,c; //local variables
.
C – Storage Class Specifiers
• Storage class specifiers tells the compiler
– where to store a variable,
– how to store the variable,
– what is the initial value of the variable and
– life time of the variable.
• Syntax: storage_specifier data_type
variable _name
• Types : 4
– auto
– extern
auto
• The scope of this auto variable is within
the function only.
• It is declared using auto keyword. It is
equivalent to local variable.
• All local variables are auto variables by
default.
extern
• The scope of the extern variable is global.
• It is available throughout the main
program.
• Is declared using the keyword extern. Can
be declared anywhere in the program.
• It is stored in memory.
static
• It’s scope is local. It’s value persists
between function calls.
• Declared using static keyword.
• Stored in memory.
• Default is zero.
register
• Scope is local.
• Available within the function.
• Stored in register memory.
• Default value is garbage value.
• Declared using register keyword.
…continued
S.No
.
Storage
Specifier
Storage
place
Initial /
default
value
Scop
e
Life
1 auto
CPU
Memory
Garbage
value
local Within the function only.
2 extern
CPU
memory
Zero Global
Till the end of the main program. Variable
definition might be anywhere in the C
program
3 static
CPU
memory
Zero local
Retains the value of the variable between
different function calls.
4 register
Register
memory
Garbage
value
local Within the function
Structures
• A structure is a collection of one or more
variables, usually of different types,
grouped together under a single name.
• A structure can be declared using the
keyword ‘struct’.
• Syntax: struct structure_name
{
Structure example
• structure example: struct book
{
char book_name[20];
char author[20];
int no_of_pages;
float price;
}
Structure members
• Each variable inside the structure
definition is called a structure member.
• In the book example above,
book_name , author, no_of_pages and
price are the struct members.
Structure variables
• A structure variable can be declared using
the structure name.
• Syntax:
struct struct_name
variable_name1,variable_name2,…;
• Example: struct book b1,b2,b3;
Accessing structure members
• Structure members can be accessed using
the dot(.) operator.
• Syntax : struct_variable . struct
member;
• Example : b1.book_name;
b2.book_name;
etc.,
Initializing structure members
• Intializing can be done in two ways:
1. using assignment operator (=)..
2. from the keyboard
1. Using the assignment operator:
eg: b1.no_of_pages=100;
b1. price=235;
2. From the keyboard:
eg: scanf(“%d”,&b1.no_of_pages);
Array of structures
• An array of structures can be declared in
the same way we declare any other array.
• Example: struct book b1[10];
b1 is the array of structures which
holds 10 structures.
• Each structure variable is accessed
using the array index.
Union
• A Union is a collection of one or more
variables, usually of different types,
grouped together under a single name.
• A union can be declared using the
keyword ‘union’.
• Syntax: union union_name
{
union example
• Union example: union book
{
char book_name[20];
char author[20];
int no_of_pages;
float price;
}
Union members
• Each variable inside the union definition is
called a union member.
• In the book example above,
book_name , author, no_of_pages and
price are the union members.
union variables
• A union variable can be declared using the
union name.
• Syntax:
union union_name
variable_name1,variable_name2,…;
• Example: union book b1,b2,b3;
Accessing union members
• union members can be accessed using
the dot(.) operator.
• Syntax : union_variable .union
member;
• Example : b1.book_name;
b2.book_name;
etc.,
Initializing union members
• Intializing can be done in two ways:
1. using assignment operator (=)..
2. from the keyboard
1. Using the assignment operator:
eg: b1.no_of_pages=100;
b1. price=235;
2. From the keyboard:
eg: scanf(“%d”,&b1.no_of_pages);
Array of unions
• An array of unions can be declared in the
same way we declare any other array.
• Example: union book b1[10];
b1 is the array of unions which
holds 10 unions.
• Each union variable is accessed using
the array index.
Differences between structure and
union
S.no C Structure C Union
1 Structure allocates storage space for all its members separately.
Union allocates one common storage space for all its
members.
Union finds that which of its member needs high storage
space over other members and allocates that much space
2 Structure occupies higher memory space. Union occupies lower memory space over structure.
3 We can access all members of structure at a time. We can access only one member of union at a time.
4
Structure example:
struct student
{
int mark;
char name[6];
double average;
};
Union example:
union student
{
int mark;
char name[6];
double average;
};
5
For above structure, memory allocation will be like
below.
int mark – 2B
char name[6] – 6B
double average – 8B
Total memory allocation = 2+6+8 = 16 Bytes
For above union, only 8 bytes of memory will be allocated
since double data type will occupy maximum space of
memory over other data types.
Total memory allocation = 8 Bytes
Pointers
• A pointer is a variable which points to
another variable.
• A pointer stores the address of another
variable.
• Pointers are used to allocate memory
dynamically.
Address of operator
• & is called as ‘addressof ‘operator.
• Helps to store the address of a variable in
a pointer.
• Then the pointer points to that variable.
• A pointer variable is default initalized to
null.
Advantages of pointers
• To return more than one value from a
function.
• To pass arguments to functions by
reference.
• Pointer concepts are very useful in
development of system software.
• With the address known, data can be
accesses from any where in the
program.

Functions in c

  • 1.
    Functions • A functionis a sub program, which performs a particular task when called. • A C program is a collection of some functions. • main() is the predefined function from where the execution of a program starts. • C program = Main() + user defined functions
  • 2.
    ..continued • Uses offunctions: • Re-useability • Modularity • Re-usability: C functions are used to avoid rewriting same code again and again in a program. • We can call functions any number of times in a program and from any place in a program for the same task to be performed. • Modularity: Dividing a big task into small pieces improves understandability of very large C programs. • A large C program can easily be tracked when it is divided into functions.
  • 3.
    Defining a Function •Syntax: function definition function declaration return_type function_name(List of parameters) { //set of statements function body
  • 4.
    ..continued • Function definition– This contains all the statements to be executed. Function definition = function declaration +function body A function body is a group of statements enclosed in flower brackets. eg: void main() function declaration { int a=10; function definition function body printf(“a = %d”,a); }
  • 5.
    Function terminology • Functionprototype • Function definition • Function call Function prototype or declaration - This informs compiler about the function name, function parameters and return value’s data type. Syntax: return_type function_name(parameter list); This is written before main() function. To inform the compiler that there exists a function in the program.
  • 6.
    Function Call • Thisstatement actually calls the function. • Syntax: function_name( list of parameter values); • This statement is written in the calling function. • With this statement, the control is transferred to the function definition. • Eg: sum(10,20);
  • 7.
    Function calls • Functionscan be defined in any one of the following 4 different ways.  No arguments , No return value .  No arguments , With return value .  With arguments , No return value .  With arguments , With return value . • Arguments:- These values are passed from main() function to the invoked function . Generally
  • 8.
    Ex:- Program toprint the sum of given two numbers using functions? i)No arguments , No return value void add ( ) ; //function prototype void main() { add(); //function calling } void add() //function definition { int a,b,c;
  • 9.
    ii)No arguments ,With return value int add ( ) ; //function prototype void main() { printf(“nsum is=%d”,add()); // function call } int add() ; //function definition { int a,b,c;
  • 10.
    iii)With arguments ,No return value void add ( int , int ) ; //function prototype void main() { int a , b ; printf(“enter any two numbers”); scanf(“%d%d”,&a,&b); add(a,b); //function call }
  • 11.
    iv)With arguments ,With return value int add ( int , int ) ; //function prototype void main() { int a , b ; printf(“enter any two numbers”); scanf(“%d%d”,&a,&b); printf(“sum is=%d”,add(a,b)); //function call
  • 12.
    Methods of Functioncalls There are two methods that a C function can be called from a program. They are, Call by value Call by reference
  • 13.
    Arguments • Actual Argument:This is the argument which is used in the function call. – Eg: sum(a,b); // a and b are actual arguments • Formal Argument: This is the argument which is used in the function definition. – Eg: void sum(int m, int n) //m and n are formal arguments. { . . }
  • 14.
    Call/pass by value •In this method, the values only values of actual arguments are passed into formal arguments. • Different Memory is allocated for both actual and formal parameters. • The value of the actual parameter can not be modified by formal parameter.
  • 15.
    Call/pass by reference –In this method, the address of the variable, not value ,is passed to the function as parameter. – Same memory is used for both actual and formal parameters since only address is used by both parameters. – The value of the actual parameter can be modified by formal parameter.
  • 16.
    Recursion • It ispossible in c for functions to call themselves. • Recursion is a technique in which a function calls itself. • Eg: int fact(int a) //function definition { int f; if(a==1) return 1; else f=a*fact(a-1); //function call
  • 17.
    Scope of thevariable • Two kinds of variables: Local and Global – Local - A variable declared within the function is called as local variable. This can be used within that function only. • Eg: void main() { int x,y; . . } X and y are local variables. Their scope is upto main function. They can be used in only main function.
  • 18.
    Scope of thevariables • Global variable: A variable declared global section of the program. This can be used anywhere in the program. • Eg: int x; // global variable void main() { int y; // local variable . } void sum() { int a,b,c; //local variables .
  • 19.
    C – StorageClass Specifiers • Storage class specifiers tells the compiler – where to store a variable, – how to store the variable, – what is the initial value of the variable and – life time of the variable. • Syntax: storage_specifier data_type variable _name • Types : 4 – auto – extern
  • 20.
    auto • The scopeof this auto variable is within the function only. • It is declared using auto keyword. It is equivalent to local variable. • All local variables are auto variables by default.
  • 21.
    extern • The scopeof the extern variable is global. • It is available throughout the main program. • Is declared using the keyword extern. Can be declared anywhere in the program. • It is stored in memory.
  • 22.
    static • It’s scopeis local. It’s value persists between function calls. • Declared using static keyword. • Stored in memory. • Default is zero.
  • 23.
    register • Scope islocal. • Available within the function. • Stored in register memory. • Default value is garbage value. • Declared using register keyword.
  • 24.
    …continued S.No . Storage Specifier Storage place Initial / default value Scop e Life 1 auto CPU Memory Garbage value localWithin the function only. 2 extern CPU memory Zero Global Till the end of the main program. Variable definition might be anywhere in the C program 3 static CPU memory Zero local Retains the value of the variable between different function calls. 4 register Register memory Garbage value local Within the function
  • 25.
    Structures • A structureis a collection of one or more variables, usually of different types, grouped together under a single name. • A structure can be declared using the keyword ‘struct’. • Syntax: struct structure_name {
  • 26.
    Structure example • structureexample: struct book { char book_name[20]; char author[20]; int no_of_pages; float price; }
  • 27.
    Structure members • Eachvariable inside the structure definition is called a structure member. • In the book example above, book_name , author, no_of_pages and price are the struct members.
  • 28.
    Structure variables • Astructure variable can be declared using the structure name. • Syntax: struct struct_name variable_name1,variable_name2,…; • Example: struct book b1,b2,b3;
  • 29.
    Accessing structure members •Structure members can be accessed using the dot(.) operator. • Syntax : struct_variable . struct member; • Example : b1.book_name; b2.book_name; etc.,
  • 30.
    Initializing structure members •Intializing can be done in two ways: 1. using assignment operator (=).. 2. from the keyboard 1. Using the assignment operator: eg: b1.no_of_pages=100; b1. price=235; 2. From the keyboard: eg: scanf(“%d”,&b1.no_of_pages);
  • 31.
    Array of structures •An array of structures can be declared in the same way we declare any other array. • Example: struct book b1[10]; b1 is the array of structures which holds 10 structures. • Each structure variable is accessed using the array index.
  • 32.
    Union • A Unionis a collection of one or more variables, usually of different types, grouped together under a single name. • A union can be declared using the keyword ‘union’. • Syntax: union union_name {
  • 33.
    union example • Unionexample: union book { char book_name[20]; char author[20]; int no_of_pages; float price; }
  • 34.
    Union members • Eachvariable inside the union definition is called a union member. • In the book example above, book_name , author, no_of_pages and price are the union members.
  • 35.
    union variables • Aunion variable can be declared using the union name. • Syntax: union union_name variable_name1,variable_name2,…; • Example: union book b1,b2,b3;
  • 36.
    Accessing union members •union members can be accessed using the dot(.) operator. • Syntax : union_variable .union member; • Example : b1.book_name; b2.book_name; etc.,
  • 37.
    Initializing union members •Intializing can be done in two ways: 1. using assignment operator (=).. 2. from the keyboard 1. Using the assignment operator: eg: b1.no_of_pages=100; b1. price=235; 2. From the keyboard: eg: scanf(“%d”,&b1.no_of_pages);
  • 38.
    Array of unions •An array of unions can be declared in the same way we declare any other array. • Example: union book b1[10]; b1 is the array of unions which holds 10 unions. • Each union variable is accessed using the array index.
  • 39.
    Differences between structureand union S.no C Structure C Union 1 Structure allocates storage space for all its members separately. Union allocates one common storage space for all its members. Union finds that which of its member needs high storage space over other members and allocates that much space 2 Structure occupies higher memory space. Union occupies lower memory space over structure. 3 We can access all members of structure at a time. We can access only one member of union at a time. 4 Structure example: struct student { int mark; char name[6]; double average; }; Union example: union student { int mark; char name[6]; double average; }; 5 For above structure, memory allocation will be like below. int mark – 2B char name[6] – 6B double average – 8B Total memory allocation = 2+6+8 = 16 Bytes For above union, only 8 bytes of memory will be allocated since double data type will occupy maximum space of memory over other data types. Total memory allocation = 8 Bytes
  • 40.
    Pointers • A pointeris a variable which points to another variable. • A pointer stores the address of another variable. • Pointers are used to allocate memory dynamically.
  • 41.
    Address of operator •& is called as ‘addressof ‘operator. • Helps to store the address of a variable in a pointer. • Then the pointer points to that variable. • A pointer variable is default initalized to null.
  • 42.
    Advantages of pointers •To return more than one value from a function. • To pass arguments to functions by reference. • Pointer concepts are very useful in development of system software. • With the address known, data can be accesses from any where in the program.