Functions
The general format of a function is:
type function_name(parameter list)
{ /* opening brace begins the body of the function*/
The
body of
the func
comes here
/* Each statement ends with a semicolon*/
} /* closing brace ends the function*/
Function Header: type function_name(parameter list)
Return data types- Examples:
int func1(....)
float func2( ...)
void func3(...)
Function Name: examples:
func1(int x, float y, char z)
func2(void)
Example:
#include <stdio.h>
long cube(long x); /*this is a function prototype containing
the name, the list of variables passed to it, and the
type of variable it returns */
void main()
{
long input, answer;
printf("Enter an integer value: ");
scanf("%d", &input);
answer = cube(input);
/* Note: %ld is the conversion specifier for */
/* a long integer */
printf("\nThe cube of %ld is %ld.", input, answer);
}
long cube(long x)
{
long x_cubed;
x_cubed = x * x * x;
return x_cubed;
}
Example:
#include <stdio.h>
long cube();
void main()
{
long input, answer;
printf("Enter an integer value: ");
scanf("%d", &input);
answer = cube(input);
printf("\nThe cube of %ld is %ld.", input, answer);
}
long cube(x)
long x;
{
long x_cubed;
x_cubed = x * x * x;
return x_cubed;
}
The value of an argument changes :
#include <stdio.h>
float half_of(float);
void main()
{
float x = 3.5, y = 65.11, z;
/* In this call, x is the argument to half_of(). */
z = half_of(x);
printf("The value of z = %f\n", z);
/* In this call, y is the argument to half_of(). */
z = half_of(y);
printf("The value of z = %f\n", z);
}
float half_of(float k)
{
/* k is the parameter. Each time half_of() is called, */
/* k has the value that was passed as an argument. */
return (k/2);
}
Returning a Value:
- The return statement terminates the function and returns a value to the calling program
- A function can have multiple return statements
Example 1:
int func1(int var)
{
int x;
....
return x;
}
Example 2:
/* Demonstrates using multiple return statements in a function. */
#include <stdio.h>
int larger_of( int, int);
void main()
{
int x, y, z;
puts("Enter two different integer values: ");
scanf("%d%d", &x, &y);
z = larger_of(x,y);
printf("\nThe larger value is %d.", z);
}
int larger_of( int a, int b)
{
if (a > b)
return a;
else
return b;
}
Function call: funct1(a, b, c); Function header: void func1(int x, int y, int z)
x = half_of(square(half_of(y)));
x = half_of(y) + half_of(z);
if ( half_of(x) > 10)
{
statements;
}
A function call is within the same function.
Example:
Computing the factorial of a number:
factorial
factorial
factorial
The expression of the value of the factorial of n in terms of the value of the factorial of (n-1) is called a recursive definition, since the value of a factorial is based on the value of another factorial.
int factorial_f(int x)
{
int result;
if (x == 0)
result=1;
else
result=x* factorial_f(x-1);
return(result);
}
main()
{
int n, fact_value;
printf("enter value n ?\n");
scanf("%d",&n);
fact_value=factorial_f(n);
printf("it factorial=%d",fact_value);
}
There are three basic places in a C program where variables will be declared:
Automatic versus Static Local variables:
Use a static local variable to retain its value between calls
/* Demonstrates automatic and static local variables. */
#include <stdio.h>
void func1(void);
void main()
{
int count;
for (count = 0; count < 20; count++)
{
printf("At iteration %d: ", count);
func1();
}
}
void func1(void)
{
static int x = 0;
int y = 0;
printf("x = %d, y = %d\n", x++, y++);
}
Replace the above function with the following and observe the result:
void func1(void)
{
static int x = 0;
int y ;
printf("x = %d, y = %d\n", x++, y++);
}
int x = 1; /* global declaration*/
main()
{
{
{
printf("innermost x =%d\n",x);
}
printf("middle x = %d\n",x);
}
printf("outermost x=%d\n",x);
}
Program output:
innermost 1
middle 1
outermost 1
int x=1; /* global declaration*/
main()
{
/* Note: there is a conflict of names between
global and local variables - the local persists */
int x;
x=10; /* outermost block */
{
int x;
x=100; /* middle block*/
{
int x;
x=1000; /* innermost block */
printf("innermost x=%d\n",x);
}
printf("middle x=%d\n",x);
}
printf("outermost x=%d\n",x);
}
Program output:
innermost 1000
middle 100
outermost 10
| Storage Class | Lifetime | Location of | Scope |
| definition | |||
| Automatic | temporary | in a function | local |
| Static | temporary | in a function | local |
| Register | temporary | in a function | local |
| External | Permanent | outside a function | global |
This document was generated using the LaTeX2HTML translator Version 96.1 (Feb 5, 1996) Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
The command line arguments were:
latex2html -split 0 -html_version 3.0 c3.tex.
The translation was initiated by Vijay & on Thu Feb 12 11:03:06 EST 1998