ch06 Functions
ch06 Functions
Chapter Contents
Functions
Definition, Declarations and Structure
Using arguments
Exiting the function
Passing information
by value
by reference
Functions - Introduction
C is a procedural language, based on functions.
A function is a self-contained unit of program code, designed to
accomplish a particular task.
We shall see:
Definition and declaration of functions.
Invoking functions.
Communicating: passing information to and from functions.
Function Definition
Every function in C has the following template:
return_type
The type of the value that the function returns.
If no return type is specified, int is the default.
If the function only performs some task and has no value to
return, one should specify void as its return type.
Parameters:
The parameters are variables that are initialized with values
passed to the function.
A function may have no parameters. In this case the
parentheses are left empty , or the keyword void is
written inside them.
© Copyright: Spymek Software Pvt. Ltd.
C1 Ch03 - Operators and Expressions – 7
Function Body
The function body contains:
Definitions of local variables are optional, but are usually
present.
Executable statements, which may be any statement in C.
Functions
Let’s write a program for printing the following header:
******************************
Hello World
******************************
It is convenient to create a function, typerow( ) , for printing
a row of 30 asterisks.
#include <stdio.h>
void typerow( )
{
int counter;
for(counter = 1 ; counter <= 30 ; ++counter)
putchar('*');
putchar('\n');
}
function return
void main(void)
{
typerow( );
printf(" hello World \n");
typerow( );
} © Copyright: Spymek Software Pvt. Ltd.
C1 Ch03 - Operators and Expressions – 9
Explanations
The function typerow() prints 30 asterisks in a row.
When the function has completed its task, it returns to the same
place in the program where it was invoked, and the program
continues.
typerow( )
putchar( )
printf( )
typerow( )
putchar( )
} © Copyright: Spymek Software Pvt. Ltd.
C1 Ch03 - Operators and Expressions – 11
Function Parameters
Let’s rewrite our function typerow( ), so that it will print out a
varying number of whichever char requested by the calling
function. For example, typerow(26, '$'); will result in 26
dollar signs in a row .
void typerow(int number, char ch)
{
int counter;
for(counter = 1; counter <= number ; ++counter)
putchar(ch);
putchar('\n');
}
Example: triangle.c
1 /* triangle.c
2 This program prints a right-angled triangle.
3 The triangle’s size and the character that composes
4 it are determined by the user. */
5
6 #include <stdio.h>
7
8 void typerow(int number, char ch); /* prototype that enables
9 the compiler to check if we send correct Parameters to the
10 function. */
11 void main(void)
12 {
13 int size; /* the size of the triangle*/
14 char ch; /* the char that will compose the triangle*/
15 int i; /* a counter */
16 printf("Enter the triangle’s size => ");
17 scanf("%d",&size);
18 getchar();
Exiting a Function
One way to leave a function is with the return statement, as shown
before.
The control returns to the place from which the function was called.
Another way to leave a function (and the whole program) is with the
function exit.
Call by Value
The parameter that was sent from the calling function is called
an ‘actual parameter’. The parameter received by the function is
called ‘formal parameter’ or ‘argument’.
Call by Reference
Sometimes we want the parameter that was sent to a function to
be changed by that function.
By Value By Reference
The value of the actual parameter Any change to the formal
is not changed (even if the formal parameter is reflected in the
parameter is changed). actual parameter.
Argument may be any expression :
Argument must be an
-3, i, 2 * a + b, -123,
address of a variable.
8.23, sqrt(abs(n)) .
void main(void)
{
int num1 = 10, num2 = 20;
temp = *num1;
*num1 = *num2;
*num2 = temp;
The life time of a variable is the time between the ‘birth’ of the
variable on the computers memory and the time it ‘dies’.
The table in the next page describes the attributes of global and
automatic local variables. The two static types will be discussed
later.
– cont’d
Global Automatic Local
declaration outside any function in a block { }
Unless specified
No automatic initialization.
otherwise,
initialization automatically initialized If specifically initialized - it
occurs at each ‘birth’
to 0
functions beneath it, in
scope its block
the same module
each time the block is
‘birth’ once, before main()
entered
once, after main() each time the block is
‘death’ ends exited
address on the data area on the stack area
Scope Diagram
1 int num = 42;
2 void func(void)
3 {
4 int other_num = 10;
5 printf ("%d %d", num, other_num);
6 }
7 void main()
8 {
9 int num = 1, other_num = 2;
10 printf ("%d %d", num, other_num);
11 {
12 int other_num = 3;
13 printf ("%d %d", num, other_num);
14 }
15 printf ("%d %d", num, other_num);
16 func();
17 }
Storage Classes
Summary
A Function is an autonomous code segment that accomplishes a
particular task.
A function may receive parameters and may return a value.
Actual parameters – sent.
Formal parameters (arguments) – received.
Variables may be passed as parameters to a function :
By value:
The value of the variable is passed.
Changing the formal parameter does not effect the actual
parameter. Scalar variables are sent this way by default.
By reference
The address of the variable is passed.
Changing the formal parameter does effect the actual
parameter. Arrays can only be sent this way.
© Copyright: Spymek Software Pvt. Ltd.
C1 Ch03 - Operators and Expressions – 27
Summary – cont’d
There are 4 storage classes :
Automatic
Static
Register
extern
The arguments received by a function are automatic local
variables of that function.
A program should be split to several modules.
The keyword extern help sharing global variables or functions
between different modules of the program.