Professional Documents
Culture Documents
CHAPTER 6 : FUNCTIONS
.
INTRODUCTION
What is a Function?
A module of code that performs a specific job
• 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);
#include <stdio.h>
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)
• 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;
return 0;
}