You are on page 1of 20

Fo

rA
pt
ec
h
C
en
tre
Session 9

U
Functions

se
O
nl
y
1
y
Objectives

nl
O
se
 Explain the use of functions

U
 Explain the structure of a function

tre
 Explain function declaration and function prototypes
 Explain the different types of variables

en
 Explain how to call functions
 Call by Value C
h
ec

 Call by Reference
pt

 Explain the scope rules for a function


rA

 Explain functions in multifile programs


Explain Storage classes
Fo


 Explain function pointers
Elementary Programming with C/Session 9/ 2 of 20
y
Functions

nl
O
se
 A function is a self-contained program segment that carries out a

U
specific, well-defined task

tre
 Functions are generally used as abbreviations for a series of

en
instructions that are to be executed more than once

C
 Functions are easy to write and understand
h
ec

 Debugging the program becomes easier as the structure of the


pt

program is more apparent, due to its modular form


rA

 Programs containing functions are also easier to maintain,


Fo

because modifications, if required, are confined to certain


functions within the program
Elementary Programming with C/Session 9/ 3 of 20
y
The Function Structure

nl
O
se
 The general syntax of a function in C is :

U
tre
en
C
h
 The type_specifier specifies the data type of the value, which
ec

the function will return.


pt

 A valid function name is to be assigned to identify the


rA

function
Fo

 Arguments appearing in parentheses are also termed as


formal parameters.
Elementary Programming with C/Session 9/ 4 of 20
y
Arguments of a function

nl
O
se
U
tre
en
Actual Arguments
C Formal Arguments
h
ec
pt
rA

 The program calculates the square of numbers from 1 to 10


Fo

 The data is passed from the main() to the squarer() function


 The function works on data using arguments Elementary Programming with C/Session 9/ 5 of 20
y
nl
Returning from the function

O
se
U
tre
en
C
h
ec

 It transfers the control from the function back to the calling program
pt

immediately.
rA
Fo

 Whatever is inside the parentheses following the return


statement is returned as a value to the calling program.
Elementary Programming with C/Session 9/ 6 of 20
y
Data Type of a Function

nl
O
se
U
tre
en
 The C
type_specifier is not written prior to the
squarer(), because squarer() returns an integer type value
function
h
ec

 The type_specifier is not compulsory if an integer type of


pt

value is returned or if no value is returned


rA

 However, to avoid inconsistencies, a data type should be


Fo

specified

Elementary Programming with C/Session 9/ 7 of 20


y
Invoking a Function

nl
O
se
 A semicolon is used at the end of the statement when a

U
function is called, but not after the function definition

tre
 Parentheses are compulsory after the function name,

en
irrespective of whether the function has arguments or not

C
 Only one value can be returned by a function
h
ec

 The program can have more than one function


pt
rA

 The function that calls another function is known as the calling


function/routine
Fo

 The function being called is known as the called function/routine


Elementary Programming with C/Session 9/ 8 of 20
y
Function Declaration

nl
O
se
 Declaring a function becomes compulsory when the function is

U
being used before its definition

tre
en
 The address() function is called
before it is defined
C
h
ec

 Some C compilers return an error, if


pt

the function is not declared before


calling
rA
Fo

 This is sometimes referred to as Implicit declaration


Elementary Programming with C/Session 9/ 9 of 20
y
Function Prototypes

nl
O
se
 Specifies the data types of the arguments

U
tre
char abc(int x, nt y);

en
Advantage :
C
h
Any illegal type conversions between the arguments used
ec

to call a function and the type definition of its parameters


pt

is reported
rA

char noparam (void);


Fo

Elementary Programming with C/Session 9/ 10 of 20


y
Variables

nl
O
se
 Local Variables

U
 Declared inside a function

tre
 Created upon entry into a block and destroyed upon exit
from the block

en
 Formal Parameters C
Declared in the definition of function as parameters
h

ec

 Act like any local variable inside a function


pt

 Global Variables
rA

 Declared outside all functions


Fo

 Holds value throughout the execution of the program

Elementary Programming with C/Session 9/ 11 of 20


y
Storage Classes-1

nl
O
se
 Every C variable has a characteristic called as a storage class

U
tre
 The storage class defines two characteristics of the variable:

en
C
• Lifetime – The lifetime of a variable is the length of time it
h
retains a particular value
ec
pt

• Visibility – The visibility of a variable defines the parts of a


rA

program that will be able to recognize the variable


Fo

Elementary Programming with C/Session 9/ 12 of 20


y
Storage Classes-2

nl
O
se
U
 automatic

tre
en
 external
C
h
ec

 static
pt
rA

register
Fo

Elementary Programming with C/Session 9/ 13 of 20


y
Function Scope rules

nl
O
se
 Scope Rules - Rules that govern whether one piece of

U
code knows about or has access to another piece of

tre
code or data

en
 The code within a function is private or local to that
function
C
h
 Two functions have different scopes
ec

Two Functions are at the same scope level


pt


rA

 One function cannot be defined within another


function
Fo

Elementary Programming with C/Session 9/ 14 of 20


y
Calling The Functions

nl
O
se
U
 Call by value

tre
en
 Call by reference
C
h
ec
pt
rA
Fo

Elementary Programming with C/Session 9/ 15 of 20


y
Calling By Value

nl
O
se
 In C, by default, all function arguments are passed by value

U
tre
 When arguments are passed to the called function, the values are

en
passed through temporary variables

C
 All manipulations are done on these temporary variables only
h
ec
pt

 The arguments are said to be passed by value when the value of the
rA

variable are passed to the called function and any alteration on this
value has no effect on the original value of the passed variable
Fo

Elementary Programming with C/Session 9/ 16 of 20


y
Calling By Reference

nl
O
se
 In call by reference, the function is allowed access to the

U
actual memory location of the argument and therefore can

tre
change the value of the arguments of the calling routine

en
 Definition C
h
ec

getstr(char *ptr_str, int *ptr_int);


pt

 Call
rA

getstr(pstr, &var);
Fo

Elementary Programming with C/Session 9/ 17 of 20


y
Nesting Function Calls

nl
O
se
main() palindrome()

U
{ {

tre
. .

en
. .
getstr();
palindrome(); C reverse();
h
ec

. cmp();
. .
pt

} .
rA

}
Fo

Elementary Programming with C/Session 9/ 18 of 20


y
Functions in Multifile Programs

nl
O
se
 Functions can also be defined as static or external

U
tre
 Static functions are recognized only within the program

en
file and their scope does not extend outside the
program file
C
h
static fn _type fn_name (argument list);
ec
pt

 External function are recognized through all the files of


rA

the program
Fo

extern fn_type fn_name (argument list);


Elementary Programming with C/Session 9/ 19 of 20
y
Function Pointers

nl
O
se
 Address is the entry point of the function

U
 Function has a physical location in memory that can be
assigned to a pointer

tre
#include <stdio.h>

en
#include <string.h>
void check(char *a, char *b, int (*cmp)());
main() C void check(char *a, char *b, int (*cmp)())
h
{
ec

{
char sl[80];
printf(“testing for equality \n”);
int (*p)();
pt

if (!(*cmp)(a,b))
p = strcmp;
rA

printf(“Equal”);
gets(s1);
else
gets(s2);
Fo

printf(“Not Equal”);
check(s1, s2, p);
}
}
Elementary Programming with C/Session 9/ 20 of 20

You might also like