230 likes | 319 Views
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.
E N D
Wed July 8, 2002 Lecture 04Functions - 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
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) ); }
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
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.
Function Definition <return-type> <function-name> (<parameter-list> ) { ... <statements> ... } function head function body
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.
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);
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
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 ); }
different versions... int mrFonk ( int ); void main( ) { int x = 5, y; y = mrFonk( x ); } int mrFonk ( int b ) { return ( b*b); }
different versions... void main() { int mrFonk( int ); int x = 5, y; y = mrFonk(x); } int mrFonk (int b) { return ( b*b); }
different versions... int mrFonk (int b) { return ( b*b ); } void main() { int x = 5, y; y = mrFonk(x); }
more examples... double smaller ( double x, double y) { return x < y ? x : y; } void main(){ ... c = smaller ( a, b); ... }
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 ( ); }
simplest function... void emptyHead ( ){ } void main(){ emptyHead ( ); }
return what? • you must return a value of the return-type. double sqr(... ){ ... return x; }
return void ? • when return-type is void, return'ing is optional. void PrintIntro(){ ... return; }
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; }
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 = ?
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();