Beruflich Dokumente
Kultur Dokumente
Characteristic of a function
A function will process information that is
passed to it from the calling portion of the
program and return a single value
Arguments
Parameters
Types of Functions
Standard functions (built-in) e.g.
Some may return nothing others multiple values
e.g.
User-defined functions
A function designed to return a single value.
The value is returned in the function itself.
The arguments of a function should not be
changed in the function body
Class Exercise
Write a function to convert Celsius to
Fahrenheit and Fahrenheit to Celsius
C = ( F - 32) / 1.8
F = C 1.8 + 32
float FtoC(float f)
{
Float c
c = (5 / 9) * (f - 32);
return(c);
}
Example
#include <stdio.h>
int add1(int);
void main()
{
int x;
x=5;
x=add1(x);
printf("%d,x);
}
int add1(int i)
{
int y;
y=i+1;
return (y);
}
RETURNING FUNCTION
RESULTS
This is done by the use of the keyword return,
followed by a data variable or constant value, the
data type of which must match that of the
declared return_data_type for the function.
e.g.
float add_numbers( float n1, float n2 )
{
return n1 + n2; /* legal */
return 6; /* illegal, not the same data type */
return 6.0; /* legal */
}
Example 2
/* Simple multiply program using argument passing */
#include <stdio.h>
int calc_result( int numb1, int numb2 )
{
auto int result;
result = numb1 * numb2;
return result;
}
main()
{
int digit1 = 10, digit2 = 30, answer = 0;
answer = calc_result( digit1, digit2 );
printf("%d multiplied by %d is %d\n", digit1, digit2, answer );
}
Class EXERCISE
Write a program in C which incorporates a
function using parameter passing and
performs the addition of three numbers.
The main section of the program is to print
the result.
FUNCTION PROTOTYPE
FUNCTION PROTOTYPE
#include <stdio.h>
void calc_factorial( int n )
{
int i, factorial_number = 1;
for( i = 1; i <= n; ++i )
factorial_number *= i;
printf("The factorial of %d is %d\n", n, factorial_number );
}
main()
{
int number = 0;
printf("Enter a number\n");
scanf("%d", &number );
calc_factorial( number );
}
#include <stdio.h>
void calc_factorial( int n ); /* Function prototype */
main()
{
int number = 0;
printf("Enter a number\n");
scanf("%d", &number );
calc_factorial( number );
}
void calc_factorial( int n )
{
int i, factorial_number = 1;
for( i = 1; i <= n; ++i )
factorial_number *= i;
printf("The factorial of %d is %d\n", n, factorial_number );
}
RECURSION
This is where a function repeatedly calls
itself to perform calculations. Typical
applications are games and Sorting trees
and lists.
Consider the calculation of 6! ( 6 factorial )
6! = 6 * 5 * 4 * 3 * 2 * 1
6! = 6 * 5!
6! = 6 * ( 6 - 1 )!
n! = n * ( n - 1 )!
Example
#include <stdio.h>
long int factorial( long int n )
{
long int result;
if( n == 0 )
result = 1;
else
result = n * factorial( n - 1 );
return ( result );
}
main()
{
int j;
for( j = 0; j < 11; ++j )
printf("%2d! = %ld\n", factorial( (long) j) );
}
Global Variables
Arguments and Local Variables are
accessible only inside the function where
they are declared.
Variable declarations that are placed
outside of any function are accessible to
all functions, and retain their values for the
life of the program.