1 / 23

Lecture 04 Functions - I

Wed July 8, 2002. Lecture 04 Functions - I. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan. re-glance @ TOC. Variables, Data Types Conditionals: if, ?, switch Loops: for, while, do-while Functions Arrays Strings.

remedy
Download Presentation

Lecture 04 Functions - I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. #include <math.h>

  4. #include <math.h> (cont'd)

  5. math.h example #include <math.h> #include <stdio.h> 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) <return-type> <function-name> ( <parameter-list> ) ; • 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 <return-type> <function-name> (<parameter-list> ) { ... <statements> ... } 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 ( b*b ); } function body return value

  12. Function example int mrFonk(int); void main(){ int x = 5, y; printf("in main, before calling mrFunk(x): x=%d, y=%d\n", x, y); y = mrFonk(x); printf("in main, after calling mrFunk(x): x=%d, y=%d\n", x, y); } int mrFonk(int b){ printf("in mrFonk: b = %d\n", b); return ( b*b ); }

  13. different versions... int mrFonk ( int ); void main( ) { int x = 5, y; y = mrFonk( x ); } int mrFonk ( int b ) { return ( b*b); }

  14. different versions... void main() { int mrFonk( int ); int x = 5, y; y = mrFonk(x); } int mrFonk (int b) { return ( b*b); }

  15. different versions... int mrFonk (int b) { return ( b*b ); } void main() { int x = 5, y; y = mrFonk(x); }

  16. more examples... double smaller ( double x, double y) { return x < 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 <studio.h> 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) z=1; else z=2; if(x) { if(y) z=1; } else z=2; if(x) ; if(y) z=1; else z=2; if(x && y) z=1; else z=2; 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();

More Related