Pemrograman Dasar
Pemrograman Dasar
Pemrograman Dasar
Main_program
NAME
abs, labs, llabs, imaxabs - compute the absolute value of an integer.
SYNOPSIS
#include <stdlib.h>
=======================================================
To use the library function abs( ), you must include the stdlib.h file.
MAN PAGE EXAMPLE (CONT’D)
function definition
THE FUNCTION PROTOTYPE
Informs the compiler that there will be a function defined later that:
Needed because the function call is made before the definition -- the
compiler uses it to see if the call is made properly
THE FUNCTION CALL
Passes program control to the function
Must match the prototype in name, number of arguments, and types of
arguments
After the statements in the function have completed, control is passed back
to the calling function, in this case main( ) . Note that the calling function
does not have to be main( ) .
GENERAL FUNCTION DEFINITION SYNTAX
type functionName ( parameter1, . . . , parametern )
{
variable declaration(s)
statement(s)
}
If there are no parameters, either functionName( ) OR functionName(void) is acceptable.
There may be no variable declarations.
If the function type (return type) is void, a return statement is not required, but the following
are permitted: return ; OR return( ) ;
POSSIBILITIES
A function may:
Have no arguments and return nothing.
Have arguments and return nothing.
Have arguments and return one value.
Have no arguments and return one value.
A function can never return more than one value
PARAMETER LIST
A function can have more than one parameter:
int functionA( int a, int b, float c )
When the function is invoked, the parameters can be a
variable, constant, or expression:
result = functionA( 7, 3 + a, dollars);
The value 7 is put into to local variable a, 3 + a is put into
local variable b, and a copy of the value in dollars is put into
c in this example.
USING PARAMETERS
void printMessage (int counter) ;
int num;
int i ;
}
USING PARAMETERS (CONT’D)
USING {
Formal parameters are the parameters that appear in the function header.
float averageTwo (int num1, int num2)
result = 12 ===================
=================== 6 + 4 is 10, OK.
Notice that 2.1 became 2, 3.5 became 3 and 4.0 became 4. This is called
truncation. The compiler tried to make the data fit the prototype! This is called
an implied conversion, and can be the source for errors that is hard to find.
Do not use implied conversions!
ANOTHER EXAMPLE
#include <stdio.h> In this example the expression was
#include <stdlib.h> the result of another function
multiplied by a constant.
int main( void )
{ Whatever the argument is, it is
int negNum = -3; evaluated to the proper data type
printf( "%d\n", abs( negNum ) * 2 ); and then the function is invoked that
that final version of the argument.
return 0;
}
GOOD PROGRAMMING STYLE:
SIMPLICITY
Functions should do one thing and do it well!
Functions can only return one thing! (You can use a data
structure as a simple variable => do not worry, let’s deal
with it later)
It is better to have more than one function than one
complicated function.
GOOD PROGRAMMING STYLE: