Professional Documents
Culture Documents
Functions
CSEB134 : BS/2008 1
Principles of Programming
Objectives
Introduction to function
Predefined functions / Standard functions
User defined functions
function declarations/prototype
function definition
function call and return
Function with parameters/ argument
Scope of variables – global and local variables
Duration of variables
Simple recursion
CSEB134 : BS/2008 2
Principles of Programming
Introduction to Functions
A function is a block of code which is used to
perform a specific task.
It can be written in one program and used by
another program without having to rewrite that
piece of code.
Functions can be put in a library. If another program
would like to use them, it will just need to include the
appropriate header file at the beginning of the
program and link to the correct library while
compiling.
A function is used by calling the name of the
function.
CSEB134 : BS/2008 3
Principles of Programming
Categories of Functions
Predefined functions (standard functions)
Built-in functions provided by C that are used by
programmers without having to write any code for
them. Pre-defined at the standard C libraries.
Example: printf(), scanf(), main() etc
User-Defined functions
Functions that are written by the programmers
themselves to carry out various individual tasks.
Examples: CalculateAverage(), Sum() etc
CSEB134 : BS/2008 4
Principles of Programming
Pre-defined Functions
<assert.h> Contains macros and information for adding diagnostics that aid
program debugging.
<ctype.h> Contains function prototypes for functions that test characters
for certain properties, convert lowercase letters to uppercase
and vice versa.
<errno.h> Defines macros that are useful for reporting error condition.
Pre-defined Functions
<signal.h> Contains function prototypes and macros to handle various
conditions that may arise during program execution.
<stdarg.h> Defines macros for dealing with a list of arguments to a
function whose number and types are unknown.
<stddef.h> Contains common definitions of types used by C for performing
certain calculations.
<stdio.h> Contains function prototypes for the standard input/output
library functions and information used by them.
<stdlib.h> Contains function prototypes for conversions of numbers to
text and text to numbers, memory allocation, random numbers,
and other utility functions
<string.h> Contains function prototypes for string processing functions.
<time.h> Contains functions prototypes and types for manipulating time
and date.
CSEB134 : BS/2008 6
Principles of Programming
User-defined Functions
A programmer can create his/her own
function(s).
It is easier to plan and write our program if we
divide it into several functions instead of
writing a long piece of code inside the main
function.
A function is reusable and therefore prevents
programmers from having to unnecessarily
rewrite what we have written before.
CSEB134 : BS/2008 7
Principles of Programming
The Concept
With Function
#include <stdio.h>
vs. void print_menu(void);
main()
{ …
print_menu();
Without Function …
} /* end main */
#include <stdio.h>
main()
void print_menu(void)
{ …
{
printf(“This program
draws a rectangle”); printf(“This program
draws a rectangle”);
…
} /* end function */
}
CSEB134 : BS/2008 8
Principles of Programming
User-defined Functions
In order to write and use our own function,
we need to do these:
1. Function declarations
Also called function prototype. Used to declare a
function.
2. Function calls
Call the function whenever it needs to be used.
3. Function definitions
Also called function implementation, which define the
function somewhere in the program.
CSEB134 : BS/2008 9
Principles of Programming
Example
#include <stdio.h>
void print_menu(void); Function declaration.
main()
{
print_menu();
Function call.
print menu();
}
void print_menu(void)
Function definition.
{
printf(“This program draws a rectangle”);
} /* end function */
CSEB134 : BS/2008 10
Principles of Programming
Function Declarations/Prototype
Consist of:
A function type.
A function name.
An optional list of parameter type enclosed in ().
Use commas to separate more than one parameter types.
Use (void) or () if there is no parameter.
A terminating semicolon.
Example:
void print_menu(void);
int GetScore(void);
void CalculateTotal(int);
char Testing(char, char);
CSEB134 : BS/2008 11
Principles of Programming
Function Calls
A function is called by the declared function
name.
Requires:
Name of the function.
A list of parameters, if any, enclosed in ()
A terminating semicolon.
Example: Means NO parameter.
print_menu();
print_menu(3);
print_menu(3, answer);
CSEB134 : BS/2008 12
Principles of Programming
Function Definitions
A function definition is where the actual code
for the function is written. This code will
determine what the function will do when it is
called.
Consists of :
A function type (return value data type).
A function name.
An optional list of formal parameters enclosed in
parentheses (function arguments)
A compound statement ( function body)
CSEB134 : BS/2008 14
Principles of Programming
A Function may…
1. Receive no input parameter and return
nothing,
2. Receive no input parameter but return a
value,
3. Receive input parameter( one or more) and
return nothing, OR
4. Receive input parameter( one or more) and
return a value
CSEB134 : BS/2008 15
Principles of Programming
1.Receive no input parameter
and return nothing: Example
#include <stdio.h>
Function declaration.
void print_menu(void);
main()
{
print_menu(); Function call.
}
int print_menu(void)
Function definition.
{
printf(“This program draws a rectangle”);
return 1;
} /* end function */
CSEB134 : BS/2008 17
Principles of Programming
3. Receive input parameter ( one or
more) and return nothing: Example
#include <stdio.h>
Function declaration.
void print_menu(int);
main()
{ Function call.
print_menu(3);
}
CSEB134 : BS/2008 20
Principles of Programming
Example
#include <stdio.h>
Formal parameter/argument
void print_menu(int);
main()
{
print_menu(3); Actual parameter/argument
}
void print_menu(int x)
{
printf(“This program draws %d rectangles”, x);
} /* end function */
CSEB134 : BS/2008 21
Principles of Programming
#include <stdio.h>
Formal parameter/argument
int print_menu(int);
main()
{
printf(“The value: %d\n”, print_menu(3));
}
Actual parameter/argument
int print_menu(int x)
{
printf(“This program draws %d rectangles\n\n”, x);
return x;
} /* end function */
CSEB134 : BS/2008 22
Principles of Programming
Three important points concerning functions
with parameters are:
The number of actual parameters in a function
call must be the same as the number of formal
parameters in the function definition.
A one-to-one correspondence must occur
among the actual and formal parameters. The
first actual parameter must correspond to the first
formal parameter and the second to the second
formal parameter, an so on.
The type of each actual parameter must be either
the same as that of the corresponding formal
parameter.
CSEB134 : BS/2008 23
Principles of Programming
CSEB134 : BS/2008 24
Principles of Programming
Example: More than one
Parameters
#include <stdio.h>
Function declaration.
void print_menu(int, char);
main()
{
char answer = ‘y’; Function call.
print_menu(3, answer);
}
void print_menu(int x, char ans)
Function definition.
{
if (ans == ‘y’ || ans == ‘Y’)
printf(“This program draws %d rectangles”, x);
} /* end function */
CSEB134 : BS/2008 25
Principles of Programming
Example
#include<stdio.h>
void main( )
{
int first, second, temp=0;
printf("Please enter the first number: "); Maybe
scanf("%d", &first);
rewritten as:
printf("Please enter the second number: ");
scanf("%d", &second);
if(second>first) {
temp=second;
second=first;
first=temp;
}
printf("\n%d is bigger than %d. \n\n", first, second);
}
CSEB134 : BS/2008 26
Principles of Programming
#include<stdio.h>
void convert_number(int, int);
void main( )
{
int first, second;
printf("Please enter the first number: ");
scanf("%d", &first);
printf("Please enter the second number: ");
scanf("%d", &second);
convert_number(first, second);
}
}
Principles of Programming
Declaration of Variables
Up until now, we have learnt to declare our
variables within the braces of segments (or a
function) including the main.
It is also possible to declare variables outside
a function. These variables can be accessed
by all functions throughout the program.
CSEB134 : BS/2008 29
Principles of Programming
Example
// To print integers from num down to 1.
#include <stdio.h>
void print_integers(int);
void main (void){
int num;
printf(“Enter an integer: “);
scanf(“%d”, &num);
for (; num >= 1; num--)
print_integers(num);
printf(“%d\n”, num);
}
void print_integers(int n){
if (n >= 1){
printf(“%d\n”, n);
print_integers(n-1);
} /* end if */
} /* end function print_integers */
CSEB134 : BS/2008 32
Principles of Programming
Summary
This chapter has introduced you to functions and
discussed elements of user-defined functions.
Each function must be defined, declared, and called.
You studied valued functions, which return a value
under their name, and learned to use the return
statement.
We covered the concepts of global and local
variables and simple recursion.
CSEB134 : BS/2008 33
Principles of Programming
Exercise
1. Write a function distance that calculates the
distance between two points (x1,y1) and (x2,y2).
All number and return values should be of type
float.
2. Write a function multiply that accept 2 integers
and returns the multiplication of those two integers.
3. Write a program that inputs a series of integers
and passes them one at a time to function even,
which uses the remainder operator to determine if
an integer is even. The function should take an
integer argument and return 1 if the integer is even
and 0 otherwise.
CSEB134 : BS/2008 34
Principles of Programming
CSEB134 : BS/2008 35
Principles of Programming
5. Write a main program that calculates total of three scores and this
calculation must be done in a separate function called
total_score(). The main program should read three(3) scores
and calls the function total_score(). Then the average of
these three scores is calculated. If the average is > 40, then the
status PASS is displayed. The sample output is as follows:
CSEB134 : BS/2008 36
Principles of Programming