Sie sind auf Seite 1von 27

Functions

Self contained program segment that carries out some


specific, well-defined task. E.g.
Main function in c
The main body of a C program, identified by the
keyword main, and enclosed by the left and right
braces is a function. It is called by the operating
system when the program is loaded, and when
terminated, returns to the operating system.
Building blocks
Group relevant and repetitive actions into functions
Variables are usually local

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

Arguments : Variables or expressions


placed in parentheses in a Call
statement of the function
Parameters: Variables placed in
Parentheses after a functions name.
When the function is called, the values of
the corresponding arguments are placed
in the 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

The Function Syntax


Syntax:
Data_type Function_Name(type declared parameter list)
{
statements that make up the function
}
NB: The syntax may change from one compiler to another

It is worth noting that a return_data_type is


assumed to be type int unless otherwise
specified, thus main() returns an integer to
the operating system.

Class Exercise
Write a function to convert Celsius to
Fahrenheit and Fahrenheit to Celsius
C = ( F - 32) / 1.8
F = C 1.8 + 32

Example of a Function Definition


(using a function to change from Fahrenheit to Celsius)

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 */
}

It is possible for a function to have multiple


return statements.
E.g.
int validate_input( char command )
{
switch( command )
{
case '+' : case '-' : return 1;
case '*' : case '/' : return 2;
default : return 0;
}
}

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

NOTE that the function prototype ends


with a semi-colon; in this way we can tell
its a declaration of a function type, not the
function code.

Example of a program using a function

/* Program to calculate a specific factorial number */

#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 );
}

Example of a program using a Function prototype

/* Program to calculate a specific 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.

Das könnte Ihnen auch gefallen