Unit 5 Functions
Unit 5 Functions
Objectives
• Create functions
• Function prototypes
• Parameters
– Pass by value or reference
– Sending a reference
• Return values
• Math functions
Intro
• Why:
– Divide and conquer
– Reuse abstractions
– Don’t rebuild the bridge
• What:
– Used prepackaged functions
• printf, scanf, rand()
– Create our own
• main
– Pass parameters
– Accept return values
C FUNCTIONS
Some definition: A function is a named, independent section of C code that
performs a specific task and optionally returns a value to the calling program
or/and receives values(s) from the calling program.
www.tenouk.com, © 7/66
Sample Function
#include <stdio.h>
int square ( int y ); // function prototype
// function main begins program execution
int main ( void )
{ int x; // counter
for ( x = 1; x <= 10; x++ ) {// loop 10 times and calc square of x each time
printf ( "%d ", square ( x ) ); // function call
}
puts (""); // add a blank line
}
// square function returns the square of its parm
int square ( int y ) // y is a copy of the x sent
{
return y * y; // returns square of y as an int
}
return
• return serves two purposes:
– It tells the computer the value to return as the
result
– It tells the computer to leave the function
immediately and return the calling function (or
the main program).
• Void return:
– Ex: void printit ( int x );
– You can still return to leave, but without a value
Prototypes
• Looks like function header but has ;
• char square( int y );
• Int square(char z);
• Forces type conversion
• Tells compiler what is valid input and output
• No Overloading
• Every function name can have only one contract
C FUNCTIONS
Passing an array to a function What are the output and
#include <stdio.h> the content of num &
// function prototype
mood variables after
void Wish(int, char[ ]); program execution was
void main(void) completed?
{
Wish(5, "Happy");
}
www.tenouk.com, © 53/66
C FUNCTIONS
#include <stdio.h>
void Rusted(char[ ]);
Build this program, show
the output & what it do?
void main(void)
{
// all work done in function Rusted()...
Rusted("Test Test");
printf("\n");
}
void Rusted(char x[ ])
{
int j;
printf("Enter an integer: ");
scanf_s("%d", &j);
for(; j != 0; --j)
printf("In Rusted(), x = %s\n", x);
}
Any data
number 4.0 y intended for y
in the
function goes
to the
location of
number in the
main
program
Use of math functions in C
// the use of ceil function.
#include <stdio.h>
#include <math.h>
int main ()
{
float val1, val2, val3, val4;
val1 = 1.6;
val2 = 1.2;
val3 = -2.8;
val4 = -2.3;
printf ("value1 = %.1lf\n", ceil(val1));
printf ("value2 = %.1lf\n", ceil(val2));
printf ("value3 = %.1lf\n", ceil(val3));
printf ("value4 = %.1lf\n", ceil(val4));
return(0);
}
String Functions in C
// C program to illustrate
// strcmp() function
#include <stdio.h>
#include <string.h>
int main()
{
char leftStr[] = "g f g";
char rightStr[] = "g f g";
// Using strcmp()
int res = strcmp(leftStr, rightStr);
if (res == 0)
printf("Strings are equal");
else
printf("Strings are unequal");
printf("\nValue returned by strcmp() is: %d", res);
return 0;
}
C program to find GCD of
two numbers
/ C program to find GCD of two numbers
#include <stdio.h>
// Recursive function to return gcd of a and b
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Driver program to test above function
int main()
{
int a = 98, b = 56;
printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
return 0;
}
Recursion – Function calls itself
• Method for repetition
• Need a stopping condition
• Need to call with some way to reach the stop
eventually
• Pushes copies of itself onto the stack (memory
use)
©1992-2013 by Pearson Education, Inc. All
Rights Reserved.
©1992-2013 by Pearson Education, Inc. All
Rights Reserved.
©1992-2013 by Pearson Education, Inc. All
Rights Reserved.