; func1
Learning Center
Plans & pricing Sign in
Sign Out



  • pg 1
									                            Computer Programming TA C162

What to do…
Multidimensional Arrays
Character Arrays i.e. Strings
Modular Programming: User Defined Functions
Enumerated Data Types
Structures and Unions

                      Second Semester 2008-2009
                              Computer Programming TA C162

What’s Next….
•User-Defined Functions
  • Declaration and Definition
  • Function Parameters
  • Examples

                        Second Semester 2008-2009
                               Computer Programming TA C162

What is Function in C?
•   Functions are central to C programming and to the philosophy of C
    program design.

•   A C program consists of one or more function definitions, including
    exactly one that must be called main

•   A function is a named, independent section of C code that performs a
    specific task and optionally returns a value to the calling program.

•   All functions are stand-alone, which means that they are not nested in
    any other construct

                         Second Semester 2008-2009
                               Computer Programming TA C162

What is Function in C?
A function is named: Each function has a unique name. By using that
name in another part of the program, one can execute the statements
contained in the function. This is known as calling the function. A function
can be called from within another function.

A function is independent: A function can perform its task without
interference from or interfering with other parts of the program.

A function performs a specific task: Such as sending a line of text to a
printer, sorting an array into numerical order or calculating a cube root.

A function can return a value to the calling program: When a program
calls a function, the statements it contains are executed. If you want them
to, these statements can pass information back to the calling program.

                         Second Semester 2008-2009
      Computer Programming TA C162

Second Semester 2008-2009
                                Computer Programming TA C162

Function Declaration
A declaration merely provides a function prototype.
Function header :
Includes the return type and the list of parameters

return_type function_name( arg-typ name-1,...,arg-typ name-n);

int hex(int val);

                          Second Semester 2008-2009
                                  Computer Programming TA C162

  Function Definition
The definition of a function includes both the function prototype and the function
body, i.e. its implementation.
 return_typ function_name(arg-typ name-1,...,arg-typ name-n)
     /* statements; */
 int hex(int value)

                            Second Semester 2008-2009
                                Computer Programming TA C162

Function Parameters
There are two types of function parameters:
Formal parameters:
Appear in a declaration or a definition of a function.

Actual parameters:
Appear in a call to the function.

  int f(int x); here x is a formal parameter

   i = f(3); here 3 is the actual parameter
          corresponding to the formal parameter.

                          Second Semester 2008-2009
                            Computer Programming TA C162

/* Function: maxi
 * Purpose : Find the maximum of its integer arguments
 * Inputs : Two parameters
 * Returns : The maximum of parameters
 * Modifies: Nothing
 * Sample call: i = maxi(k, 3)
int maxi(int, int);
int maxi(int i, int j)
      return i > j ? i : j;

Exercise: Use this function to find maximum of three numbers.

                      Second Semester 2008-2009
                        Computer Programming TA C162

Passing Arguments to a Function
 long int cube(int); /* Function declaration */
 int main()
                            Actual parameter
 { long int c;
   c = cube(5); /* Function call */
   printf(“Cube of 5 = %ld”,c);
 }                               Formal parameter
 /* Function definition starts from here */
long int cube(int num)
{ long int cb; /*Local variable declaration */
  cb = num*num*num;
  return cb; /* Return value */

                  Second Semester 2008-2009
                        Computer Programming TA C162

int Addnum(int, int);
int main()
{ int a = 10,b = 20;
   printf("%d %d %d",a,b,Addnum(a,b));

int Addnum(int x, int y)
{ int c;
   c = x+y;
   return c;

                  Second Semester 2008-2009
                          Computer Programming TA C162

Sum of sin(x) Series
#include <stdio.h>
int main()
   int i,sign;
   float sum, element, fact, xpow;
   sum = 0.0;
   xpow = x;
   fact = 1.0;
   sign = 1;
   for( i=1; i<=n; i++ ) {
   element = sign * xpow/fact;
   sign *= -1;
   xpow *= x*x;
   fact *= (2*i)*(2*i+1);
   sum += element;
return 0;

                    Second Semester 2008-2009
                        Computer Programming TA C162

Driver Program for Sin(x) Series
long int fact( int n; /* Function Declaration */
float sinsum(float x, int n); /*Function Declaration */
int main()
{ int n;
 float x, sin_sum;
  printf(“Enter value of x and n”);
  scanf(“%f %d”,&x,&n);
 sin_sum = sinsum(x,n); /* Function Call */
 printf(“Sum of sin series = %f”,sin_sum);
return 0;

                  Second Semester 2008-2009
                        Computer Programming TA C162

Modular Programming Approach
long int fact( int n)/* Function Definition */
{int i;
 long int f=1;
 for( i=1; i<=n; i++ ) f *= i;
 return f;
float sinsum(float x, int n) /* Function Definition */
{int i,sign=1;
 float sum=0.0, element;
 for( i=1; i<=n; i++ ) {
 element = sign * pow(x,2*i-1)/fact(2*i-1);
 sign *= -1;
 sum += element;}
 return sum;

                  Second Semester 2008-2009

To top