0% found this document useful (0 votes)
17 views44 pages

Comprog11 Semifinals Compilation

The document provides an overview of functions in programming, including user-defined and library functions, their definitions, categories, and elements such as declaration, call, and definition. It explains the importance of user-defined functions for modular programming and outlines different types of functions based on their arguments and return values. Additionally, it discusses call by value and call by reference, illustrating how parameters can be passed to functions and how modifications affect the original variables.

Uploaded by

Jinjen b.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views44 pages

Comprog11 Semifinals Compilation

The document provides an overview of functions in programming, including user-defined and library functions, their definitions, categories, and elements such as declaration, call, and definition. It explains the importance of user-defined functions for modular programming and outlines different types of functions based on their arguments and return values. Additionally, it discusses call by value and call by reference, illustrating how parameters can be passed to functions and how modifications affect the original variables.

Uploaded by

Jinjen b.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Functions

User – defined functions


Definition
 A set of statements working together with common goal
is known as function.
 Also known as subprograms which are used to compute
a value or perform a specific task.
 They can’t run independently and are always called by
the main() program or by some other function.
Categories of Functions
 C functions can be classified into two
categories
– Library functions
– User-defined functions
Library Functions
 These are also known as Pre defined functions.
 Library functions are not required to be written by us
 printf() and scanf() belong to the input/output
library functions => <stdio.h>
 sqrt(), pow(),cos()etc are some of mathematical
library functions => <math.h>
 strlen(), strcmp(),strcat()etc are some of
string manipulation library functions => <string.h>
 Example:
scanf(), printf(), strlen(), strcmp(), strcat(),
sqrt(), pow()
are this are library functions.
User Defined Functions
 User defined functions are self-contained blocks
of statements which are written by the user to
compute or perform a task.
 They can be called by the main program
repeatedly as per the requirement.
 main() is an example of user-defined functions
Need for User Defined Functions

 Every program must have a main function


 It is possible to code any program utilizing only
main function, but it can lead to a number of
problems
 If a program is divided into functional parts, then
each part may be independently coded and later
combined into a single unit
 These subprograms called ‘functions’ are much
easier to understand, debug, and test
Elements of User Defined Functions
 In order to make use of user-defined functions,
we need to establish three elements that are
related to functions.

 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:

function_type function_name(arguments list);

Example:

int add(int a, int b);


Function Declaration
 A function declaration consists of four parts
– Function type (return type)
– Function name
– Parameter list
– Terminating semicolon
 Format
– Function-type function-name (parameter list);
 Very similar to the function header line expect the
terminating semicolon.
– int mul(int m, int n); /* Function declaration */
 Equally acceptable forms of declarations are
– int mul (int, int);
– mul (int a, int b);
– mul (int, int);
Function Declaration
 A prototype declaration may be placed in two places in a
program.
– Above all the functions (including main) (global prototype)
– Inside a function definition (local prototype)
 It is good to declare prototypes in the global declaration
section before main.
 It adds flexibility, provides an excellent quick reference
to the functions used in the program, and enhances
documentation
Function Call
 The program that calls the function is referred to
as the calling program or calling functions
Syntax:

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

Functions with no return values


Category of Functions
 Category 1: Functions with no arguments
and no return values
 Category 2: Functions with arguments and
no return values
 Category 3: Functions with arguments and one
return value
 Category 4: Functions with no arguments but
return a value
 Category 5: Functions that return multiple
values
No Arguments and No Return Values
 When a function has no arguments, it does not
receive any data from the calling function
 When it does not return a value, the calling
function does not receive any data from the
called function
 There is no data transfer between the calling
function and the called function
 There is only a transfer of control but not data
No Arguments and No Return Values
Example
#include <stdio.h>

void add(void); OUTPUT:


Enter two number:3
int main()
{ 4
add();
return 0; Sum is:7
}

void add() // called


function
{
int a, b, c;
printf("\nEnter two numbers:
");
scanf("%d %d", &a, &b);
c = a + b;
printf("\nSum is: %d\n", c);
}
Arguments but No Return Values
 The calling function read data from the terminal
and pass it on to the called function.
 It is a one way data communication, i.e. the
called program receives data from calling
program but it does not return any value to the
calling program.
 This approach is wiser because the calling
function can check for the validity of data, if
necessary, before it is handed over to the called
function.
 Called function with arguments
value(p,r,n)
Arguments but No Return Values
Example
#include <stdio.h> int add(int x, int y)
{
int add(int x, int y); return x + y;
}
int main()
{
int a, b; OUTPUT:
printf("\nEnter two Enter two number:2
numbers: "); 4
scanf("%d %d", &a, &b);
printf("\nSum is: %d\n", Sum is:6
add(a, b));
return 0;
}
Category of Functions
 Category 1: Functions with no arguments and
no return values
 Category 2: Functions with arguments and no
return values
 Category 3: Functions with arguments and
one return values
 Category 4: Functions with no arguments
but return a value
 Category 5: Functions that return multiple
values
Arguments With Return Values
Example
#include <stdio.h> int add(int x, int y)
{
int add(int x, int y); int z;
z = x + y;
int main() return z;
{ }
int a, b, sum;
printf("\nEnter two
numbers: "); OUTPUT:
scanf("%d %d", &a, &b); Enter two number:6
7
sum = add(a, b);
printf("\nResult is: %d\ Sum is:13
n", sum);

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

Reference Variable as Parameters


Call by Value
 Calling a function with parameters passed as values

int a=10; void fun(int a)


fun(a); {
defn;
}
Here fun(a) is a call by value.
Any modification done with in the function is local to it and
will not be effected outside the function
Call by Reference
 Calling a function by passing pointers as parameters
(address of variables is passed instead of variables)

int a=1; void fun(int *x)


fun(&a); {
defn;
}
Any modification done to variable a will effect outside the
function also
Call by Reference
Example program – Call by value
#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 = 10
return 0;
}

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;
}

void fun(int *x) {


printf("%d\n", *x); // Output: x = 10
(*x)++;
printf("%d\n", *x); // Output: x = 11
}
Explanation

a and x are referring to same location. So value will


be over written.
Conclusion
 Call by value => copying value of variable in another
variable. So any change made in the copy will not affect
the original location.
 Call by reference => Creating link for the parameter to
the original location. Since the address is same,
changes to the parameter will refer to original location
and the value will be over written.

You might also like