Lecture 04 Functions I - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Lecture 04 Functions I

Description:

hyperbolic sine of x. hsin(x) arctan of (y/x), result between -pi and pi. atan2(y,x) ... Function Definition return-type function-name ( parameter-list ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 24
Provided by: ahm4
Category:

less

Transcript and Presenter's Notes

Title: Lecture 04 Functions I


1
Lecture 04Functions - I
Wed July 8, 2002
  • METU Dept. of Computer Eng. Summer 2002
  • Ceng230 - Section 01
  • Introduction To C Programming
  • by Ahmet Sacan

2
re-glance _at_ TOC
  • Variables, Data Types
  • Conditionals if, ?, switch
  • Loops for, while, do-while
  • Functions
  • Arrays
  • Strings

3
include ltmath.hgt
4
include ltmath.hgt (cont'd)
5
math.h example
  • include ltmath.hgt
  • include ltstdio.hgt
  • void main ( )
  • double d int x
  • scanf("lf", d)
  • printf("sqrt( ) lf\n", sqrt(d) )
  • x ceil(d)
  • printf("ceil ( ) d", x)
  • printf("floor( ) d", floor(d) )

6
Functions
  • A function is a sub-program section of a program
    designed to complete some tasks under the
    direction of another program unit.
  • Using functions maximize
  • modularity
  • readability

7
Function Declaration (Prototype)
  • ltreturn-typegt ltfunction-namegt
  • ( ltparameter-listgt )
  • Function declaration gives the identity of the
    function. It tells the compiler how the function
    may be called.
  • The prototype of a function f must occur either
    outside all functions and before any call to f,
    or inside all functions that call f.

8
Function Definition
  • ltreturn-typegt ltfunction-namegt
  • ( ltparameter-listgt )
  • ...
  • ltstatementsgt
  • ...

function head
function body
9
Function Jargon
  • Call a function
  • transfer control to it. Execution continues from
    the function. When function is done, the caller
    continues execution after the calling expression.
  • Return value
  • determines the value of the calling expression.
  • Parameters
  • declared in the function header.
  • Means of data exchange with the caller.
  • Arguments
  • Declared in caller.
  • Correspond to function parameters.

10
Function Types
  • with / without return-type.
  • void PrintIntro( )
  • int ReadAnInt( )
  • with / without arguments.
  • void PrintIntro( )
  • void PrintResult( int res )
  • type of arguments readable, writeable...
  • int Add( int a, int b)
  • void Add( int a, int b, int sum)

11
Function example
  • int mrFonk(int) /prototype says take an int,
    and return an int. /
  • void main()
  • int x 5, y
  • y mrFonk(x) / function is called with
    argument x /
  • / return value of
    mrFunk is assigned to y /
  • int mrFonk(int b) / function header almost
    identical to prototype. /
  • return ( bb )

function body
return value
12
Function example
  • int mrFonk(int)
  • void main()
  • int x 5, y
  • printf("in main, before calling mrFunk(x)
    xd, yd\n", x, y)
  • y mrFonk(x)
  • printf("in main, after calling mrFunk(x)
    xd, yd\n", x, y)
  • int mrFonk(int b)
  • printf("in mrFonk b d\n", b)
  • return ( bb )

13
different versions...
  • int mrFonk ( int )
  • void main( )
  • int x 5, y
  • y mrFonk( x )
  • int mrFonk ( int b )
  • return ( bb )

14
different versions...
  • void main()
  • int mrFonk( int )
  • int x 5, y
  • y mrFonk(x)
  • int mrFonk (int b)
  • return ( bb )

15
different versions...
  • int mrFonk (int b)
  • return ( bb )
  • void main()
  • int x 5, y
  • y mrFonk(x)

16
more examples...
  • double smaller ( double x, double y)
  • return x lt y ? x y
  • void main()
  • ...
  • c smaller ( a, b)
  • ...

17
more...
  • void PrintIntro( )
  • printf("Welcome to my program.\n")
  • printf("I just learnt about functions.\n")
  • printf("So, here I'm using them...\n")
  • void main ( )
  • PrintIntro ( )
  • PrintIntro ( )

18
simplest function...
  • void emptyHead ( )
  • void main()
  • emptyHead ( )

19
return what?
  • you must return a value of the return-type.
  • double sqr(... )
  • ...
  • return x

20
return void ?
  • when return-type is void, return'ing is optional.
  • void PrintIntro()
  • ...
  • return

21
Find 5 errors so it compiles...
  • include ltstudio.hgt
  • int main()
  • int num1, num2,
  • int num3 5
  • char letter1 'a', letter2
  • num2 num3 1
  • num1 num2 x num2 num3
  • letter2 letter1
  • printf("The variable y equals d\n," y)
  • printf("The variable x equals d\n", x)
  • print("The variable z equals d\n", z)
  • printf("The variable letter1 equals c \n",
    letter1)
  • printf("The variable letter2 equals c \n",
    letter2)
  • return 0

22
  • if(x)
  • if(y)
  • z1
  • else
  • z2

if(x) if(y) z1 else z2
if(x) if(y) z1 else z2
if(x y) z1 else z2
z ?
23
Find equivalent
  • if ( x )
  • if ( y )
  • if ( z ) humpty()
  • else
  • else
  • else
  • if ( y )
  • if ( z ) dumpty( )
  • else bumpty( )
  • else

if ( x y z ) humpty () else if ( y z )
dumpty ( ) else bumpty()
if ( x y z ) humpty () else if ( !x y
z ) dumpty() else if ( !x y !z)
bumpty()
if ( y) if ( x z ) humpty () else if ( !x
z ) dumpty() else if ( !x !z) bumpty()
Write a Comment
User Comments (0)
About PowerShow.com