+ All Categories
Home > Software > Functions in c

Functions in c

Date post: 07-Apr-2017
Category:
Upload: sunila-tharagaturi
View: 20 times
Download: 1 times
Share this document with a friend
42
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
Transcript
Page 1: Functions in c

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

Page 2: Functions in c

..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.

Page 3: Functions in c

Defining a Function

• Syntax: function definition function declaration

return_type function_name(List of parameters) {//set of statementsfunction body

}

Page 4: Functions in c

..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); }

Page 5: Functions in c

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.

Page 6: Functions in c

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);

Page 7: Functions in c

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 these values are called as inputs.

• Return value:- These values are passed from one function to another function while invoking function. Generally these values are called as outputs.

Page 8: Functions in c

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;printf(“enter any numbers”);scanf(“%d%d”, &a , &b) ;c = a + b ;printf(“\n sum is=%d ” , c ) ;}

Page 9: Functions in c

ii)No arguments , With return value

int add ( ) ; //function prototypevoid main(){printf(“\nsum is=%d”,add()); //

function call}int add() ; //function definition{int a,b,c;printf(“enter any numbers”);scanf(“%d%d”, &a , &b) ;c = a + b ;return c ;

}

Page 10: Functions in 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}void add ( int x , int y )

//function definition{int z = x + y ;printf(“sum is=%d”,z) ; }

Page 11: Functions in c

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}int add ( int x , int y ) //function

definition{int z = x + y ;return z ; }

Page 12: Functions in c

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

Page 13: Functions in c

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. {

.

. }

When the sum function is called a’s value is passed into m and b’s value is passed into n.That is why call by value is also called as pass by value and call by reference is also called as pass by reference.

Page 14: Functions in c

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.

Page 15: Functions in c

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.

Page 16: Functions in c

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  return f;

}

Page 17: Functions in c

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.

Page 18: Functions in c

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 variablevoid main()

{int y; // local variable

. } void sum(){ int a,b,c; //local variables

.}X can be used in all the functions in a program.

Page 19: Functions in c

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– static– register

Page 20: Functions in c

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.

• Auto variables are stored in memory.

• Default value of an auto variable is garbage value.

Page 21: Functions in c

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.

• It’s default value is zero

Page 22: Functions in c

static

• It’s scope is local. It’s value persists between function calls.

• Declared using static keyword.

• Stored in memory.

• Default is zero.

Page 23: Functions in c

register

• Scope is local.

• Available within the function.

• Stored in register memory.

• Default value is garbage value.

• Declared using register keyword.

• Can be accessed very fast. Can declare only few variables.

Page 24: Functions in c

…continuedS.No

.Storage Specifier

Storage place

Initial / default value

Scope Life

1 auto CPU Memory

Garbage value local Within the function only.

2 extern CPU memory

Zero GlobalTill 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

Page 25: Functions in c

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{

datatype1 variable_name1;datatype2 variable_name2;

……

}

Page 26: Functions in c

Structure example

• structure example: struct book{

char book_name[20];char author[20];int no_of_pages;float price;

}

Page 27: Functions in c

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.

Page 28: Functions in c

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;

• In the above example b1,b2,b3 are the structure variables.

• Each structure variable will be allocated memory for all the structure members.

Page 29: Functions in c

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.,

Page 30: Functions in c

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); scanf(“%d”,&b2.no_of_pages);

Page 31: Functions in c

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.

• Eg: b1[0] refers to the first element of the array.

b1[2] refers to the third element of the array.

b1[9] refers to the last element of the array.

Page 32: Functions in c

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{

datatype1 variable_name1;datatype2 variable_name2;

……

}

Page 33: Functions in c

union example

• Union example: union book{

char book_name[20];char author[20];int no_of_pages;float price;

}

Page 34: Functions in c

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.

Page 35: Functions in c

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;

• In the above example b1,b2,b3 are the union variables.

• Each union variable will be allocated memory for only the biggest of the union members.

Page 36: Functions in c

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.,

Page 37: Functions in c

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); scanf(“%d”,&b2.no_of_pages);

Page 38: Functions in c

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.

• Eg: b1[0] refers to the first element of the array.

b1[2] refers to the third element of the array.

b1[9] refers to the last element of the array.

Page 39: Functions in c

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 – 2Bchar name[6] – 6Bdouble 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

Page 40: Functions in c

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.

• Syntax : datatype *pointer_variable;

• Example : int *ip; //points to an int

char *chp; //points to a char variable

float *fp; //points to a float

Page 41: Functions in c

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.

• The size of a pointer variable is 2 bytes(for a 16 bit compiler), as it stores only the address.

Page 42: Functions in c

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.


Recommended