C and Functions - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

C and Functions

Description:

A function can have many input variables but only one (well defined) output variable. ... y = Arcsin(x) sometimes call many-one. Functions Lackey cFunctionsNew2.ppt. 3 ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 28
Provided by: EdwardDon8
Category:
Tags: arcsin | functions

less

Transcript and Presenter's Notes

Title: C and Functions


1
C and Functions
  • A first look

2
Function
  • A function can have many input variables but only
    one (well defined) output variable.
  • y 3x 1
  • z xx xw ww
  • y sqrt (x)
  • y Arcsin(x)
  • sometimes call many-one.

3
Concept of functions in C
mysterious function
input1
output1
input2
input3
4
Example pow
pow
double x
double x y
double y
5
or
pow x to the y power
double
double
double
6
Example scanf
scanf
int
format string
  • one or more variables

7
Example main
main
void
void
8
How is this represented in C?
  • double pow (double x, double y)
  • void main (void)
  • int scanf( string, variable1, variable2,..)
  • the key to is the data types

9
comments
  • C only works with functions.
  • main is a function.
  • A function can use other functions.
  • Other names for functions
  • procedures (Pascal)
  • subroutines (Fortran, Basic)
  • methods (Java)
  • include this brings in function libraries

10
Functions are useful and fun!
11
Why functions?
  • A functions is a reusable block of code that can
    be shared.
  • A function is a box that has input (called
    parameters) and at most one well defined output.
  • The data type of the parameters and output must
    be known to use a function properly.
  • You can use functions to breakup a program into
    smaller, easier to code modules.

12
A function prototype
  • A function prototype tells you about the data
    type of the input and output.

Input Output
13
Function Prototypes
  • Basic prototype
  • outputDataType functionName ( inputDatatype1
    dummyVariable1, )
  • examples
  • double pow(double a, double b)
  • void main (void)
  • int product ( int a, int b)
  • Note prototypes are terminated with

14
Write prototype statements for the following
functions
  • Display only your name on the screen
  • void myNamePrint (void)
  • Find the area of a circle given the radius
  • double circleArea (double radius)
  • Find the first non-zero digit of a student_id.
  • int firstDigit (long id)
  • Display the largest of 3 integers
  • void printBiggest ( int a, int b, int b)

15
Find the error in the following prototypes
  • int funOne ( double x, float y)
  • no ending
  • void funTwo (void, void)
  • Can not use void, void. Just use void.
  • float funThree( float x, y)
  • each dummy variable must have its own data type.

16
Corrected prototypes
  • int funOne ( double x, float y)
  • void funTwo (void)
  • float funThree( float a, float x)
  • Proper C prototype is different. Though C and
    java uses dummy variables, C just uses the only
    data type in the prototype. However all work
    correctly.
  • int funOne(double, double)
  • float funThree (float, float)

17
A function definition or how to build your own
  • int product (int x, int y)
  • int t
  • t x
  • t t y
  • return t

18
Some comments
  • A function consists of
  • the header which is similar to the prototype
    statement with no
  • The function body
  • return
  • variables or parameters are never defined (x and
    y in this case)
  • other variable, like t, are called local
    variables.

19
More comments
  • local variables can not be seen outside the
    function in which they are defined
  • in this example the parameters are passed by
    value and can NOT be changed.
  • there can only be one function called main

20
Another look at product
  • // A function definition or how it works
  • int product (int x, int y)
  • int t
  • t x
  • t t y
  • return t

21
Find the error in the following
  • void fun (int x, int y)
  • int z
  • z xx 2xy yy
  • return z

22
Your own function in a program
  • A function consists of two parts.
  • the function prototype
  • the function definition
  • Common location in source code
  • include
  • one or more function prototypes
  • main
  • one or more function definitions

23
Using a function
  • Use the defined functions as you would any other
    function (like y sqrt(x), y sin(x))

24
Factorial Function
  • include ltstdio.hgt
  • int factorial (int)

25
  • void main (void)
  • int a, c
  • a 7
  • c factorial (a)
  • printf("\nd factorial is d", a, c)
  • printf("\nd factorial is d",9,factorial(9))
  • getchar()
  • return

26
  • int factorial (int x)
  • int t, j
  • t 1
  • for(j 1 jltx j)
  • t tj
  • return t

27
Fini
Write a Comment
User Comments (0)
About PowerShow.com