LO4_Functions
LO4_Functions
LO4_Functions
FUNCTIONS C Programming
Defining a Function
The general form of a function definition in C programming language is as
follows:
76
C Programming
Example
Given below is the source code for a function called max(). This function takes
two parameters num1 and num2 and returns the maximum value between the
two:
return result;
}
Function Declarations
A function declaration tells the compiler about a function name and how to call
the function. The actual body of the function can be defined separately.
A function declaration has the following parts:
Parameter names are not important in function declaration, only their type is
required, so the following is also a valid declaration:
Function declaration is required when you define a function in one source file
and you call that function in another file. In such case, you should declare the
function at the top of the file calling the function.
77
C Programming
Calling a Function
While creating a C function, you give a definition of what the function has to do.
To use a function, you will have to call that function to perform the defined task.
When a program calls a function, the program control is transferred to the called
function. A called function performs a defined task and when its return
statement is executed or when its function-ending closing brace is reached, it
returns the program control back to the main program.
To call a function, you simply need to pass the required parameters along with
the function name, and if the function returns a value, then you can store the
returned value. For example:
#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
int ret;
return 0;
}
78
C Programming
return result;
}
We have kept max()along with main() and compiled the source code. While
running the final executable, it would produce the following result:
Function Arguments
If a function is to use arguments, it must declare variables that accept the
values of the arguments. These variables are called the formal parameters of
the function.
Formal parameters behave like other local variables inside the function and are
created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways in which arguments can be passed
to a function:
79
C Programming
Call by Value
The call by value method of passing arguments to a function copies the actual
value of an argument into the formal parameter of the function. In this case,
changes made to the parameter inside the function have no effect on the
argument.
By default, C programming uses call by value to pass arguments. In general, it
means the code within a function cannot alter the arguments used to call the
function. Consider the function swap() definition as follows.
return;
}
Now, let us call the function swap() by passing actual values as in the following
example:
#include <stdio.h>
/* function declaration */
void swap(int x, int y);
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
80
C Programming
return 0;
}
Let us put the above code in a single C file, compile and execute it, it will
produce the following result:
It shows that there are no changes in the values, though they had been changed
inside the function.
Call by Reference
The call by reference method of passing arguments to a function copies the
address of an argument into the formal parameter. Inside the function, the
address is used to access the actual argument used in the call. It means the
changes made to the parameter affect the passed argument.
To pass a value by reference, argument pointers are passed to the functions just
like any other value. So accordingly, you need to declare the function
parameters as pointer types as in the following function swap(), which
exchanges the values of the two integer variables pointed to, by their
arguments.
81
C Programming
return;
}
Let us now call the function swap() by passing values by reference as in the
following example:
#include <stdio.h>
/* function declaration */
void swap(int *x, int *y);
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
return 0;
}
Let us put the above code in a single C file, compile and execute it, to produce
the following result:
82
C Programming
It shows that the change has reflected outside the function as well, unlike call
by value where the changes do not reflect outside the function.
By default, C uses call by value to pass arguments. In general, it means the
code within a function cannot alter the arguments used to call the function.
83