Functions
Objectives
‣ to learn about functions and how to use them to
write programs with separate modules variables
‣ to understand the capabilities of some standard
functions in C
‣ to learn how to pass information to functions using
input arguments
‣ to learn how to return a value from a function
2
Function
‣ also called subroutine, chunk of code, building block of a
program
‣ allow complicated programs to be divided into small
blocks – “divide and conquer”
‣ a named sequence of statements that performs some
useful procedure
‣ is a complete and independent program
‣ can be thought of as a “black box” that has passed one
or more input values and automatically returns a single
output value
3
C Library Functions
‣ promotes code reuse by providing predefined
functions that can be used in your programs
stdio.h
‣ library containing predefined functions that can be
used to perform input and output operations
math.h
‣ standard math library containing predefined functions
that can be used to perform mathematical
computations
4
Some
Mathematical
Library Functions
5
Writing your own Functions
A Function must have:
1. return type
‣ can be any of the standard data types
‣ void if function does not return anything
2. name followed by a pair of ()
‣ names follow rules in writing identifiers
3. 0 or more parameters to be placed inside the pair of ()
‣ A parameter is a local variable. It has a data type and a name. It is a
special kind of variable that refers to data that a subroutine receives on
which to operate
4. pair of curly braces {} which will contain the function body
5. a return value
6
Writing your own Functions
A Function may belong to any of the following types:
1. Functions with no parameter and no return type
2. Functions with parameters and no return type
3. Functions with parameters and return types
7
Writing your own Functions
Functions with no parameter and no return type
void drawSquare()
{
// prints a 3x3 square
printf(“***\n”);
printf(“***\n”);
printf(“***\n”);
}
Note that:
‣ function has no return type – it is VOID.
‣ function has a name - drawSquare
‣ no parameters
‣ drawSquare will display a 3x3 square
8
Writing your own Functions
9
Writing your own Functions
Functions with parameter(s) and no return type
void addition(int a, int b)
{
// compute for the sum of a & b
int sum;
sum = a + b;
printf(“The sum of is %d”, sum);
}
Note that:
‣ function has no return type – it is VOID.
‣ function name is addition
‣ two parameters a and b – both of the type int
‣ addition will display the sum of two integers
10
Writing your own Functions
Functions with parameter(s) and return type
int addition(int a, int b)
{
// compute for the sum of a & b
int sum;
sum = a + b;
return sum;
}
Note that:
‣ function has return type – int
‣ function name is addition
‣ two parameters a and b – both of the type int
‣ addition will return the sum of two integers
11
Writing your own Functions
12
Writing your own Functions
13
Writing your own Functions
Parameter
‣ piece of information you provide in order to call a function
‣ like variables in the sense that they contain values and have
types
Argument
‣ a value that you provide when you call a function
‣ must have the same type as the corresponding parameter
14