EEE11 Lec09

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Lecture

   09:  More  Functions  


return-­‐type  function-­‐name  (parameter-­‐list,…)  
{  body…}  
 
¡  return-­‐type  is  the  data  type  of  the  return  
value  of  the  function  
¡  function-­‐name  is  the  name  of  the  function  
(you  don’t  say)  
¡  parameter-­‐list  is  the  list  of  inputs  that  the  
function  needs  
¡  body  is  the  code  that  the  function  needs  to  do  
2  
¡  You  should  define  your  function  first  before  you  use  it  
¡  Or,  if  the  definition  of  the  function  is  in  the  latter  part  
of  your  code,  you  can  prototype  your  function  before  
using  it:  
 
double  descriminant  (int,  int,  int);  
 
int  main(void){  
double  d  =  descriminant(1,  2,  3);  
…  
return  0;  
}  
 
double  descriminant  (int  a,  int  b,  int  c){  body…}  

3  
4  
¡  declared  inside  a  function  or  a  block  
¡  can  only  be  used  by  statements  of  that  
function  or  block  
¡  note  that  formal  parameters  or  arguments  of  
functions  are  considered  local  variables  of  
that  function  

5  
void printFirstEven(int n){!
!int a = 0;!
!
!while (a < n){!
! !a++;!
! !printf(“Even %d: %d\n”, a, a * 2);!
!}!
}!
!
int main(void){!
!int a = 0;!
!
!while (a < 20){!
! !printf(“a = %d\n”, a);!
! !a++;!
!}!
!printFirstEven(10);!
!
!printf(“Final a = %d\n”, a);!
!return 0;!
}!

6  
int main(void){!
int a = 5;!
!
printf("Value of a: %d\n", a);!
!
{!
int a = 3;!
printf("Value of a: %d\n", a);!
!
a++;!
printf("Value of a: %d\n", a);!
}!
!
printf("Value of a: %d\n", a);!
!
return 0;!
}!
!

7  
int main(void){!
int a = 5;!
!
printf("Value of a: %d\n", a);!
!
{!
a = 3;!
printf("Value of a: %d\n", a);!
!
a++;!
printf("Value of a: %d\n", a);!
}!
!
printf("Value of a: %d\n", a);!
!
return 0;!
}!
!

8  
¡  defined  outside  a  function  
¡  usually  on  the  top  of  the  source  code  
¡  can  be  accessed  by  any  function  

9  
int a = 0;!
!
void incrementGlobal(void){!
a++;!
printf("New global value: %d\n", a);!
return;!
}!
!
int main(void){!
!
printf("Value of a: %d\n", a);!
!
a = 27;!
printf("Value of a: %d\n", a);!
!
incrementGlobal();!
printf("Value of a: %d\n", a);!
!
return 0;!
}!

10  
int a = 0;!
!
void incrementGlobal(void){!
a++;!
printf("New global value: %d\n", a);!
return;!
}!
!
int main(void){!
!
int a = 30;!
printf("Value of a: %d\n", a);!
!
a = 27;!
printf("Value of a: %d\n", a);!
!
incrementGlobal();!
printf("Value of a: %d\n", a);!
!
return 0;!
}!

11  
int a = 0;!
!
void incrementGlobal(void){!
a++;!
printf("New global value: %d\n", a);!
return a;!
}!
!
int main(void){!
int a = 30;!
printf("Value of a: %d\n", a);!
a = 27;!
printf("Value of a: %d\n", a);!
! {!
! ! int a = 10;!
! ! printf("Value of a: %d\n", a);!
! ! incrementGlobal();!
! ! printf("Value of a: %d\n", a);!
! }!
incrementGlobal();!
printf("Value of a: %d\n", a);!
return 0;!
}!

12  
int a = 0;!
!
int incrementGlobal(void){!
a++;!
printf("New global value: %d\n", a);!
return a;!
}!
!
int main(void){!
int a = 30;!
printf("Value of a: %d\n", a);!
a = 27;!
printf("Value of a: %d\n", a);!
! {!
! ! int a = 10;!
! ! printf("Value of a: %d\n", a);!
! ! a = incrementGlobal();!
! ! printf("Value of a: %d\n", a);!
! }!
incrementGlobal();!
printf("Value of a: %d\n", a);!
return 0;!
}!

13  
14  
¡  function  that  calls  itself  

int example(int input){!


!if (input != 1)!
! !return input + example(input – 1);!
!else!
! !return input;!
}!

15  
¡  It  depends!  
¡  In  tree  traversing  algorithms  (big  words.  J),  
recursion  are  usually  used.  
¡  In  simple  stuff  such  as  the  previous  example,  
iteration  is  much  preferred.  
¡  Recursion  is  also  slower  and  takes  additional  
memory  (EEE  105  stuff)  

16  
17  
¡  Similar  to  functions  but  not  quite  J  
!
#define sum(a,b) a+b!
!
int main(void){!
!int num1 = 3;!
!int num2 = 5;!
!
!printf(“Sum is %d\n”, sum(num1, num2));!
!return 0;!
}!

18  
¡  The  pre-­‐processor  will  do  a  find-­‐and-­‐replace  
!
#define sum(a,b) a+b!
!
int main(void){!
!int num1 = 3;!
!int num2 = 5;!
!
!printf(“Sum is %d\n”, num1+num2);!
!return 0;!
}!

19  
¡  Error  prone  if  not  careful  
!
#define sum(a,b) a+b!
!
int main(void){!
!int num1 = 3;!
!int num2 = 5;!
!
!printf(“2x of sum is %d\n”,sum(num1,
num2)*2);!
!return 0;!
}!

20  
¡  Should  always  put  body  in  parenthesis  
!
#define sum(a,b) (a+b)!
!
int main(void){!
!int num1 = 3;!
!int num2 = 5;!
!
!printf(“2x of sum is %d
\n”,sum(num1,num2)*2);!
!return 0;!
}!

21  
¡  Another  example:  
!
#define square(a) a*a!
!
int main(void){!
!int num1 = 3;!
!
!printf(“Square is %d\n”, square(num1));!
!return 0;!
}!

22  
¡  Another  example:  
!
#define square(a) a*a!
!
int main(void){!
!int num1 = 3;!
!
!printf(“Square is %d\n”, square(num1+2));!
!return 0;!
}!

23  

You might also like