Function Definition
C functions are simple, but because of how C works, the power of functions is a bit limited.
- Functions receive either a fixed or variable amount of arguments.
- Functions can only return one value, or return no value.
In C, arguments are copied by value to functions, which means that we cannot change the arguments to affect their value outside of the function. To do that, we must use pointers, which are taught later on.
Functions are defined using the following syntax:
Following are some important points about functions in C.
1) Every C program has a function called main() that is called by operating system when a user runs the program.
1) Every C program has a function called main() that is called by operating system when a user runs the program.
2) Every function has a return type. If a function doesn’t return any value, then void is used as return type.Moreover if the return type of the function is void ,we still can use return statement in the body of function definition by not specifying any constant,variable,etc. with it ,by only mentioning the ‘return;’ statement which would symbolise the termination of the function as shown below:
3) In C, functions can return any type except arrays and functions. We can get around this limitation by returning pointer to array or pointer to function.
4) Empty parameter list in C mean that the parameter list is not specified and function can be called with any parameters. In C, it is not a good idea to declare a function like fun(). To declare a function that can only be called without any parameter, we should use “void fun(void)”.
As a side note, in C++, empty list means function can only be called without any parameter. In C++, both void fun() and void fun(void) are same.
5)If in a C program, a function is called before its declaration then the C compiler automatically assumes the declaration of that function in the following way:
int function name();
And in that case if the return type of that function is different than INT ,compiler would show an error.
4) Empty parameter list in C mean that the parameter list is not specified and function can be called with any parameters. In C, it is not a good idea to declare a function like fun(). To declare a function that can only be called without any parameter, we should use “void fun(void)”.
As a side note, in C++, empty list means function can only be called without any parameter. In C++, both void fun() and void fun(void) are same.
5)If in a C program, a function is called before its declaration then the C compiler automatically assumes the declaration of that function in the following way:
int function name();
And in that case if the return type of that function is different than INT ,compiler would show an error.
Function Construction
return-value-type function-name( parameter-list ){
Statements;
}
return-value-type: data type of the value returned
If not filled, then default data type will be used (default integer)
If return-value-type is void then the function will not return value
Parameter-list: list of value sent from the function initiator (user)
Function Prototype
Function in C usually written above the
initiator/caller or main program. Otherwise should use Function
Prototype
Function Prototype Objective:
•To ensure a function is known by
the initiator/caller
•Compiler
will validate the parameters
Syntax :
return-value-type function-name ( parameter-list );
Example:
#include
<stdio.h>
int maximum (int x, int y){
int max = x;
if ( y > max) max = y;
return max
}
void main () {
int a,b;
printf("Input 2 even values : ");
scanf("%d %d", &a, &b);
printf("Largest value : %d\n",maximum(a,b));
}
can be written as
follows:
int
maximum (int a, int b);
Important:
parameters
data type, number of parameters and its order
Identifier Scooping
Identifier Scoping:
scope
of identifier is reachable
Identifier Scoping:
•Local
•Global
Local Identifier
•Identifier declared in a function
including the parameters
•Scope limited in the function
Global Identifier
•Identifier declared outside any
function and placed on top of all functions in a C program
•Reachable from any point in the
program
•Global Identifier, can be
re-declared in subprogram
•It is advisable not to use global
variable for the following reasons:
–Error rate might increase as line
of code increase.
–Difficult in debugging
–Exclusivity of data is low. All
functions in the program can change its value
Passing Parameters
If a module is not self sufficed then
needed data/value and its result passes in and out using parameter(s)
List of parameters is the interface of a
module with other modules
Passing Parameter
–By-Value, sent
to other module is the value
–By
Location/by reference, sent to other module is the address
Example: (Passing Parameter by Value)
#include <stdio.h>
void Line (char
x
) { /* x is Formal Parameter*/
{
int i; / *i, x are Local Variable */
for (i = 1; i<=10; i++) printf(“%c”,x);
}
/*Main Program*/
void main()
{
char A =
’-’;
Line(A);
/* A is Actual Parameter */
}
Example: (Passing Parameter by Location)
#include <stdio.h>
void Calculate (int X, int Y, int
*P, int *Q)
{
*P = X
+ Y;
*Q = X
* Y;
}
void main()
{
int
X, Y, P, Q; /*local variable*/
printf(“
X=”); scanf(“%d”,&X);
printf(“
Y=”); scanf(“%d”,&Y);
Calculate(X,Y,&P,&Q);
printf(”X
+ Y = %d\n”, P);
printf(”X
* Y = %d\n”, Q);
}
No comments:
Post a Comment