You are on page 1of 6

C PROGRAMMING NOTES

FUNCTIONS

What is a function?

A function is a self-contained block of statements that perform a coherent


task of some kind. Every C program can be thought of as a collection of
these functions. As we noted earlier, using a function is something like
hiring a person to do a specific job for you.

Let us now look at a simple C function that operates. Actually we will be


looking at two things:
1) a program( a function) that calls function and
2) the function inself.
Ex:-

#include<stdio.h> Smile, and the world smiles with you


#include<conio.h> ok! BYE BYE…
void main()
{
clrscr();
message();
printf("\n ok! BYE BYE… ");
getch();
}

message()
{
printf("Smile, and the world smiles with you ");
}
Explanation:-

Here through main() function we are calling the function message().


What do we mean when we say that main() ‘calls’ the function
message()? We mean that the control passes to the function message().
At that time the activity of the main() function is temporarily
suspended and it falls asleep while the message() function activates.
When the message() function completes the execution of all the
statements that it has to execute then the control returns to the main()
function, which wakes up again and begins executing its code at the
exact point where it left off. That means starts executing from the next
line that comes after the line, where the function is called. Thus the
main() is called the ‘calling’ function, whereas message() becomes the
‘called’ function.

Ex:-

#include<stdio.h> Smile, and the world smiles with you


#include<conio.h> ok! BYE BYE…
void main() Italy is country of culture and arts.
{
clrscr();
message();
printf("\n ok! BYE BYE… ");
italy();
getch();
}

message()
{
printf("Smile, and the world smiles with you ");
}
italy()
{
printf("\n Italy is country of culture and arts. ");
}

So, from this a number of conclusion can be drawn:-

1) Any C program contains at least one function, that is main().


2) If C program contains more than one function, then one (and only
one) of these functions must be the main() function,. Because
program execution always starts with the main() function.
3) There is no limit on the number of functions that a C program can
contain.
4) Each function in a C program is called in the sequence specified
by the function calls in the main().
5) After each function has done its job, control returns to that
function from where it has already called. When main() runs out
of function calls and rest of the jobs after that the program ends.
The general from of a function:-

Function (arg1, arg2, arg3)


{ Statement 1;
Statement 2;
Statement 3;
}

 A function is called when the function is followed by a


semicolon.
void main()
{
argentina (); [ here the function argentina() is called]
}
 A function is defined when the function name is followed by a
pair braces in which one or more statement is present. Example:-

argentina ()
{
printf(" we r in argentina.");
}
 any function can be called any number of times.
 The order in which the functions are defined and the order in which
they get called need not necessarily be same. For example:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Intially we r in the main function.\n");
message(); Intially we r in the main function.
italy();
printf("\nBye bye"); Be happy
getch(); Italy is a city of art and cultutre
}
italy()
{
printf("\n Italy is a city of art and cultutre");
}
message()
{
printf("\n Be happy");
}

Here in case of definition function italy () comes first then the


function message(). But in case of calling message() comes first and
then italy().

Passing values between functions and returning values from a


function:-

Function call by value:-

When we call a function and pass the ‘values’ of variables to the


called function then such function call is called ‘call by value’.
By this what we mean is, on calling a function we are passing value of
variables to it. Lets consider the following example:-

Ex:- write a program that takes two integer as inputs and calculates
the sum of them through a called /( user defined) function and returns
the returns the result from that function.
#include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c;
clrscr();
printf("Intially we r in the main function.");
message(); Intially we r in the main function.
printf("\nEnter the numbers\n"); Be happy
scanf("%d %d", &a, &b); Enter the numbers
c=sum(a,b); 5
printf("\n so, the result is %d", c); 10
printf("\nBye bye"); Italy is a city of art and culture
getch(); so, the result is 15
} Bye bye
message()
{
printf("\n Be happy");
}
italy()
{
printf("\n Italy is a city of art and culture");
}
int sum(int x, int y)
{ int z;
z=x+y;
italy();
return c;
}
Here from the main() function the passed values are a and b to the
argument x and to the argument y of the user defined function sum(int
x, int y). And the returned value is z from the user defined function
sum(…) to the calling function main(). And the returned value is
received by the variable c.

Function call by reference:-

When we pass the address of a variable to a function, then it is called


a ‘call by reference’. This operation can be done by using the concept
of pointers.
Ex:- Write down a program that will find out the area and perimeter of
a circle, where the radius, the address of the variable( area) which
holds the area of the circle and the address of the variable( perimeter)
which holds the perimeter of the circle is passed to called function.
#include<stdio.h>
#include<conio.h> Enter the radius of the circle
void main (void) 5
{ int radius; So, the area is 78.500000
float area,perimeter; So, the perimeter is 31.400000
clrscr();

printf ("Enter the radius of the circle\n");


scanf("%d",&radius); 4000 5 radius
areaperi(radius,&area,&perimeter); 4002 78.500000 area
printf("\n So, the area is %f.",area); 4006 31.400000 perimeter
printf("\n So, the perimeter is %f",perimeter);
getch();
}
areaperi(int r,float *a,float *p)
{ *a=3.14*r*r;
*p=2*3.14*r;
}

Here address of area=&area=4002 and


address of perimeter=&perimeter=4006

so, *area=*(4002)=3.14*5*5=78.500000
*perimeter=*(4006)=2*3.14*5=31.400000

You might also like