Comprog11 Semifinals Compilation
Comprog11 Semifinals Compilation
Function Declaration
Function Call
Function Definition
Function Declaration
The calling program should declare any function
that is to be used later in the program. This is
known as the function declaration or function
prototype.
Syntax:
Example:
function_name(actual parameters);
Example:
add(a,b);
Calling a Function
A function can be called by simply using the
function name in a statement
When the function encounters a function call,
the control is transferred to the function mul(x,y)
main()
{
int p;
p = mul(10,5);
printf("%d\n", p);
}
int mul(int x,int y)
{
int p; /*local variables*/
p = x * y; /* x = 10, y = 5*/
return(p);
}
Calling a Function
The function is executed and the value is
returned and assigned to p
A function which returns a value can be used in
expressions like any other variables
printf("%d\n", mul(p,q));
y = mul(p,q) / (p+q);
if (mul(m,n)>total)
printf("large");
A function cannot be used on the right side of an
assignment statement
mul(a,b) = 15;
A function that does not return any value may
not be used in expression; but can be called to
perform certain tasks specified in the function
main() { printline(); }
Function Definition
The function definition is an independent program
module that is specially written or implement the
requirements of the function.
Syntax:
return_type function_name(formal_parameters) {
// Function body
// Statements to implement the function
// Return statement (if needed)
}
Example:
int add(int a, int b){
return a + b;
}
Function
Definition of Functions
• A function definition, also known as function
implementation shall include the following elements;
– Function name;
– Function type;
– List of parameters;
– Local variable declaration;
– Function statements; and
– A return statement.
Formal Parameter List
The parameter list declares the variables that will receive
the data sent by the calling program.
They serve as input data to the function to carry out the
specified task.
They represent actual input values, they are often referred
to as formal parameters.
These parameters can also be used to send values to the
calling programs
The parameter is known as arguments.
– float quadratic (int a, int b, int c) { ….. }
– double power (double x, int n) { ….. }
– int sum (int a, int b) { ….. }
There is no semicolon after the closing parenthesis
The declaration parameter variables cannot be combined
Return Values and Their Types
If a function does not return any value, we can
omit the return statement.
Its return type should be specified as void
A function may or may not send back any value
to the calling function
Done through return statement
It is possible to send any number of values to
the called function
The called function can only return one value
per call
SYNTAX:
return;
or
return (expression);
Return Values and Their Types
return;
– Plain return does not return any value
– Acts as the closing brace of the function
– The control is immediately passed back to the calling function
– EXAMPLE:
if(error)
return;
return (expression);
– Return the value of the expression
EXAMPLE:
mul(x,y)
int x,y;
{
int p;
p = x*y;
return(p);
}
– Returns the value of p which is the product of the values of x and y
– The last statement can be combined into one statement as
Return Values and Their Types
A function may have more than one return
statements
This situation arises when the value returned is
based on certain conditions
EXAMPLE:
if(x <= 0)
return(0);
else
return(1);
Return type of data:
– All function by default return int type data
– We can force a function to return a particular type of data
by using type specifier in the function header
– EXAMPLE:
double product(x,y)
float sqr_root(p)
– When a value is returned, it is automatically cast to the
function’s type
Functions
return 0;
}
Returning Non-integer Value
float mul(float a, float b);
The type-specifier tells double div(float a, float b);
the compiler, the type of int main() {
data the function is to float a, b;
a = 12.345;
return b = 9.82;
The called function must printf("%f\n", mul(a, b));
be declared at the start of printf("%lf\n", div(a, b));
return 0;
the body in the calling
}
function, like any other
variable. This is to tell the float mul(float x, float y){
calling function the type return x * y;
}
of data that the function
is actually returning double div(float p, float q){
return p / q;
}
No Arguments but Returns a Value
int get_number(void);
There could be
occasions where we int main()
may need a design {
functions that may int m = get_number();
printf("%d\n", m);
not take any
return 0;
arguments but }
returns a value to the
calling function. int get_number(void)
{
int number;
scanf("%d", &number);
return number;
}
Functions Returning Nothing
Functions that do not We can also declare them in
return any values are not the main with the qualifier void
declared in the main This states explicitly that the
main() functions do not return values
{ main()
printline(); {
value(); void printline();
printline(); void value();
} void printline();
printline() }
{ void printline()
.... {
} ....
value() }
{ void value()
.... {
} ....
}
Functions
void fun(int x) {
printf("%d\n", x); // Output: x = 10
x++;
printf("%d\n", x); // Output: x = 11
}
Explanation
Example Program – Call by Reference
#include <stdio.h>
void fun(int *x);
int main() {
int a = 10;
printf("%d\n", a); // Output: a = 10
fun(&a);
printf("%d\n", a); // Output: a = 11
return 0;
}