Lecture 08
Lecture 08
Lecture 08:
Functions (Part I)
• Uses of functions
• Functions
• Parameters
• Return values
Uses of functions
procedure sayHello
{
output “Hello World!”
}
Main Program
{
do procedure sayHello
}
Example: hello1.c #include <stdio.h>
/*
Prints a simple greeting. * Print a simple greeting.
*/
/*
* Call a function which prints a
* simple greeting.
*/
int main()
Main Program
{ {
return 0;
} }
Example: hello1.c #include <stdio.h>
/*
* Print a simple greeting.
*/
/*
* Call a function which prints a
* simple greeting.
*/
int main()
{
return 0;
}
Example: hello1.c #include <stdio.h>
/*
Function * Print a simple greeting.
*/
name
void sayHello ( void )
{
Function body printf(“Hello World!\n”);
}
/*
* Call a function which prints a
* simple greeting.
*/
int main()
{
sayHello();
return 0;
}
Example: hello1.c #include <stdio.h>
/*
Return type * Print a simple greeting.
*/
/*
Formal * Call a function which prints a
* simple greeting.
Parameter List */
int main()
{
sayHello();
return 0;
}
Parameters
• Information passed to a function
• “Formal” parameters are local variables
declared in the function declaration
• “Actual” parameters are variables or
values passed to the function when it is
called
Example:
badsort.c
if ( a > b )
{
printf("%d %d\n", b, a);
Parameters (aka
} Arguments)
else
{
printf("%d %d\n", a, b);
}
}
Example: badsort2.c
void badSort ( a, b )
int a; Old style.
int b;
{
if ( a > b )
{
printf("%d %d\n", b, a);
}
else
{
printf("%d %d\n", a, b);
}
}
Example:
badsort.c
if ( a > b )
{
printf("%d %d\n", b, a);
}
else
{
printf("%d %d\n", a, b);
}
}
Example:
badsort.c Formal
parameters
/* Print two numbers in order. */
Output: 3 5
Example: badswap.c
int main()
/* Swap the values of two variables. */ {
int a = 3, b = 5;
void badSwap ( int a, int b )
{ printf("%d %d\n", a, b);
int temp; badSwap ( a, b );
printf("%d %d\n", a, b);
temp = a;
a = b; return 0;
b = temp; }
Output: 3 5
5 3
Example: badswap.c
int main()
/* Swap the values of two variables. */ {
int a = 3, b = 5;
void badSwap ( int a, int b )
{ printf("%d %d\n", a, b);
int temp; badSwap ( a, b );
printf("%d %d\n", a, b);
temp = a;
a = b; return 0;
b = temp; }
Output: 3 5
5 3
3 5
Example: badswap.c
int main()
/* Swap the values of two variables. */ {
int a = 3, b = 5;
void badSwap ( int a, int b )
{ printf("%d %d\n", a, b);
int temp; badSwap ( a, b );
printf("%d %d\n", a, b);
temp = a;
a = b; return 0;
b = temp; }
void sayHello ()
{
Also valid: printf(“Hello World!\n”);
}
if (a > b)
{
result = a;
}
else
{
result = b;
}
return result;
}
Example:
maxmin.c
/* Returns the larger of two numbers. */
return result;
is the integer 7.
}
Example:
maxmin.c
This style okay. /* Returns the larger of two numbers. */
int
max (int a, int b)
{
int result;
if (a > b)
{
result = a;
}
else
{
result = b;
}
return result;
}
Example:
maxmin.c
return b;
}
Note:
Changing the value of b does not
affect the actual parameter in the
function call.
Example:
maxmin.c
#include <stdio.h>
/* Returns the smaller of two numbers. */
/* Returns the larger of two numbers. */
int min (int a, int b)
int max (int a, int b) {
{ if (a < b)
if (a > b) {
{ return a;
return a; }
} else
else {
{ return b;
return b; }
} }
}
int main()
{
int maxOfMins = max(min(1,2), min(3,4));
Or you can write
printf("%d\n",
printf("%d\n", maxOfMins);
max(min(1,2), min(3,4)));
return 0;
}
Return Values (cont.)