Lecture 9
We have already known that in a C program,
there must be a main () function. We use to call this main () function as the
brain. Without brain, can you imagine any human? No! Similarly without a main
() function, you cannot imagine a C program! In C, we used many library
functions as well- printf (), scanf (), clrscr (), or getch (). printf() prints
on the output, scanf () takes input from the user and assigns that input to a
variable by going to the variable’s address, clrscr () clears the output buffer
and getch () waits for a key-stroke from the user. Well, if you think uniform
one thing is common to say- the function does one particular job. Isn’t it? In
C, user can define functions as well- as many functions as they wish. If you
have a program to take two integers from the user and add, subtract, multiply,
divide and mod you can define 5 different functions and ask each of them to do
that job respectively. How? We will take a look at it later!
Now let’s take a look at an example.
#include<stdio.h>
#include<conio.h>
void
message();
void
main(){
clrscr();
message();
printf("This is inside main
function");
getch();
}
void
message(){
printf("This is inside message
function\n");
}
-
Before
starting main () function, we have written void message ();This is called function prototyping.
-
Inside
main () function
we have just called the message () function with its name.
-
The
control passes to the message () function and prints This is
inside message function.
Then the cursor goes to the new line.
-
The
control then finds the ending brace of message () function and returns to the line
immediately after the calling place.
-
It
then prints This is inside main function.
What do we mean when we say that main( ) ‘calls’
the function message( )? We mean that the control passes to the function
message( ). The activity of main( ) is temporarily suspended; it
falls asleep while the message( ) function wakes up and goes to work.
When the message( ) function runs out of statements to execute, the
control returns to main( ), which comes to life again and begins
executing its code at the exact point where it left off. Thus, main( ) becomes
the ‘calling’ function, whereas message( ) becomes the ‘called’
function.
Let us see another example.
#include<stdio.h>
#include<conio.h>
void italy ();
void brazil ();
void argentina ();
void
main(){
clrscr();
printf("I am in main
function\n");
printf("I came back in main
function");
getch();
}
void italy (){
printf("Italy : 1934 1938 1982
2006\n");
}
void brazil (){
printf("Brazil : 1958 1962 1970 1994
2002\n");
}
void argentina (){
printf("Argentina : 1978 1986\n");
}
From this example, we can come across many new knowledge-
§ There is no limit on the number of
functions that might be present in a C program.
§ Each function in a program is called
in the sequence specified by the function calls.
§ After
each function has done its job, control returns to the place of calling that
function.
Well, then, should all the function calls take
place inside the main () function? No! Functions defined by the user can call
other functions as well. Let us see another example.
#include<stdio.h>
#include<conio.h>
void italy ();
void brazil ();
void argentina ();
void main(){
clrscr();
printf("Beginning with main
function\n");
printf("I came back in main function
at last");
getch();
}
void italy (){
printf("Italy : 1934 1938 1982
2006\n");
}
void brazil (){
printf("Brazil : 1958 1962 1970 1994 2002\n");
}
void argentina (){
printf("Argentina : 1978 1986\n");
}
Since the compiler always begins the program
execution with main( ), every function in a program must be called
directly or indirectly by main( ). In other words, the main( ) function
drives other functions.
Summary
of Preliminary Knowledge on Functions
1. C program is a collection of one or
more functions.
A function gets called when the function name
is followed by a semicolon. For example,
#include<stdio.h>
#include<conio.h>
void argentina ();
main( )
{
}
2. A function is defined when function
name is followed by a pair of braces in which one or more statements may be
present. For example,
{
statement
1 ;
statement
2 ;
statement
3 ;
}
3. Any function can be
called from any other function. Even main( ) can be called from other
functions. For example,
#include<stdio.h>
#include<conio.h>
void message();
void
main( ){
message( ) ;
}
message(
){
printf ( "\nCan't imagine life
without C" ) ;
main( ) ;
}
4.
A function can be called any number of times. For example,
#include<stdio.h>
#include<conio.h>
void message();
main( ){
message(
) ;
message(
) ;
}
message( ){
printf
( "\nJewel Thief!!" ) ;
}
5.
The order in which the functions are defined in a program and the order
in which they get called need not necessarily be same. For example,
#include<stdio.h>
#include<conio.h>
void
message1();
void message2();
main( ){
message1(
) ;
message2(
) ;
}
message2( ){
printf
( "\nBut the butter was bitter" ) ;
}
message1( ){
printf
( "\nMary bought some butter" ) ;
}
Here, even
though message1( ) is getting called before message2( ), still, message1(
) has been defined after message2( ). However, it is advisable to
define the functions in the same order in which they are called. This makes the
program easier to understand.
6.
A function can call itself. Such a process is called ‘recursion’. We
would discuss this aspect of C functions later in this chapter.
7.
A function can be called from other function, but a function cannot be
defined in another function. Thus, the following program code would be wrong,
since argentina (
) is being defined inside another function, main( ).
main( ){
printf
( "\nI am in main" ) ;
printf ( "\nI am in argentina "
) ;
}
}
8. There are basically two
types of functions:
Library functions Ex. printf( ), scanf( ) etc.
User-defined functions Ex. argentina ( ), brazil (
) etc.
You remember, I told you about a C program where we can use 5 functions
for 5 different operations on 2 integers? Now let us see what I meant.
#include<stdio.h>
#include<conio.h>
void
sum();
void
sub();
void
mul();
void
div();
void
mod();
void
main(){
sum();
sub();
mul();
div();
mod();
getch();
}
void
sum(){
int a,b;
printf("Enter 2 integers: ");
scanf("%d %d",&a,&b);
printf("Summation: %d",a+b);
}
void
sub(){
int a,b;
printf("Enter 2 integers: ");
scanf("%d %d",&a,&b);
printf("Subtraction: %d",a-b);
}
void
mul(){
int a,b;
printf("Enter 2 integers: ");
scanf("%d %d",&a,&b);
printf("Multiplication:
%d",a*b);
}
void
div(){
int a,b;
printf("Enter 2 integers: ");
scanf("%d %d",&a,&b);
printf("Division: %d",a/b);
}
void
mod(){
int a,b;
printf("Enter 2 integers: ");
scanf("%d %d",&a,&b);
printf("Modulus: %d",a%b);
}
Call by Value
From what we have seen so far, we can say that
in a C program with functions, we can have two sections-
Function Declaration or function prototyping
Function Definition or function body
The basic format of function definition is-
return-type
function-name (data_type parameter_1, data_type parameter_2,…,
data_type parameter_n){
declarations;
statements;
}
§ We have seen only void as the
return-type in functions so far. But it can be of any valid C data type. It
specifies what type of data the function will hand over to the caller.
§ Parameter-list is a comma separated
list containing declaration of parameters received by the function when it is
called. We have also seen only void in the parameter-lists so far (it means the
function does not get any value from the caller).
Let’s see a variation of function from what we
have seen so far-
#include<stdio.h>
#include<conio.h>
int
square(int); // function prototype
main(){
int x,y;
for (x=1;x<=10;x++){
y=square(x); //we called square with
current value of x
// the square gets it
into z and returns z*z
// the value is assigned
into y
printf (“The square of %d is %d \n”,x,y);
}
}
//function
definition
int
square(int z){
return z*z;
}
In this example, we have printed the square of
integers from 1 to 10. we have a function prototype int square(int)
which means that the
program uses a function named square which will return an integer value (return
type) and which will get an integer value from the caller (parameter). Then,
for values of 1 to 10 of x, we have called the function square by passing the
current value of x. The function square gets the value of x into z and returns
z*z to the caller. This value is assigned to another variable y. We then
printed the value as the square of x.
Let’s now see an example that will tell us the
maximum of three integers using a function called maximum().
#include<stdio.h>
#include<conio.h>
int
maximum(int,int,int); // function prototype
main(){
int x,y,z;
printf(“Enter three integers: ”);
scanf(“%d %d %d”,&x,&y,&z);
printf (“The maximum is
%d”,maximum(x,y,z));
}
//function
definition
int
maximum(int a,int b, int c){
int max=a;
if(b>max)
max=b;
if(c>max)
max=c;
return max;
}
The variables x, y and z are
called ‘actual arguments’, whereas the variables a, b and c are
called ‘formal arguments’. Any number of arguments can be passed to a function
being called. However, the type, order and number of the actual and formal
arguments must always be same.
Instead of using different variable names x,
y and z, we could have used the same variable names a, b
and c. But the compiler would still treat them as different
variables since they are in different functions.
There is no restriction on the number of return
statements that may be present in a function. Also, the return statement
need not always be present at the end of the called function. The following
program illustrates these facts.
1 comments:
This is great blog! Keep it up. chemical cleaning aircon
Post a Comment