Sie sind auf Seite 1von 0

Functions

TOP-DOWN DESIGNING AND STRUCTURED PROGRAMMING


Top-down designing is decomposing a problem into smaller problems. Eventually, one
has a collection of small problems or tasks, each of which can be easily coded into
functions.

WHAT IS A FUNCTION?
A function is a section of a program that performs a specific task.
The function construct in C is useful in writing code for the bottom most directly solvable
problems. Programmers can then combine these functions with other functions and
ultimately with main() to solve the original problem.
C automatically provides some functions, such as printf() and scanf(). Programmers
also can write their own functions.

FUNCTION DEFINITION
A function definition describes what a function does.
The general form of a function is:
type name (parameter list) header of the
declaration of the parameters function
{
declarations body of the
statements function
}
where:
Type is the type of the value that the function returns, if any. The type void is used if a
function does not return a value.
Parameters (also called formal parameters) are the variables that hold actual values that
the calling environment passes to a function.

FUNCTION INVOCATION
A program consists of one or more functions with one of them being main(). Program
execution always begins with main().
To call or to invoke a function, just simply use its function name.
Examples:
#include <stdio.h>
void main(void)
{
int x;
printf (\nEnter a number >)
scanf (%d, &x);
printf (\nThe number is %d., x);
}
the printf() and scanf() functions are called by simply using their respective function names.



1
Functions

FUNCTION INVOCATION
#include <stdio.h>
void prn_message(void)
{
printf (Message for you: );
printf (Have a nice day!\n);
}
void main(void)
{
prn_message();
printf (Thank you!\n);
}
execution begins in main(). When the program encounters prn_message(), it invokes the
function and program control passes to it.
After executing prn_message(), program control passes back to the calling environment
which is main() and program execution continues.

FUNCTIONS WITH PARAMETERS
When a program calls a function, it may pass arguments or values to that function. The
parameters in a function will eventually hold these values .
Upon function invocation, the function uses the value of the argument corresponding to a
formal parameter.
Example:
#include <stdio.h>
void prn_message(k)
{
int i;
printf ("Message for you:\n");
for (i =1; i <=k; ++i)
printf (" Have a nice day!\n");
}
void main(void)
{
int n;
printf ("Enter a small integer >");
scanf ("%d", &n);
prn_message(n);
printf ("\nThank you.");
}

The important parts of the program above are:
prn_message(n)
This statement invokes the function prn_message(). The calling environment passes the
value of n as an argument to the function
void prn_message(k)
int k;
2
Functions

This is the header of the function definition of prn_message(). The identifier k is a
parameter that is of type int. The variable k will the hold the value of n that was passed
upon invocation of prn_message().
{
int i;
printf ("Message for you:\n");
for (i =1; i <=k; ++i)
printf (" Have a nice day!\n");
}
This is the body of the function definition for prn_message(). If n is equal to 2, then the
function will print the string Have a nice day! twice. The variable i here is a variable that
is necessary in the execution of the function prn_message(). Parameter k and variable i
are local to the function prn_message().

Local Variables
If a function defines and uses variables, then these variables are the local variables of that
function.
A local variable comes into being when its function is called. When the function call is
over, the variable is no longer available. In other words, a local variable does not exist
outside the confines of its defining function.
Local variables defined and used in a function have no relation to those in another even if
the functions use the same variable name.

Example
#include <stdio.h>
void magic1(x)
{
printf (\nThe value of x is %d., x);
x =10;
printf ("\nThe value of x is %d.", x);
}
void main(void)
{
int x =5;
printf ("\nThe value of x is %d.", x);
magic1(x);
printf ("\nThe value of x is %d.", x);
}
The value of x is 5.
The value of x is 5.
The value of x is 10.
The value of x is 5.




3
Functions

OPTIONS IN DECLARING PARAMETERS
It is possible to declare the parameters within the parameter listing.
Example:
#include <stdio.h>
void prn_message(int k)
{
int i;
printf ("Message for you:\n");
for (i =1; i <=k; ++i)
printf (" Have a nice day!\n");
}
void main(void)
{
int n;
printf ("Enter a small integer >");
scanf ("%d", &n);
prn_message(n);
printf ("\nThank you.");
}

THE return STATEMENT
The return statement has two purposes.
1. Every time a function encounters a return statement, program control is immediately
passed back to the calling environment. In other words, the function terminates.
2. In addition, if an expression follows the keyword return, then the function returns the value
of the expression to the calling environment as well.
The value returned, if any, must agree in type with the function definition header. If no
type is explicitly declared, the type is implicitly int.
A return statement has one of the following two forms:
return;
return expression;
Examples:
return (3);
return (a+b);
Although it is not necessary, it is considered good programming practice to enclose in
parentheses the expression being returned so that it is more clearly visible.










4
Functions

SAMPLE PROGRAM
Write a C program that prompts the user to enter two integers and outputs the smaller of
the two.
#include <stdio.h>
int min (int x, int y)
{
if (x <y)
return (x);
else
return (y);
}
void main(void)
{
int j, k, m;
printf("Input one integer : ");
scanf("%d", &j);
printf("Input another integer: ");
scanf ("%d", &k);
m =min(j,k);
printf ("\n%d is the minimum of %d and %d.\n\n", m, j, k);
}

The important parts of the program above are:
m =min(j,k);
This statement invokes the function min(). The calling environment, which is main(),
passes the values of j and k as arguments to min(). The function will return a value, and
that value is assigned to m.
min (x, y)
int x, y;
This is the header of the function definition of min(). The identifiers x and y are parameters
of type int. The values of j and k that were passed upon invocation of min() will be
assigned to x and y respectively.
{
if (x <y)
return (x);
else
return (y);
}
This is the body of the function definition for min(). If the value of x (which is actually j) is
less than the value of y (which is actually k), then the function returns the value of x to the
calling environment; otherwise it returns the value of y.






5
Functions

FUNCTION PROTOTYPES
In C, a function call can appear before the actual definition of the function. A function
definition may appear at the end of the program or even at a different program or file.
However, the compiler does not know the number and types of the parameters of the
function during function calls since the definition comes toward the end of the program.
Function prototypes enable programmers to specify the number and types of parameters
of the function before the actual function definition.
The general form of a function prototype is
type function_name (parameter type list);
Example:
#include <stdio.h>
int min (int, int);
void main(void)
{
int j, k, m;
printf("Input one integer : ");
scanf("%d", &j);
printf("Input another integer: ");
scanf ("%d", &k);
m =min(j,k);
printf ("\n%d is the minimum of %d and %d.\n\n", m, j, k);
}
int min (int x, int y)
{
if (x <y)
return (x);
else
return (y);
}

OPTIONS IN FUNCTION PROTOTYPES
Function prototypes may appear together with the variable declaration of main()
#include <stdio.h>
main()
{
int j, k, m;
int min (int, int);
Function prototypes can also include the identifier names of parameters which may or may
not match with the actual parameters of the function.
#include <stdio.h>
int min (int a, int b);
main()
{
int j, k, m;

6
Functions

Practice 1
Write a program to convert dollars to pesos using a function to do the actual conversion.
#include <stdio.h>
float convert (float);
main()
{
float dollars, pesos;
printf (Enter the no. of dollars you want to convert : );
scanf (%f, &dollars);
pesos =convert (dollars);
printf (\n\n$ %.2f is equal to %.2f pesos., dollars, pesos);
}
float convert (float x)
{
float y;
y =x * 26.95;
return (y);
}

Practice 2
Write a program to compute the area of a circle using a function to do the actual
computation.
#include <stdio.h>
#define PI 3.14
float compute_area (float);
void main(void)
{
float radius, area;
printf (Enter the radius of the circle: );
scanf (%f, &radius);
area =compute_area (radius);
printf (\n\nThe area of the circle is %.2f., area);
}
float compute_area (float x)
{
float y;
y =PI * x * x;
return (y);
}








7
Functions

Practice 3
Write a program to compute base
exp
using functions
#include <stdio.h>
int compute (int, int)
void main (void)
{
int answer, base, exp;
printf (\nEnter the value of the base > );
scanf (%d, &base);
printf (\nEnter the value of the exponent >);
scanf (%d, &exp);
answer =compute (base, exp);
printf (\n\n\n%d raised to %d is equal to %d., base, exp, answer);
}
int compute (int x, int y)
{
int counter, answer;
counter =1;
answer =1;
while (counter <=y) {
answer =answer * x;
counter =counter +1;
}
return (answer);
}


GLOBAL VARIABLES
Variables defined outside main() are the global variables of the program . Unlike local
variables, any function may use global variables.
#include <stdio.h>
void magic1 (void);
int x;
void main(void)
{
x =5;
printf ("\nThe value of x is %d.", x);
magic1();
printf ("\nThe value of x is %d.", x);
The output of this program will be:
The value of x is 5.
The value of x is 5.
The value of x is 10.
The value of x is 10.

}
void magic1(void){
printf (\nThe value of x is %d.,x);
x =10;
printf ("\nThe value of x is %d.", x);
}


8

Das könnte Ihnen auch gefallen