Вы находитесь на странице: 1из 30

Computer Programming

06 Function
Function: INTRODUCTION
Functions are often:
used to break up large programs into named sections.
used when the same piece of code has to run multiple times.
In this case you can put this piece of code in a function and give that
function a name. When the piece of code is required, you just have to
call the function by its name.
So you only have to type the piece of code once.
You have already been using a function which is the main
function.
Function: TYPE
There are two types of functions:
The ones that are provided with the compiler (Built In Functions)
The ones that you write.
Function: BUILT IN FUNCTION

sqrt(), pow()

printf(), scanf()
Function: BUILT IN FUNCTION
Example:
Function: FUNCTION SCHEME
Function prototype:
prototype
parameter
parameters data type
function name
data type of function output
Function call:
call sqrt function
argumen
function name
assign the result to answer variable
Function: FUNCTION SCHEME
Function prototype:
prototype
parameter
parameters data type
function name
data type of function output
Function call:
call sqrt function
argumen
function name
assign the result to answer variable

Function prototype:
return_value function_name (list of parameter)
Function: BUILT IN FUNCTION #include <ctype.h>
Function: BUILT IN FUNCTION
int isalpha(int c)
Function: BUILT IN FUNCTION
#include <ctype.h>
Function: BUILT IN FUNCTION #include <math.h>
Function: BUILT IN FUNCTION #include <math.h>
Function: BUILT IN FUNCTION
#include <stdio.h>
Function: BUILT IN FUNCTION
int getchar(void)
Function: Writing Your Own Functions
SINTAX:
return_type functionName (param1_type param1, ) {
//local declaration
//statements
//return return_value
}

can accept (unlimited number of) parameters (if necessary)


and can return a result.
Function: Writing Your Own Functions
EXAMPLE:
SINTAX:
return_type functionName
(param1_type param1, )
{
//local declaration
//statements
//return return_value
}

can accept (unlimited number of)


parameters (if necessary) and can
return a result.
Function: Writing Your Own Functions
EXAMPLE:
Function: Writing Your Own Functions
SINTAX: EXERCISE:
return_type functionName Q0601
(param1_type param1, )
Write a function:
{
unsigned long factorial(int n);
//local declaration
Then, write a program to compute combination of two
//statements integer n & r:
//return return_value !
, = !
} !

can accept (unlimited number of) Q0602


parameters (if necessary) and can Write your own power function:
return a result.
double myPower(int a, int b);
Then, write a program to compute .
Procedure: DECLARATION
A function may simply perform a task without returning any
value is called PROCEDURE
SINTAX:
void functionName (param1_type param1, ) {
//local declaration
//statements
// NO return statement
}
Procedure: DECLARATION
PROCEDURE EXAMPLE:

function call
Procedure: DECLARATION
EXERCISE:
Q0603
Write a procedure to print string Hello world n times
to the output screen:
void helloWorld(int n);
n is an integer value get from the users input.

Q0604
Write a procedure to print fibonacci row:
1 1 2 3 5 8 . n
n is the number in that row.
Function: PASSING PARAMETER TO A FUNCTION
There are two ways to pass parameters to a function:

PASS BY VALUE: mechanism is used when you don't want to change the
value of passed paramters. When parameters are passed by value then
functions in C create copies of the passed in variables and do required
processing on these copied variables.

PASS BY REFERENCE: mechanism is used when you want a function to do


the changes in passed parameters and reflect those changes back to the
calling function. In this case only addresses of the variables are passed to
a function so that function can work directly over the addresses.
Function: PASS-BY-VALUE
Arguments are always
PASSED BY VALUE in C
function calls.
This means that local
``copies'' of the values
of the arguments are
passed to the routines.
Any change made to the
arguments internally in
the function are made
only to the local copies
of the arguments.
Function:
PASS-BY-VALUE
Run this code and
observe that a and b
are NOT exchanged!
Arguments are always
PASSED BY VALUE in C
function calls.
Function:
PASS-BY-REFERENCE
The RIGHT way to do
this is to use pointers:
The rule of thumb
here is that:
use regular
variables if the
function does not
change the values
of those arguments
MUST use pointers
if the function
changes the values
of those arguments
Function: GLOBAL & LOCAL VARIABLE
LOCAL VARIABLE
is a variable that is declared inside a function.
can only be used in the function where it is declared.

GLOBAL VARIABLE
is a variable that is declared outside all functions.
A global variable can be used in all functions.
Function: GLOBAL & LOCAL VARIABLE
EXAMPLE:
Function
EXERCISE (T0601):
Write your own function that calculates arithmetic middle of three
real numbers. Write additional main program that stores given
three numbers and calls on your previous function, and then prints
calculated arithmetic middle.
Function
EXERCISE (T0602):
Compose a function that calculates value of a sinus func. as sum of
n elements. Functions argument is given in radians. Func. sinus is
defined as:
3 5 7
sin x = + +
3! 5! 7!
Function
EXERCISE (T0603):
Compose a function that calculates value of a sinus func. as sum of
n elements. Functions argument is given in radians. Func. sinus is
defined as:
+1 21 3 5 7
sin x = =1(1) = + +
21 ! 3! 5! 7!

Вам также может понравиться