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.
Note: Every identifier which has () that is paranthesis is a function for eg main() is a function.
Let us now look at a simple C function that operates in much the same way as the mechanic. Actually, we will be looking at two things—a function that calls or activates the function and the function itself.
main( ) //main function
{
void message( ) ; //function declaration or prototype
message(); //function call
printf ( "\nWelcome to functions" ) ;
}
void message( ) //function definition
{
printf ( "\nWelcome" ) ;
}
And here’s the output...
Welcom
Welcome to functions
Here, main( ) itself is a function and through it 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( ). 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.
If you have grasped the concept of ‘calling’ a function you are prepared for a call to more than one function. Consider the following example:
main( )
{
void italy( ) ;
void brazil( ) ;
void argentina( ) ;
printf ( "\nI am in main" ) ;
italy( ) ;
brazil( ) ;
argentina( ) ;
}
void italy( )
{
printf ( "\nI am in italy" ) ;
}
void brazil( )
{
printf ( "\nI am in brazil" ) ;
}
void argentina( )
{
printf ( "\nI am in argentina" ) ;
}
The output of the above program when executed would be as under:
I am in main
I am in italy
I am in brazil
I am in argentina
From this program a number of conclusions can be drawn:
− Any C program contains at least one function.
− If a program contains only one function, it must be main( ).
− If a C program contains more than one function, then one (and
only one) of these functions must be main( ), because program
execution always begins with main( ).
− 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 in main( ).
− After each function has done its thing, control returns to
main( ).When main( ) runs out of function calls, the program
ends.
A Function has three parts :
- Function declaration or function prototype
- Function call
- Function definition
It specifies the name of the function, its return type and its arguments. It should be before the main method or inside the main method. For example void message();
Note: It should end with a ';' (semicolon);
Function call :
Function call is done by the calling function to invoke that function. When function call is made then the control is transffered to the function definition. It should be in a method which act as calling method.
Function Definition :
It consists of function body that is the respective statements which execute when the function is invoked.
For eg: void message()
Note:
- The return type and the arguments should be the same as specified in function declaration or prototype . For eg: message has void return type and no argument as we specified in function declaration.
- Function definition should not end with ';'(semicolon)
Syntax:
void main()
{
// Function prototype([ ]);
// Function Call([ ]);
}
// Function definition([ ]);
{;
}
Types of functions :
There are two types of functions :
- Built in functions
- User defined functions
These functions are also called as 'library functions'. These functions are provided by system. These functions are stored in library files. e.g.
- scanf()
- printf()
- getch()
- getche()
- clrscr();
- gotoxy(int x,int y);
- strlen
- strcat
Some in-built functions tutorial section
click on the above link to go to this section.
2. User defined functions
The functions which are created by user for program are known as 'User defined functions'.
As for example :
main( )
{
void italy( ) ;
void brazil( ) ;
void argentina( ) ;
printf ( "\nI am in main" ) ;
italy( ) ;
brazil( ) ;
argentina( ) ;
}
void italy( )
{
printf ( "\nI am in italy" ) ;
}
void brazil( )
{
printf ( "\nI am in brazil" ) ;
}
void argentina( )
{
printf ( "\nI am in argentina" ) ;
}
Here we have created 3 user-defined functions-
italy(), brazil() and argentina
Note that the calling function that is main() is a built-in function.
Any C function by default returns an int value. More specifically, whenever a call is made to a function, the compiler assumes that this function would return a value of the type int. If we desire that a function should return a value other than an int, then it is necessary to explicitly mention so in the calling function as well as in the called function. Suppose we want to find out square of a number using a function. This is how this simple program would look like:
Function declaration and Prototypes :
main( )
{
float a, b ;
printf ( "\nEnter any number " ) ;
scanf ( "%f", &a ) ;
b = square ( a ) ;
printf ( "\nSquare of %f is %f", a, b ) ;
}
square ( float x )
{
float y ;
y = x * x ;
return ( y ) ;
}
And here are two sample runs of this program...
Enter any number 3
Square of 3 is 9.000000
Enter any number 1.5
Square of 1.5 is 2.000000
The first of these answers is correct. But square of 1.5 is definitely not 2.This happened because any C function, by default, always returns an integer value. Therefore, even though the function square( ) calculates the square of 1.5 as 2.25, the problem crops up when this 2.25 is to be returned to main( ). square( ) is not capable of returning a float value. How do we overcome this?
The following program segment illustrates how to make square( ) capable of returning a float value.
main( )
{
float square ( float ) ;
float a, b ;
printf ( "\nEnter any number " ) ;
scanf ( "%f", &a ) ;
b = square ( a ) ;
printf ( "\nSquare of %f is %f", a, b ) ;
}
float square ( float x )
{
float y ;
y = x * x ;
return ( y ) ;
}
And here is the output...
Enter any number 1.5
Square of 1.5 is 2.250000
Enter any number 2.5
Square of 2.5 is 6.250000
Now the expected answers i.e. 2.25 and 6.25 are obtained. Note that the function square( ) must be declared in main( ) as float square ( float ) ;
This statement is often called the prototype declaration of the square( ) function. What it means is square( ) is a function that receives a float and returns a float.
- Function with no return type and no argument.
- Function with return type and no arguments.
- Function with return type and with argument.
- Function with no return type and with argument.
In this the function has not any arguments and it doesn't return any type, means its type is void
since void means empty.
For eg:
void main()- this is the in-built method having no return type and no argument.
Program in which we have user defined function:
#include <stdio.h>
#include <conio.h>
void main()
{
void message();
clrscr();
message();
getch();
}
void message()
{
printf("Functions");
}
Output:
Functions
Function with return type and no arguments
In this the function returns a type but does not contain any arguments.
Example:
#include <stdio.h>
#include <conio.h>
void main()
{
int value();
int a=value();
clrscr();
printf("value of a is : %d",a);
getch();
}
int value()
{
int x=10;
return x;
}
Output:
Value of a is : 10
This type of functions have return type along with the arguments.
Example:
#include <stdio.h>
#include <conio.h>
void main()
{
int sum(int,int); // function having return type and consist of two integer type arguments
int x=10;
int y=10;
int result=sum(x,y);
clrscr();
printf("Sum is : %d",result);
getch();
}
int sum(int a,int b)
{
int sum=0;
sum=a+b;
return sum;
}
Output :
Sum is : 20
Note :
int sum(int,int); that is the function declaration can be before the start of main function.
So following program is also correct :-
#include <stdio.h>
#include <conio.h>
int sum(int,int); //declaration is before main() function
void main()
{
int x=10;
int y=10;
int result=sum(x,y);
clrscr();
printf("Sum is : %d",result);
getch();
}
int sum(int a,int b)
{
int sum=0;
sum=a+b;
return sum;
}
Output :
Sum is : 20
Function with no return type and with argument
In this the function has arguments but doesn't have any return type.
Example :
#include <conio.h>
void main()
{
void sum(int,int); // function consist of two integer type arguments but not have return type
int x=10;
int y=10;
sum(x,y);
clrscr();
}
void sum(int a,int b)
{
int sum=0;
sum=a+b;
printf("Sum is : %d",sum);
getch();
}
Output :
Sum is : 20
Advantages of functions :
- It is easy to use.
- Debugging is more suitable for programs.
- It reduces the size of a program.
- It is easy to understand the actual logic of a program.
- Highly suited in case of large programs.
- By using functions in a program, it is possible to construct modular and structured programs.
Prev NEXT
1 comments:
This is a great explanation of functions in C! Understanding how to create modular, reusable blocks of code is essential for writing clean and maintainable programs. If you're working on larger projects or need to manage complex functionality, Ambition Host is a fantastic tool for simplifying the management of containerized applications and improving your workflow.
Post a Comment