Docsity

User-Written Functions - Intro to Computer Programming - Lecture Slides, Slides of Computer Engineering and Programming

Partial preview of the text

Download User-Written Functions - Intro to Computer Programming - Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!

User-Written Functions

Lecture 12

Things to Look Out For

  • Vocabulary
    • Prototypes
    • Definition
    • Call
    • Return type
    • Arguments or parameters
  • Local variables
  • Pass by value
  • Pass by reference

User-Written Functions

  • The parentheses, ( ), both indicate that main is a function, AND provide a place for any variable names (arguments) that could be sent as values from another function. Since main is the main program function, it will normally not have a parameter list. (Who calls the main function anyway?)
  • Most, but not all, other functions have parameter lists. Some such, as rand ( ), do not since they need no data or values from the calling function.

Writing User-Written Functions

  • There are typically three different types of statements needed to properly set-up and make use of a user-written function. They are:
    1. The function prototype statement.
    2. The first line of the function definition.
    3. The calling statement.

2. The First Line of the Function

Definition.

  • The first line of the function definition is the same as the examples of the function prototype except that the SEMICOLON IS ELIMINATED and variable or argument NAMES ARE REQUIRED.
  • Example:

float sum ( int a, int b, int c )

  • Here, the function sum is of type float and has

three calling parameters, all of which are ofDocsity.com

The Body of the Function

  • A complete function consists of the the function definition and the body of the function in the { }.
  • Example:

float sum ( int a, int b, int c) { float total; total = a + b + c ; return total;

3. The Calling Statement.

  • The function call (or invocation) is like the first line of the function definition except NO DATA TYPES are shown and constants may be used instead of variable names. The valued returned by a function is normally assigned to a variable in the calling function.
  • Example:

value = sum ( i, j, k ) ; or value = sum ( 5, 6.73, 2 ) ; Docsity.com

Factorial Function Main Program

#include <stdio.h>

long factorial ( int x ) ;

int main ( )

{

int k; for ( k=0 ; k<=10 ; k++ ) { printf ("%2d factorial is %ld\n", k , factorial (k) ) ; }

?

?

Factorial Program Output

  • 0 factorial is
  • 1 factorial is
  • 2 factorial is
  • 3 factorial is
  • 4 factorial is
  • 5 factorial is
  • 6 factorial is
  • 7 factorial is
  • 8 factorial is
  • 9 factorial is
  • 10 factorial is

The Next Slide is VERY VERY

Important!!

Call By Value vs. Call By Reference

  • Two ways in which variables are passed:
    • Call by value
    • Call by reference
  • "Call by value" means function only gets a copy of the value of the calling argument. The called function can not change the value back in the calling routine. This is normally how it is in C.
  • "Call by reference" means the function gets the original argument, and thus the called function can change the value of the variableDocsity.com

Problem G

  • Problem G11 calculates the values of sine for angles between 0 and 90 degrees.
  • You are provided with a partial program that has a main function and a user written sine function.
  • You are to complete the user written sine function (mysine(x) ).
  • The main program will compare the calculations or your mysine function with the sin function from the math.h library. Docsity.com