Functions
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 is such that 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.
Types of functions
• Predefined standard library functions – such as puts(), gets(), printf(),
scanf() etc – these are the functions which already have a definition in
header files (.h files like stdio.h), so we just call them whenever there is a
need to use them.
• User Defined functions – the functions which we can create by ourselves,
for example we can create function abc() and call it in main() in order to use
it.
Defining a Function
The general form of a function definition in C programming language is as
follows:
return_type function_name( parameter list )
{
body of the function
}
Examples
Given below is the source code for a function called max(). This function takes
two parameters a and b and returns the maximum value between them.
int max(int a, int b)
{
if (a > b) return a;
return b;
}
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>
printf("a = %d\n",a);
printf("b = %d\n",b);
printf("max(a,b) = %d\n",res);
return 0;
}
Example
Function f(n) returns the sum of digits for two digital number n.
#include <stdio.h>
int f(int n)
{
int a = n / 10; // number of tens
int b = n % 10; // number of units
return a + b;
}
int main(void)
{
printf("%d\n",f(34));
return 0;
}
E-OLYMP 108. Median number Three integers a, b, c are given. Find the
median number.
► Median number equals to a + b + c – min(a, b, c) – max(a, b, c).
Minimum of three numbers can be found like min(a, min(b, c)) or we can
implement a function minimum of three:
int min(int x, int y, int z)
{
return min(x, min(y, z));
}
E-OLYMP 920. Use the function Three real numbers x, y, z are given. Find the
value of min(max(x,y), max(y,z), x + y + z).
► Use min() and max() functions.
E-OLYMP 906. Product of digits Write a function f(n) that returns the product of
digits for three digital number n.
► Let a = n / 100 is a digit of hundrets, b = n / 10 % 10 is a digit of tens, c = n %
10 is a digit of units. Then f(n) = a * b * c.
E-OLYMP 939. The square of sum Write a function f(n) that returns the square
of sum of digits for two digital positive integer n.
► Let a is a digit of tens, b is a digit of units. Then f(n) = (a + b)2.
double x;
double f(double x)
{
return x * x * x + 2 * x * x - 3;
}
int main(void)
{
while(scanf("%lf",&x) == 1)
printf("%.4lf\n",f(x));
return 0;
}
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 Call by value and Call by reference.
Call by value
This method 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. Different memory is allocated for both actual and formal
parameters.
• Actual parameter is the argument which is used in function call.
• Formal parameter is the argument which is used in function definition
#include <stdio.h>
formal parameters
int max(int a, int b)
{
return (a > b) ? a : b;
}
int main(void)
{
int a = 111, b = 222;
int res = max(a,b);
. . . actual parameters
}
Function f has two formal parameters a and b. Inside the body of the function f we
work with formal parameters, changing them.
#include <stdio.h>
local variables
void f(int a, int b)
{ f a = 10 a++; a = 11
a++; b = b + 2; function b=b+2;
printf("%d %d\n",a,b); // 11 22 b = 20 b = 22
}
copy stack stack
int main(void)
{
int a = 10, b = 20; main a = 10 a = 10
f(a,b); function
printf("%d %d\n",a,b); // 10 20 b = 20 b = 20
return 0;
} local variables
Call by reference
This method 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 argument.
#include <stdio.h> reference
contains the address
void f(int &a, int &b)
{ f &a a++; &a
a++; b = b + 2; function b=b+2;
printf("%d %d\n",a,b); // 11 22 &b &b
}
stack stack
int main(void)
{ a = 10 a = 11
int a = 10, b = 20; main
f(a,b); function
printf("%d %d\n",a,b); // 11 22 b = 20 b = 22
return 0;
} local variables
Let’s implement the function swap that swaps the values.
#include <stdio.h>
int main(void)
{
int a = 10, b = 20;
printf("%d %d\n",a,b); // 10 20
swap(a,b);
printf("%d %d\n",a,b); // 20 10
return 0;
}
We want to create a function f that returns two values: the sum and the product of
two numbers. Let’s pass two parameters a and b by value, and parameters sum and prod
by reference.
#include <stdio.h>
int main(void)
{
int a = 3, b = 5, sum, prod;
f(a,b,sum,prod);
printf("%d %d\n",sum,prod); // 8 15
return 0;
}
int y f
#include <stdio.h> function
int x
int a, b; // a, b global
int d int d
int f(int x, int y) // x, y local
int b int y main
{ function
int z = x * y; // z local int a int x
return z;
} DATA STACK
segment segment
int d; // d global
int main()
{
int x = 3, y = 9; // x, y local
int d = f(x, y); // d local
printf("%d\n", d);
}
How to access global variable in the function if we have a local variable with the
same name?
x – local variable
::x – global variable
:: - scope resolution operator. Used to access global variable when there is a local
variable with the same name
#include <stdio.h>
int x; // x global
int main()
{
f(5, 6);
printf("%d\n", x);
}