Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lecture 5 Function

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

C Functions

Function is a group of statements that together perform a task. Every C program has at least one function, which
is main(), and all the most trivial programs can define additional functions.
You can divide up your code into separate functions. How you divide up your code among different functions is
up to you, but logically the division usually is so each function performs a specific task.
A function declaration tells the compiler about a function's name, return type, and parameters. A function
definition provides the actual body of the function.

Defining a Function
The general form of a function definition in C programming language is as follows:

A function definition in C programming language consists of a function header and a function body. Here are all
the parts of a function:
he value the function returns.
Some functions perform the desired operations without returning a value. In this case, the return_type is the
keyword void.
gether
constitute the function signature.

This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number
of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.

Example
Following is the source code for a function called max(). This function takes two parameters num1 and num2 and
returns the maximum between the two:
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:

For the above defined function max(), following is the function declaration:

Parameter names are not important in function declaration only their type is required, so following is also 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.

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, program control is transferred to the called function. A called function performs
defined task, and when its return statement is executed or when its function-ending closing brace is reached, it
returns program control back to the main program.
To call a function, you simply need to pass the required parameters along with function name, and if function
returns a value, then you can store returned value. For example:
I kept max() function along with main() function and compiled the source code. While running 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.
The 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 that arguments can be passed to a function:

Function 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 language uses call by value method to pass arguments. In general, this means that
code within a function cannot alter the arguments used to call the function. Consider the function swap() definition
as follows.

Now, let us call the function swap() by passing actual values as in the following example:

Let us put above code in a single C file, compile and execute it, it will produce the following result:

Which shows that there is no change in the values though they had been changed inside the function.

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. This means that
changes made to the parameter affect the passed argument.
To pass the 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 its arguments.

Let us call the function swap() by passing values by reference as in the following example:

Let us put above code in a single C file, compile and execute it, it will produce the following result:

Which shows that there is no change in the values though they had been changed inside the function.

You might also like