06 Function
06 Function
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
}
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.
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!