Upload
jewel-cunningham
View
213
Download
0
Embed Size (px)
Citation preview
FUNCTIONS AND STRUCTURED PROGRAMMING
CHAPTER 10
Introduction
A c program is composed of at least one function definition, that is the main() function.
Execution of the program begins with main() and also ends with the main() function.
However, a C program can also be composed of other functions aside from the main().
Consider the program below…
Write a program that will print the lines Hi! Hello! How are you? using several functions aside from the main function.
#include<stdio.h>main(){
clrscr( );printf (“Hi”);greet1( );greet2( );getch( );
}greet1 ( ){
continue,….
printf ( “Hello”);}
greet2 ( ){ printf (“How are you”);}
Sample Output:
Hi! Hello! How are you?
The c program presented in previous slide is composed of 3 functions: the main function, the function greet1 and the function greet2.
Therefore we can say that we can create a program that is composed of other function aside from the main function.
Note: The main( ) function should always be present in every C program.
Functions Defined
Functions are the building blocks of C in which all program activity occurs.
A function is also called a subprogram or subroutine. It is a part of a C program that performs a task, operation or computation then may return to the calling part of the program.
Other functions aside from the main( ) can only be executed by the program through a “function call”.
Note: Function call is a C statement that is used to call a function to execute C statements found inside the function body.
Functions Defined
Going back to the example, greet1( ); is an example of a function call, calling the function greet ( ).
main ------clrscr------printf greet1( ) function call------greet1( ) function
greet2 function call----greet2( ) function
getch( )
General form of a function
Where function_type specifies the type of value that the function will
return. function_name is any valid identifier name which will name
the function. Parameter list is a comma separated list of variables that
receive the values when the function is called. body of the function is composed of valid c statements that
the function will execute.
function_type function_name (parameters list){
body of the function;}
General form of a function
Example:
int subtractor (int x, int y)
{
int z;
z = x – y;
return (z);
}
Function type
Function name
Parameter list
Body of the function
Local variable declaration
Note: Return statement is a c statement that is used to return a value in a c program.
Type of functions
Void Functions – which does not return any value when invoked.
Function that returns a value once invoked.
Example of Void Functions
#include<stdio.h>main(){
clrscr( );printf (“Hi”);greet1( );greet2( );getch( );
}greet1 ( ){
continue,….
printf ( “Hello”);}
greet2 ( ){ printf (“How are you”);}
Sample Output:
Hi! Hello! How are you?
Example of Functions that returns a value once invoked.
Sample Output:
-1
int subtractor (int x, int y) { int z; z = x – y; return (z);}
#include<stdio.h>main(){
clrscr( );int a,b,;b=4; a=3;a=subtractor(a,b);printf(“%d”, a);
}
Actual and Formal Parameters
Actual Parameters are the variables found in the function call whose values will be passed to the formal parameters of the called function.
Formal Parameters are the variables found in the function header that will receive from the actual parameters.
Actual and Formal Parameters #include<stdio.h>
main(){
int area, x,y;x=25, y=10;area= Compute (x,y);printf(“%d”,area);
}Compute (int L, int W){
int a;a=L*W;return a;
}
x and y are the Actual parameters
L and W are the Formal parameters
Note: The actual parameter and formal parameter must agree in data type.
The actual and formal parameter must have a one-to-one correspondence.
Call by value and Pass by value
In the method call by value, the values of the actual parameters are passed to the formal parameters.
Call by value
In the method call by value, the values of the actual parameters are passed to the formal parameters.
Changes that happen to the values of the formal parameters inside the function will not affect the values of the actual parameters.
Call by value Example:
#include<stdio.h>main(){
clrscr();x=10;y=5;printf(“%d %d\n”, x,y);pass (x,y);printf(“%d %d\n”,x,y);getch();
}pass (int a, int b){ a=a+5;
b=b*2;printf(“%d %d \
n”,a,b);}
Sample Output:
10 5
15 10
10 5
Pass by value or Call by reference
The actual parameters also pass their value to the formal parameters.
But the changes that happen to the values of the formal parameters inside the function will affect the values of the actual parameters.
This is because the actual address of the variables is passed using the address of operator (&) together with the pointer operator (*).
Pass by value or Call by reference Example:
#include<stdio.h>main(){
clrscr();x=10;y=5;printf(“%d %d\n”, x,y);pass (&x,&y);printf(“%d %d\n”,x,y);getch();
}pass (int *a, int *b){ *a=*a+5;
*b=*b*2;printf(“%d %d \
n”,*a,*b);}
Sample Output:
10 5
15 10
15 10
<#include<math.h>
sqrt(x) fabs(x) - calculates the absolute value of a number
ceil(x) - ceil (11.25)=12 floor(x) - floor(11.25)=11 sin(x) cos(x) tan(x) pow(x)