0% found this document useful (0 votes)
22 views

e102f20LectureMoreFunctions

Uploaded by

ekim73
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

e102f20LectureMoreFunctions

Uploaded by

ekim73
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

ENGR 102

Fall 2020
Even More on Functions
Functions

 Every C program must have a function


called main ( )

 Program execution always begins with


function main ( )

 Any other functions are subprograms


and must be called.
Function Calls

One function calls another by using the


name of the called function next to ( )
enclosing a parameter list.

A function call temporarily transfers control


from the calling function to the called
function.
Two Parts of Function Definition

int Cube ( int n ) heading


{
body
return n * n * n ;

}
What is in a heading?
type of value name
returned of parameter list
functio
n

int Cube ( int n )


What is in a prototype?
A prototype looks like a heading
but must end with a semicolon;
and its parameter list just needs
to contain the type of each
parameter.

int Cube ( int ); //


prototype
When a function is called,
Temporary memory is set up ( for its value
parameters and any local variables, and also for
the function’s name if the return type is not void).

Then the flow of control passes to the first statement


in the function’s body. The called function’s body
statements are executed until one of these occurs:
return statement (with or without a return value),
or,
closing brace of function body.

Then control goes back to where the function was


called.
#include <stdio.h>
int Cube ( int ) ; //prototype
int main ( )
{
int YourNumber ; actual parameters
int MyNumber ;
YourNumber = 14 ;
MyNumber = 9 ;
printf(“\nMy Number = %d \n“,MyNumber);
printf(“its cube is %d \n“,Cube (MyNumber));
printf(“Your Number = %d \n”“ << YourNumber ;
cout << “its cube is %d \n”,Cube (YourNumber));
return 0;
}
To compile successfully,

 Beforea function is called in your


program, the compiler must
previously process either the
function’s prototype, or the
function’s definition (heading and
body).
A C Function
 Can return in its identifier at most 1
value of the type which was
specified (called the return type) in
its heading and prototype.
 But, a void-function cannot return
any value in its identifier.
Write a void Function

called DisplayMessage ( ) which you can call


from main ( ) to describe the pollution
index value it receives as a parameter.

Your city describes a pollution Index


less than 35 as “Pleasant”,
35 through 60 as “Unpleasant”,
and above 60 as “Health Hazard.”
formal parameter

void DisplayMessage(int Index)


{
if(Index < 35)
printf(”\nPleasant \n”);
else if(Index <= 60)
printf(“Unpleasant \n”);
else
printf(“Health Hazard \n”;
}
THE REST OF THE PROGRAM
#include <stdio.h>

void DisplayMessage (int); //prototype

int main() actual parameter


{
int PollutionIndex;

printf(“\nEnter air pollution index:\


t”);
scanf(“%d”,&PollutionIndex);
DisplayMessage(PollutionIndex); //call
return 0;
}
return ;
 is valid only in the body block of void
functions

 causes control to leave the function


and immediately return to the calling
block leaving any subsequent
statements in the function body
unexecuted.
Header Files Contain
 named constants like
const int INT_MAX = 32767;
 new type definitions (besides int, char, etc.)
 function prototypes like
float pow( float, float );
float sqrt( float );
char toupper( char
); int
islower( char );
Program With Several
Functions
function prototypes

main( ) function definition

Square( ) function definition

Cube( ) function definition


Value-Returning Functions
#include <stdio.h>

int Square (int) ; // prototypes


int Cube (int) ;
int main ( )
{
// function call
printf(“\nThe square of 27 is %d :\n”,Square (27));
// function call
printf(“The cube of 27 is %d :\n”,Cube (27));

return 0;
}
Rest of Program

int Square (int n) // function prototype and body


{
return(n*n);
}

int Cube (int n) // function prototype and body


{
return (n*n*n);
}
A void function call stands alone
#include <stdio.h>
void DisplayMessage ( int ) ; //
prototype

int main ()
{
DisplayMessage(15); // function
call

printf(“\nGood Bye \n“);

return 0;
}
A void Function Does NOT
Return a Value

void DisplayMessage ( int n )


{
printf(“\n I have liked math for %d years \n”,years);
}
Parameter List

 is the means used for a function to share


information with the block containing the call.
Parameters are classified by
their location

Actual Parameters Formal Parameters


Always appear in Always appear in
a function call the function
within the calling heading, or
block. function prototype.
Some C Textbook Terminology
 Use the term “arguments” for actual
parameters

 Those books then refer to formal parameters


as just “parameters”
Questions
 What is a function used for?
To do a task and cut down on the amount
of detail in your main program.
 Can one function call another function?

Yes
 Can a function even call itself?

Yes, that is called recursion. It is very


useful and requires special care in writing.
More Questions
 Does it make any difference what names you
use for formal parameters?
NO. Just use them in function body.
 Do formal parameter names and actual
parameter names have to be the same?
NO.
 What is the advantage of that? It seems
confusing.
Functions Are Written To
Specifications
 The specifications state the return type, the
parameter types, whether any parameters
are “outgoing,” and what task the function is to
perform with its parameters.

 The advantage is that teamwork can occur


without knowing what the actual parameter
names will be.

You might also like