C Functions - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

C Functions

Description:

Title: C Functions Author: Mike Holmes Last modified by: Mike Holmes Created Date: 11/21/2000 6:05:24 PM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 10
Provided by: MikeHo151
Category:
Tags: functions | holmes

less

Transcript and Presenter's Notes

Title: C Functions


1
C Functions
2
Functions
  • May return only one value.
  • May receive many formal parameters.
  • Should only have one entrance and one exit.
  • Parameters are always passed by value.
  • Passing a pointer looks like passing by
    reference.
  • Pointer is passed by values.

3
main The way in.
  • The main function is a function like any other.
  • It defines the entry point to your program.

4
Function definitions
  • Functions are defined in three parts.
  • Return data type.
  • Name of the function (subject to variable naming
    rules.
  • Parameters to be passed into the function.
  • void main( void)
  • void - indicates that the function does not
    return a value.
  • main name of the function.
  • (void) indicates that no parameters are passed
    into the function.

5
Function prototypes
  • A function prototype is a forward reference to a
    function.
  • They provide a way to define functions prior to
    their real definition.
  • int add( int, int)
  • Note that formal parameters are not necessary
    only data types.

6
Function Definition
  • To define a function start with the function
    header.
  • Define the return data type.
  • Define the function name.
  • Define the formal parameters.
  • int add( int a, int b)
  • return ab

7
Function Definition
  • No semicolon at the end of the function
    definition.
  • Formal parameters a and b are declared.
  • Returns the value of ab.
  • Example
  • in main function.
  • int a add(2, 3)
  • returns the sum of 2 and 3 (5).
  • stores this value in the variable a.

8
Dealing with structs
  • Since parameters are passed by value structs are
    copied to the function.
  • With large structures this may be time consuming
    ( and wasteful of space ).
  • The alternative is to pass in the address of your
    structure. (A pointer to the structure.)

9
Dealing With Pointers
  • Pointers may be declared to any data type.
  • datatype variablename
  • To access the actual value you need to
    dereference the pointer.
  • variablename 3
  • Without the variablename will be the address of
    the variable with the it is the value of the
    variable.
  • \\MERCURY\DATA\USERS\MHOLMES\spsdemo\demo1.c
  • spsdemo\Debug\demo1.exe
Write a Comment
User Comments (0)
About PowerShow.com