R Functions
R Functions
It is independent
which can perform its
task without
A function is named
interference from or
interfering with other
parts of the program.
A function is defined
Every C program has using its predefined
at least one function, syntax, and are called
which is main() from elsewhere in
your program.
Why Functions?
Improves the
readability of
code
Debugging of
Avoid
code is easier,
duplicating
errors are easy
codes
to be traced.
Syntax:
return_type function_name( arg-typ name-1,...,arg-typ name-n);
Example:
int hex(int val);
int max(int num1, int num2);
function_na Block of
• Can be of me • Contains code
any data variables names
• Advised to have along with their • Set of C
type such as a meaningful statements,
data types
int, double, name • Kind of inputs which will be
char, void, • Easy to for the function executed
short etc. understand the argument whenever a
return_type purpose of call will be
list
function seeing made to the
it’s name function.
Calling a function
By creating a C To use a function,
function, we give a we will have to call
definition of what that function to
the function has to perform the defined
do. task.
When a program
calls a function, the
program control is
transferred to the
called function
which performs a
When its return defined task
statement is To call a function,
executed or when you simply need to
its closing brace is pass the required
reached, it returns parameters along
the program control with the function
back to the main name.
program.
If the function
returns a value,
Illustration of
working of
Functions in C
Example of functions
#include <stdio.h>
int main()
{ //main function definition
int a = 5, b = 10;
int sum;
printf("The value of a and b : %d %d ", a, b);
sum = add(a, b); //function call The value of a
printf("\nsum = %d ", sum); and b : 5 10
} sum = 15
//function definition
int add(int a, int b)
{
int c;
c = a + b;
return c; //returns a integer value to the calling function
}
Classification of user defined functions into four
types, according to parameters and return value
Functions wit Functions wit
hout hout
arguments bu arguments an
t with return d without
values return values
Functions wit Functions wit
h h
arguments bu arguments an
t without d with return
return values values
Functions Without Arguments Without Return Values
The calling function will not send parameters to the called function
and called function will not pass the return value to the calling function.
#include <stdio.h>
void add(); //function declaration
int main()
{ //main function definition
add(); //function call without passing arguments
}
//function definition
void add()
The value of a and b :
{ int a = 5, b = 10; 5 10
int c; sum = 15
printf(" The values of a and b : %d %d ", a, b);
c = a + b;
printf(" \nsum : %d ", c); //no return values to the calling function
}
Functions With Arguments Without Return Values
The calling function will pass parameters to the called function but
called function will not pass the return value to the calling function.
#include <stdio.h>
void add(int,int); //function declaration
int main()
{ //main function definition
int sum;
int a = 5, b = 10;
printf(" The values of a and b : %d %d ", a, b);
add(a, b); //function call with passing arguments to called function }
// function definition
void add(int a, int b) The value of a and b :
5 10
{
sum = 15
int c;
c = a + b;
printf(" \nsum : %d ", c); //no return values to the calling function
main
}
Functions Without Arguments With Return Values
The calling function will not pass parameters to the called function but
called function will pass the return value to the calling function.
#include <stdio.h>
int add(); //function declaration
int main()
{ //main function definition
int sum;
sum = add(); //function call without passing arguments to called
function
printf(" \nsum = %d ", sum);
}
// function definition The value of a and b :
int add() 5 10
sum = 15
{ int c;
int a = 5, b = 10;
printf(" The values of a and b : %d %d ", a, b);
c = a + b;
return c; //passing return values to the calling function main }
Functions With Arguments With Return Values
The calling function will pass parameters to the called function and
called function also pass the return value to the calling function
#include <stdio.h>
int add(int, int); //function declaration
int main()
{ //main function definition
int sum;
int a = 5, b = 10;
printf(" The values of a and b : %d %d ", a, b);
sum = add( a, b); //function call with passing arguments
printf(" \nsum = %d ", sum);
}
// function definition The value of a and b :
int add(int a, int b) 5 10
{ int c; sum = 15
c = a + b;
return c; //passing return values to the calling function main
}
Function Parameters
There are two types of function parameters:
Formal parameters:
Appear in a declaration or a definition of a function.
Actual parameters:
Appear in a call to the function.
Examples:
int f(int x); here x is a formal parameter
Related Function
The actual parameters are passed by The formal parameters are in the called
the calling function. function.
Data Types
4. Thus actual values of a and b remain 4. Thus actual values of a and b get
unchanged even after exchanging the changed after exchanging values of x
values of x and y. and y.
7. A copy of the original variables are 7. No copy of the variables are created
created in memory stack. on the memory stack.
2. #include<stdio.h>
int main()
{ function(); return 0; }
void function()
{ printf("Function in C is awesome"); }
A. Function in C is awesome B. no output C. Runtime error D. Compilation
error
3. #include<stdio.h>
int main()
{ main(); return 0; }
A. Runtime error B. Compilation error C. 0 D. none of the above
4. #include<stdio.h>
int function();
main()
{ int i;
i = function();
printf("%d", i);
return 0;
}
function()
{ int a;
a = 250;
return 0;
}
A. Runtime error B. 0 C. 250 D. No output
5. #include<stdio.h>
int function();
main()
{ int i;
i = function();
printf("%d", i);
return 0; }
function()
{ int a; a = 250; }
A. 250 B. 0 C. 1 D. Some Garbage value
6. #include<stdio.h>
int function(int, int);
int main()
{ int a = 25, b = 24 + 1, c;
printf("%d", function(a, b));
return 0; }
int function(int x, int y) { return (x - (x == y)); }
A. Compilation error B. 25 C. 1 D. 24
7. What will be the output of the C program by considering 'c' as a User input?
#include<stdio.h>
int main()
{ char c = ' ', x;
getc(c);
if((c >= 'a') && (c <= 'z'))
x = convert(c);
printf("%c", x); return 0; }
A. Runtime Error B. Any symbols or special characters C. Compilation Error D. B
8. #include<stdio.h>
int main()
{ int num = returns(sizeof(float));
printf("Value is %d", ++num); return 0; }
int returns(int returns)
{ returns += 5.01;
return(returns); }
A. 9 B. Compilation error C. 10 D. None of the above
Through this lecture, students will learn the
Learning basics of computer networks with its different
Outcomes topologies.