cst181-1 Lecture8

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

CST 181­1

structured Programming
Laboratory Session 08
Home work Assignment
Reasons to Use Functions

 Break a complex task into subtasks.


 Eliminates duplicating program instructions.
Functions

 Two types:
Standard library functions(built­in)
User defined functions
 Two varieties:
Those with return values
(Produces a value that can be assigned to a
variable.)
Those with none.
Functions
Prototypes of functions
int main( )
{
..........
}
function_1( )
{
...........
}
function_2( )
{ .........}
Functions

 Each function has a unique name and when the


name is encountered while in execution the
control of the program is transferred to the
statement(s) within the function.
 When the function is ended (returns)the control
is resumed to the next statement following the
function call.
Advantages of Functions

 Well designed functions perform a specific and


easily understood task.
 Complicated tasks should be broken down into
multiple functions and then each can be called
in the proper order.
Function Prototypes
 Using functions in your program requires that you
first declare the function (the prototype). Later you
can implement the function.
 The prototype tells the compiler in advance about
some characteristics(name of the function, return
type and parameters) of a function.
 A function prototype takes the form:
type function­name(type argument­1, type
argument­2, .....);
Examples for prototypes:
void exit(int x);
int kbhit( );
double power(double x, double y);
 The first function returns no value and it takes
an integer argument.
 Second prototype returns an integer value and
has no inputs.
 Third function returns a double value and takes
two double values.
Function Definition
 The definition of a function is the actual body of the
function. It starts with function header, which is the
same as the function prototype but does not end
with a semicolon.
 The function prototype and the definition must
have exactly the same return type, name and
parameter list.
 Function definition takes the form:
Return­type function­name(type argement­1, type argument­2, ..... )
{ Statement(s);
}
Example For Function

Following function which accepts an integer as


the input and returns its square.
int square(int x)
{
int y;
y = x*x;
return y;
}
Example ­01

 Write a program to calculate the square of


given integer number.
Sample Code

int main()
{
int p;
printf("Enter the number:\n");
scanf("%d",&p);
printf("square is %d\n",square(p));
}
Example­02

 Write a program to calculate the commission for


user given sales amount.
commission = 50000 + 0.10 * sales;
Sample Code
Example ­03

 Write a program to calculate the factorial of a


given number.
(Use a function to calculate factorial)
Sample code
In Class Assignment

 Write a C program to calculate the


circumference and area of a circle given its
radius.Implement calculation of circumference
and areas as separate functions.
Sample Code
Passing Variables to Functions

 You can use as many functions as you need in


your programs and you can call a function from
another or even from it­self.
 In order to avoid errors when using functions,
you have to have a clear understanding about
the mechanism of passing variables from one
function to another.
The Scope of a Variable

 A variable has a scope, which determines how


long it is available to your program (or function)
and where it can be accessed from.
 Variables declared within a block are scoped
only to that block; they can be accessed only
within that block and go out of existence when
the execution of the block is completed.
The Scope of a Variable

 A variable declared inside a function is called a


local variable . Scope of a local variable is
limited to the function.
 Such variables are not seen by any other
functions including the main function.
 such as the variable x in Example 01
The Scope of a Variable

 On the other hand you can also define global


variables which are accessible from any
function within the same source file.
 The global variable can be defined within the
program but anywhere outside function block
including the main function.
Global Variable­Example
 For example you can define a global variable
“discount” as follows:
#include <stdio.h>
float discount; //global variable
float sub_total(float total); //function
prototype
int main()
{
....
Best Practices

 A well written C source code should not have


any global variables unless they are specifically
required since an accidental change to a global
variable may produce erroneous results.
Excersice

 Predict the output of each printf function in


Program
Code
Thank you!

You might also like