0% found this document useful (0 votes)
2 views

C Functions program

The document explains how C functions can be invoked, detailing the passing of arguments and the return of values. It covers function headers, bodies, and the differences between call by value and call by reference, providing examples for clarity. Additionally, it includes a simple program demonstrating function invocation in C.

Uploaded by

akinbodejohn2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C Functions program

The document explains how C functions can be invoked, detailing the passing of arguments and the return of values. It covers function headers, bodies, and the differences between call by value and call by reference, providing examples for clarity. Additionally, it includes a simple program demonstrating function invocation in C.

Uploaded by

akinbodejohn2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C FUNCTIONS

Any C Function can be invoked or called by another. The invoking function typically passes
information to the invoked function. The invoked function may return information to its
invoker. All combinations are possible:

• The invoker passes information, and the invoked returns information


• The invoker passes information, but the invoked returns none
• The invoker passes no information, but the invoked returns information
• The invoker passes no information, and the invoked returns

An invoking function passes information by passing arguments. In C any expression can


be an argument. In the invoking function, arguments are evaluated and the values passed to
the invoked function. An invoked function has parameters that catch the information passed
to it. A function may return at most one value.

Every function has a header and a body.

A function’s header consists of

- The function’s name, which is any valid C identifier


- A list of zero or more parameters

The parameters (sometimes called formal parameters) are placeholders for the arguments
that the function expects. The parameters are enclosed in parenthesis and separated by
commas.

Example 1
/* start of header */
float abs_diff(num1, num2)
float num1, num2;
/* end of header */

/* start of body */
{
If (num1 > num2)
return (num1 – num2);
else
return (num2 – num1);
}
/* end of body */

FUNCTION CALLS
When a function is invoked, its statements begin executing until either a return statement
or the last statement in its body is executed, after which the statement in the invoking
function resume execution in their regular sequence.
The return statement
When a return statement in function is executed, the function returns to its invoker. The
return statement is optional in a function that does not return a value but if used, it is written
Return;

A function’s parameters should match the function’s arguments in number and data type.
For example if a function is invoked with two arguments, it should have two parameters. If
the arguments are of type int and char respectively, then the first parameter should be of type
int and the second of type char.

FUNCTION CALL BY VALUE

Examples

Void main ()
{
int num1 = 5;
int num2 = 2;
int ans1, ans2;
int square (); /* declaring a function */

ans1 = square (num1);


ans2 = square (num1 + num2);
}
int square (numb)
int numb;
{
numb = numb * numb;
return (numb);
}

We show two calls to square to illustrate both a simple variable and a complex expression
as an argument.
Call by value lets us pass arguments to a function but with a guarantee that the original
arguments will remain unchanged no matter what the invoked function does to the copies.
The difference between
ans1 = square (num1);
ans2 = square (num1 + num2);

is that in ans1, the argument to square is a simple one while in ans2 the argument to square
is a complex expression which has to be evaluated before being passed. Also note that in the
function square, the parameter numb receives the value of the argument (expression) being
passed to the function from the main function.
So when
ans2 = square (num1 + num2);
is executed, the value of the expression: num1 + num2 is computed which is 7, then a
temporary cell numb is created and the value of the argument is copied to numb.
Note that num1’s contents have not changed from the first call to square and that this call
also does not alter the contents of either num1 or num2.
Therefore the value of ans1 is 25 and that of ans2 is 49.
The benefit of call by value is that it protects the values of the arguments in the invoking
function from being changed by the invoked function.

FUNCTION CALL BY REFERENCE


In some programming languages, functions work directly on the arguments and not on
copies so that it is easy to change the values of the arguments. and in the case above we will
have the function replace num1’s contents by its square.

Example

/* Call by Reference */
Main ( )
{
int num1 = 25;
int num2;
int *ptr = &num1;
int add1( );

num2 = add1 (ptr);


}
int add1 (p)
int *p;
{
Return (++ *p)
}

After this, the function add1( ) returns 26 to main, which is assigned to num2. After
executing the statement:
num2 = add1 (ptr);
in main, both num1 and num2 hold 26. So with respect to the variable num1, the call to
add1 is call by reference.

function program

#include <stdio.h>

void my_function();

main()
{
printf("Main function.\n");

my_function();

printf("Back in function main.\n");

return 0;
}
void my_function()
{
printf("Welcome to my function. Feel at home.\n");
}

You might also like