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

More on Functions

Recursive functions

A function call is within the same function.

Example:
Computing the factorial of a number:
factorial tex2html_wrap_inline77
factorial tex2html_wrap_inline79
factorial tex2html_wrap_inline81

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

Structured Programming

main function

Declarations of Variables: Storage class

There are three basic places in a C program where variables will be declared:

Local Variables

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

External Variables

Register Variables

Scope of Variables

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

About this document ...

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


Vijay &
Thu Feb 12 11:03:06 EST 1998