C lecture 4.1
C lecture 4.1
return_type: Return type can be of any data type such as int, double, char, void, short etc.
function_name: It can be anything, however it is advised to have a meaningful name for the functions so that it would be
easy to understand the purpose of function just by seeing it’s name.
argument list: Argument list contains variables names along with their data types. These arguments are kind of inputs for the
function. For example – A function which is used to add two integer variables, will be having two integer argument.
Block of code: Set of C statements, which will be executed whenever a call will be made to the function.
Function Declaration: declaration is giving a prototype like simply a name .
return_type function_name();
ex- int sum();
In the declaration we just provide function name,return not write body.
Function Definition: definition is associating the task or the meaning with the prototype. Here we write the body of function
what the work.
ex-
int sum(){
c=a+b;
return c;
}
Function Call:A function call is an expression that passes control and arguments (if any) to a function and has the form:
expression (expression-listopt)
If the function does not return a value, returns void.
Prototype Declaration: A function prototype is simply the declaration of a function that specifies function's name,
parameters and return type. It doesn't contain function body. A function prototype gives information to the compiler that
the function may later be used in the program.
The function prototypes are used to tell the compiler about the number of arguments and about the required datatypes of
a function parameter, it also tells about the return type of the function. By this information, the compiler cross-checks
the function signatures before calling it.
Example:
#include<stdio.h>
void function(int); //prototype
main()
{
function(50);
}
void function(int x)
{
printf("The value of x is: %d", x);
}
Passing Parameters through Functions:
There 4 types to pass parameters through a function. These are:
A function can take parameters which are just values you supply to the function so that the function can do something utilizing
those values. These parameters are just like variables except that the values of these variables are defined when we call the
function and are not assigned values within the function itself.
Parameters are specified within the pair of parentheses in the function definition, separated by commas. When we call the
function, we supply the values in the same way. Note the terminology used - the names given in the function definition are
called parameters whereas the values you supply in the function call are called arguments.
No argument-no return type :
#include<stdio.h>
void add(void);
int main()
{
add();
return 0;
}
void add(void)
{
float x;
int y;
printf(" enter two number:\n ");
scanf("%f%d",&x,&y);
printf("\n division=%f",x+y);
}
Argument but no return type :
#include<stdio.h>
void mul(float,int);
int main()
{
float x;
int y;
printf("enter two number:\n");
scanf("%f%d",&x,&y);
mul(x,y);
return 0;
}
void mul(float a,int b)
{
printf("\n multiplication is=%f",a*b);
}
No argument but return type :
#include<stdio.h>
float div(void);
int main()
{
float r;
r=div();
printf("\n division=%f",r);
return 0;
}
float div(void)
{
float x;
int y;
printf("\n enter two numbers:");
scanf("%f%d",&x,&y);
return(x/y);
}
Argument and return type :
#include<stdio.h>
float sub(float,int);
int main()
{
float r,x;
int y;
printf("\n enter two numbers:");
scanf("%f%d",&x,&y);
r=sub(x,y);
printf("\n subtraction=%f",r);
return 0;
}
float sub(float a,int b)
{
return(a-b);
}
Function arguments in c programming :
Here, as shown in the figure above arguments value is used to send values to the called program.
Basically, there are two types of arguments:
• Actual arguments: the values that are passed to the called function from the main function are known as Actual
arguments.
• Formal arguments : The variables declared in the function prototype or definition are known as Formal arguments
The actual arguments and formal arguments must match in number, type, and order.
int main()
{
int x, y, z;
x = 5;
y = 5;
z = add(x,y); // call by value
printf("\n result=%d",z);
return 0;
}
In this program, function add() is called by passing the arguments x and y. The copy of the values of x and y are passed to a and b respectively and then are
used in the function. So by changing the values of a and b, there will be no change in the actual arguments x and y in the function call.
Pass by reference :
#include <stdio.h>
void swap (int *a, int *b) // a and b are reference variables
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x = 2, y = 4;
printf("before swapping x = %d and y = %d\n", x, y);
swap(&x, &y); // call by reference
printf("after swapping x = %d and y = %d\n", x, y);
return 0;
}
In the above program, the formal arguments a and b becomes the alias of actual arguments x and y when the function was called. So when the
variables a and b are interchanged x and y are also interchanged.