Professional Documents
Culture Documents
Session 9
Session 9
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 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
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
function
Fo
nl
O
se
U
tre
en
Actual Arguments
C Formal Arguments
h
ec
pt
rA
O
se
U
tre
en
C
h
ec
It transfers the control from the function back to the calling program
pt
immediately.
rA
Fo
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
specified
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
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
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
is reported
rA
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
Global Variables
rA
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
nl
O
se
U
automatic
tre
en
external
C
h
ec
static
pt
rA
register
Fo
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
rA
nl
O
se
U
Call by value
tre
en
Call by reference
C
h
ec
pt
rA
Fo
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
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
Call
rA
getstr(pstr, &var);
Fo
nl
O
se
main() palindrome()
U
{ {
tre
. .
en
. .
getstr();
palindrome(); C reverse();
h
ec
. cmp();
. .
pt
} .
rA
}
Fo
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
the program
Fo
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