lecture 04 functions - i metu dept. of computer eng. summer 2002 ceng230 - section 01 introduction...
Post on 21-Dec-2015
220 views
TRANSCRIPT
Lecture 04Functions - I
METU Dept. of Computer Eng. Summer 2002Ceng230 - Section 01 Introduction To C Programmingby Ahmet Sacan
Wed July 8, 2002
re-glance @ TOC
• Variables, Data Types
• Conditionals: if, ?, switch
• Loops: for, while, do-while
• Functions
• Arrays
• Strings
#include <math.h>sin(x) sine of x
cos(x) cosine of x
tan(x) tan of x
asin(x) arcsine of x, result between -pi/2 and +pi/2
acos(x) arccosine of x, result between 0 and +pi
atan(x) arctan of x, result between -pi/2 and +pi/2
atan2(y,x) arctan of (y/x), result between -pi and +pi
hsin(x) hyperbolic sine of x
hcos(x) hyperbolic cosine of x
htan(x) hyperbolic tan of x
exp(x) exponential function
#include <math.h> (cont'd)
log(x) natural logarithm
log10(x) logarithm to base 10
pow(x,y) x to the power of y (x^y)
sqrt(x) the square root of x (x must not be negative)
ceil(x) ceiling; the smallest integer not less than x
floor(x) floor; the largest integer not greater than x
fabs(x) absolute value of x
ldexp(x,n) x times 2**n
frexp(x, int *exp) returns x normalized between 0.5 and 1; the exponent of 2 is in *exp
modf(x, double *ip) returns the fractional part of x; the integral part goes to *ip
fmod(x,y) returns the floating-point remainder of x/y, with the sign of x
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>
...}
functionhead
functionbody
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 exampleint 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 );}
functionbody
return value
Function exampleint 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 ( );}
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;
i ii iii iv
X = 0 0 1 1
Y = 0 1 0 1
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();