CSC 107 - Programming for Science - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

CSC 107 - Programming for Science

Description:

A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible? ... instead write: return fact; return(fact); void printLine(FILE ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 19
Provided by: matthe85
Category:

less

Transcript and Presenter's Notes

Title: CSC 107 - Programming for Science


1
CSC 107 -Programming for Science
  • Lecture 19
  • Functions

2
Question of the Day
  • A landscaper plants 5 rows of 4 trees each, but
    only uses 10 trees. How is this possible?

3
Todays Goal
  • Todays lecture discusses writing using
    functions
  • How to declare them, use them, trace them
  • After todays lecture, you should be ready to
    write programs that include functions

4
Functions
  • Already written programs using functions
  • Programs consist of at least one function
  • main is a programmer-defined function
  • Already know it can use pre-defined functions
  • Can also use other programmer-defined functions

5
Why We Use Functions
  • Simplify code
  • Parcel out each task to a function
  • Compute commonly-used math function
  • Each function can return a single value
  • Perform input output
  • Change arguments values (call-by-reference)

6
Function Definitions
  • Functions must be defined to use
  • Otherwise, get Undefined Symbol error during
    compilation
  • Functions defined outside of main
  • All definitions have the same form
  • return_type function_name(parameters)
    declarations statements

7
Return Type
  • Each function must declare a return type
  • Can be any data type or void
  • void used when not returning data
  • Trying to return value will cause an error
  • Otherwise, must return value of that type
  • return expression ends function execution and
    returns result of expression
  • Statement is required before function ends

8
Function Body
  • Begin by declaring variables it may use
  • Already been doing this in main
  • Variables created every time function called
  • Variables destroyed when function finished
  • Variables values not normally known at start of
    function
  • No different than how main works
  • Not changed by function having been called before
  • Not changed by another function using variables
    of same name

9
Function Parameters
  • Parameters are another form of variable
  • Declared in header between parentheses
  • Functions can have 0 or more parameters
  • 0 parameters noted using void keywordint
    main(void) ... int yourMommaWearsCombatBoots(v
    oid) ...double three(void) return 3.0
  • Otherwise, function must list all parameters

10
Function Parameters
  • Each parameter must have type and name
  • Just like the variables that they actually
    arevoid printInt(int printMe) ... int
    isLeapYear(int year) ... char dayOfWeek(int
    month, int day, int year) ... double
    nthRoot(double val, int n) ...
  • Parameters also live for duration of call
  • Values copied into them at function start
  • Changing parameters value does NOT change other
    variables values

11
Example Functions
  • int fact(int n) int fact 1while(ngt1)
    fact factn n--// Could instead write
    return factreturn(fact)
  • void printLine(FILE fout, int numStars) for
    (int i 0 i lt numStars i) fprintf(fout,
    )fprintf(fout, \n)

12
Calling A Function
  • Any function can call any other function
  • We have already done this a lot from
    mainprintf, scanf, getchar, sqrt, fabs,
  • No different calling programmer-written functions
  • Call must include values (of proper type) for
    each parameter
  • Function must be declared before the call
  • Via include statements e.g. library functions
  • Via defining it above any function calling it
  • Via a function prototype

13
Function Prototypes
  • Function name, return type, parameters
  • Line ends with a semicolon
  • Similar to variable declaration
  • Allows function calls to compile
  • But get Unknown Symbol error if there is no
    function definition
  • Usually listed at top of a file
  • Just after include and define statements

14
(No Transcript)
15
(No Transcript)
16
(No Transcript)
17
Your Turn
  • Get into groups and complete daily activity

18
For Next Lecture
  • Continue programming assignment 2
  • Hope that Prof. Hertz can revisit the 20th
    century and gets his electricity back
Write a Comment
User Comments (0)
About PowerShow.com