FUNCTION
C PROG
C Functions A function is a block of code that performs a specific task.
we can divide a large program into the basic building blocks
known as function.
The function contains the set of programming statements
enclosed by {}.
A function can be called multiple times to provide reusability
and modularity to the C program
Types of function
There are two types of function in C programming:
1- Standard library functions
2-User-defined functions
Standard library functions
The standard library functions are built-in functions in C programming.
These functions are defined in header files. For example,
•The printf() is a standard library function to send formatted output to the
screen (display output on the screen). This function is defined in
the stdio.h header file.
Hence, to use the printf()function, we need to include the stdio.h header
file using #include <stdio.h>.
•The sqrt() function calculates the square root of a number. The function is
defined in the math.h header file.
For example, main() is a function, which is used to execute code,
and printf() is a function; used to output/print text to the screen :
Example
int main() {
printf("Hello World!");
return 0;
}
User-defined function
You can also create functions as
per your need. Such functions created by the user are known as user-defined
functions.
Create a Function
To create (often referred to as declare) your own function, specify the name of the
function, followed by parentheses () and curly brackets {}:
Syntax
void myFunction()
{
// code to be executed
}
Function Aspects
There are three aspects of a C function.
•Function declaration A function must be declared globally in a c program to tell the
compiler about the function name, function parameters, and return type.
•
Function call Function can be called from anywhere in the program. The parameter list
must not differ in function calling and function declaration. We must pass the same
number of functions as it is declared in the function declaration.
•
Function definition It contains the actual statements which are to be executed. It is the
most important aspect to which the control comes when the function is called. Here, we
must notice that only one value can be returned from the function.
SN C function aspects Syntax
1 Function declaration return_type function_name (argument list);
2 Function call function_name (argument_list)
3 Function definition return_type function_name (argument list) {function
body;}
Return Value A C function may or may not return a value from the function. If you don't have to
return any value from the function, use void for the return type.
Let's see a simple example of C function that doesn't return any value from the function.
Example without return value:
void hello()
{
printf("hello c");
}
If you want to return any value from the function, you need to
use any data type such as int, long, char, etc.
The return type depends on the value to be returned from the
function.
Let's see a simple example of C function that returns int value
from the function.
Example with return value: int get()
{
return 10;
}
#include <stdio.h>
int main()
{
void myFuction(); // Function declaration
myFunction(); // call the function
return 0;
}
// Function definition
void myFunction()
{
printf("I just got executed!");
}
// Create a function
void myFunction()
{
printf("I just got executed!");
}
int main()
{
myFunction(); // call the function
return 0;
}
A function can be called multiple times:
void myFunction()
{
Example printf("I just got executed!");
}
int main()
{
myFunction();
myFunction();
myFunction();
return 0;
}
I just got executed!
I just got executed!
I just got executed!
Different aspects of function calling
A function may or may not accept any argument. It may or may
not return any value. Based on these facts, There are four different
aspects of function calls.
•function without arguments and without return value
•function without arguments and with return value
•function with arguments and without return value
•function with arguments and with return value
Example for Function without argument and return value
#include<stdio.h>
void main ()
{
void printName();
printf("Hello ");
}
void printName()
{
printf(“Ajay");
}
Example for Function without argument and with return value
#include<stdio.h>
void main()
{
int sum();
int result;
printf("\nGoing to calculate the sum of two numbers:");
result = sum();
printf("%d",result);
}
int sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}
Example for Function with argument and without return value
#include<stdio.h>
void sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b);
}
void sum(int a, int b)
{
printf("\nThe sum is %d",a+b);
}
Example for Function with argument and with return value
#include<stdio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\nThe sum is : %d",result);
}
int sum(int a, int b)
{
return a+b;
}