Lecture-Functions
Lecture-Functions
Week 10
Ms. Noor-ul-Huda
Lecturer
Department of Computer Science
College of Computer Science and Information Systems
noor.huda@iobm.edu.pk
C 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
Functions
What is a Function?
A function is a block of code that performs a specific task.
It is a modular approach to break down a large program into smaller,
manageable pieces.
Types of Functions
There are two types of functions in C programming:
1.Library Functions: are the functions which are declared in the C header files
such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
2.User-defined functions: are the functions which are created by the C
programmer, so that he/she can use it many times. It reduces the
complexity of a big program and optimizes the code.
Math
#include <math.h>
Use any math function
If c1 = 13.0, d = 3.0 and f = 4.0, then the statement
printf( "%.2f", sqrt( c1 + d * f ) );
©1992-2013 BY PEARSON EDUCATION, INC. ALL RIGHTS RESERVED.
©1992-2013 BY PEARSON EDUCATION, INC. ALL RIGHTS RESERVED.
Create your function
Choose a name
◦ Function should perform a single well defined task
◦ If you can’t find a concise descriptive name, you may have too many jobs for the function
Define a contract
◦ Inputs
◦ Arguments – choose type
◦ None should be noted as void
◦ Will the function change the parameter’s value?
◦ Output
◦ Only one ; by convention, 0 means good
Write prototype
◦ Looks like function header but has ;
◦ int square( int y );
◦ Tells compiler what is valid input and output
◦ Forces type conversion
Write body
Call the function
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
•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 ;
int square( int y );
Forces type conversion
Tells compiler what is valid input and output
Placement
◦ Applies to all functions appearing within the top level braces (or all if outside all
braces)
◦ Can put into its own .h file and then include without <>
◦ #include “myfunctions.h” (no semicolon)
No Overloading
• Every function name can have only one contract
Where do the variables
live?
On Stack: (lives and dies with function)
◦ Local – created in the function – automatic – on stack
◦ Argument – same as local
Pass by reference
◦ Argument accepts address: *<var name>
◦ Caller sends address: &<var name>
Variable life
◦ Local vs global