You are on page 1of 24

COMPUTER PROGRAMMING

CHAPTER 6 : FUNCTIONS
.
INTRODUCTION
What is a Function?
 A module of code that performs a specific job

 Experience has shown that the best way to develop and

maintain large programs is to build it from smaller


components or modules
 In C, such modules are known as functions

 Each module is generally simpler and more manageable

than the entire program


 This concept is known as Divide and Conquer, also with

Hiding in the mix


INTRODUCTION
• Divide and Conquer: break large programs into a series
of smaller functions.
– Helps manage complexity
– Makes it easier to build large programs
– Makes is easier to debug programs
• A function is invoked by its name and parameters.
– No two functions have the same name in your C program.
– No two functions have the same name AND parameter types in
your C++ program.
– The communication between the function and invoker is through
the parameters and the return value.
INTRODUCTION

• A function is independent:
– It is “completely” self-contained.
– It can be called at any places of your code
and can be ported to another program.
• Functions make programs reusable and
readable
INTRODUCTION
• Advantages of using C Function:
– Problem can viewed in a smaller scope
– Program development are much faster compared
to the common structure
– Program becomes easier to maintain
• Types of Function in C Language:-
– Standard library functions
• Input/output functions
• Mathematical functions
– User-defined functions = functions created by user
for specific purposes.
INTRODUCTION
Syntax Example
main () Function Function Formal
{ Type Name parameter
function_1();
function_2();
function_n(); void print_menu (void)
return 0; {
} printf (“This Program Draws A Rectangle or A”);
function_1()
{ printf (“Triangle on The Screen.\n”);
statement; printf (“Enter 1 to draw a rectangle.\n”);
return (); printf (“Enter 2 to draw a triangle.”);
} }
function_2()
{
statement;
return();
}
FUNCTION
CONCEPT
• Function Prototype
• Function Definition
• Function Call
Example
#include <stdio.h>
int maximum(int, int, int); Function Prototype: this
function takes three
main() ints, and returns one int.
{
printf("Maximum is: %d\n", maximum(5, 7, 3));
}
int maximum(int x, int y, int z)
{
int max = x;
if (y > max)
max = y;
Function Definition
if (z > max)
max = z;

return max;
} Return statement
Rules of Prototyping
ELEMENTS OF FUNCTIONS
• Function Prototype
– In general, all function in C program must be declared.
– The only exception is the function main, which must not
be declared
– A function type consists of:-
i. Function type
ii. Function name
iii. A list of parameters, the list of function parameter
types is written as (void) or (). If the function has
more than one formal parameter, the parameter
type in the list must be separated by commas
ELEMENTS OF FUNCTIONS
• Example
void print_menu (void);
double squared (double number);
int get_menu_choide (void);

• Global Prototype vs Local Prototype


– Global=Function prototype placed in the
outside function definitions
– Local=Placed in the function in which
they appear
Understanding Scope
Scope
• Determines where the variable can be
referenced in a program.
• By understanding scope, you can make your
programs:
– more modular
– easier to debug
• Generally arises in discussions of global
versus local variables
Global v. Local Variables
• Global:
– A variable declared outside any function
– Can be referenced by any function in the program
• Local:
– A variable declared inside a function
– Can only be referenced within that function.
• If you have a local variable in a function and a global
variable with the same name, the local one is used
Example
/* This program demonstrates global variables and scope */

#include <stdio.h>

void a (void); If you have a local


void b (void); variable and a global
int x = 1; /* Global Variable */ variable with the same
name, the local one is
main () {
used.
printf ("In main, x equals: %d\n", x);
In main, x equals: 1
a(); In function (a), x equals: 100
b();
In function (b), x equals: 1
printf ("In main, x equals: %d\n", x); In main, x equals: 2
return 0;
}

void a (void) {
int x = 100;
printf ("In function (a), x equals: %d\n", x);
}

void b (void) {
printf ("In function (b), x equals: %d\n", x++);
}
Avoid Global Variables!
• Now that you know about global variables,
never use them!
• Why?
– If you use a global variable, any function can
modify it
– This makes it extremely hard to track down
problems
– Undermines the modularity of your programs
Rules of Function Definition
ELEMENTS OF FUNCTIONS
• Function Definitions
– Has two principal components:
i. First line
ii. Body of the function
- The first line of a function definition consists of these elements:-
i. Function type
ii. Function name
iii. An optional list of formal parameters enclosed in parentheses
(pewarisan)

In general terms, the first line can be written as


function_type function_name (formal parameters)
Function Prototype vs. Definition

• The Function prototype and the function definition


must include the same data types. However,

– Prototype: you do not need to include the


names of parameters, but you must include the
parameter data types.
• e.g. int square (int);

– Definition: you must include the name and


data type of all parameters.
• e.g. int square (int y);
Rules of Function Calls
FUNCTION
ELEMENTS
• Example Functions
• Function Calls
– Require the name of the function followed by the list
of actual parameters (or arguments) or parentheses
– () indicate the function call
– For example:-
• print_menu()
• draw_triangle()
– The actual parameters may expressed as constants,
single variables or more complex expression but
must be of the same data type as its corresponding
formal parameter.
FUNCTION CALLS
• Function Calls
– If the function return a value, the function call is
often written as an assignment statement
– For example:
• y=polynominal(x);
• This function access causes the value returned
by the function to be assigned to the variable y
- If the function does not return anything, the
function calls appears by itself:-
- display(a,b,c)
FUNCTION CALLS

• Functions can use a void return type if you intend to return nothing.
For example:
void print_header()
{
printf("Program Number 1\n");
printf("by Marshall Brain\n");
printf("Version 1.0, released 12/26/91\n");
}
• This function returns no value. You can call it with the following
statement:
– print_header();
#include <stdio.h>
int main(void)
{
int ICNumber;

printf(“Please Key In Your IC Number : \n”);


scanf(“%d”, &ICNumber);
printf(“Please Key In

return 0;
}

You might also like