VI. Functions (1)
VI. Functions (1)
IN C PROGRAM
<p> COMSCI 1101 LECTURE </p>
TABLE OF CONTENTS.
01 02 03
Creating & Calling Passing Arguments
Overview of Functions
Functions to Functions
04 05 06
Call by Value & Call
Returning Values Types of Functions
by Reference
TABLE OF CONTENTS.
07 08
Scope of Function
Recursive Functions
Variables
OVERVIEW OF
FUNCTIONS
WHAT IS A FUNCTION?
▪ A function is a named, independent section of C
code that performs a specific task and optionally
returns a value to the calling statement.
WHAT IS A FUNCTION?
▪ A function is named. Each function has unique
identifier.
void print_message(){
printf(“Hello World!\n”);
}
void main(){
clrscr();
print_message();
getch();
}
Hello World!
_}
#include <stdio.h>
#include <conio.h>
int print_message(){
printf(“Hello World!\n”);
return 1;
}
void main(){
clrscr();
int a = 0;
a = print_message();
printf(“Returned value: %d\n”,a);
getch();
}
Hello World!
Returned value: 1
_}
PASSING ARGS
TO FUNCTIONS
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
getch();
}
The result = 3.000000
The result = 6.000000
The result = 12.000000
_}
PASSING ARGUMENTS
▪ As seen, the function double_it accepts two
arguments.
return expression;
RETURNING VALUES
▪ This statement indicates that the function is to
return the value of expression back to the calling
function.
void main(){
clrscr();
float answer;
answer = divide(3,2);
printf(“answer = %lf\n”,answer);
getch();
}
answer = 1.500000
_}
CALL BY VALUE &
CALL BY REFERENCE
CALL BY VALUE
▪ There are two ways to pass parameters to functions:
void main(){
clrscr();
getch();
}
double_it: the result = 12.000000
main: data = 1.500000
_}
CALL BY REFERENCE
▪ In a call by reference language, instead passing the
value of the argument into the function, a reference
to (i.e. the memory address of) the value is passed
into the function.
void main(){
clrscr();
getch();
}
double_it: the result = 12.000000
main: data = 12.000000
_}
TYPES OF
FUNCTIONS
DIFFERENT FUNCTIONS
▪ We could create different kinds of user-defined
functions base on our needs:
▪ Syntax:
void function_name(){
statement1;
statement2;
…
}
#include <stdio.h>
#include <conio.h>
void hello_world(){
printf(“Hello World!\n“);
}
void main(){
clrscr();
hello_world();
getch();
}
Hello World!
_}
NON-RETURNING FUNCTIONS
▪ Non-returning functions with parameters.
▪ Syntax:
void main(){
clrscr();
hello_world(1);
getch();
}
Hello World!
This is the value of param x: 1
_}
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
getch();
}
Hello World!
This is the value of param x: 1
This is the value of param y: 2.500000
This is the value of param z: A
_}
RETURNING FUNCTIONS
▪ Returning functions without parameters.
▪ Syntax:
data_type function_name(){
statement1;
statement2;
return expression;
}
#include <stdio.h>
#include <conio.h>
int getInteger(){
int value;
printf(“Enter an integer value: “);
scanf(“%d”,&value);
return value;
}
void main(){
clrscr();
int number;
number = getInteger();
printf(“The value of number is %d“,number);
getch();
}
Enter an integer value: 4
The value of number is 4
_}
RETURNING FUNCTIONS
▪ Returning functions with parameters.
▪ Syntax:
void main(){
clrscr();
double mathGrade = 90, sciGrade = 85, engGrade = 98, result;
getch();
}
Average grade is 91.00
_}
SCOPE OF FUNCTION
VARIABLES
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
int a = 1, b = 2, c = 3;
{
int a = 5, b = 6;
printf(“inner block a: %d\n”,a);
printf(“inner block b: %d\n”,b);
printf(“inner block c: %d\n”,c);
}
printf(“outer block a: %d\n”,a);
printf(“outer block b: %d\n”,b);
printf(“outer block b: %d\n”,c);
getch();
}
inner block a: 5
inner block b: 6
inner block c: 3
outer block a: 1
outer block b: 2
outer block c: 3
_}
FUNCTION VARIABLES
▪ An outer block variable name is valid in an inner
block unless block redefines it.
void customFunct(){
int a = 4;
printf(“customFunct a: %d\n”,a);
}
void main(){
clrscr();
int a = 2;
printf(“main a: %d\n”,a);
customFunct();
getch();
}
main a: 2
customFunct a: 4
_}
FUNCTION VARIABLES
▪ As we know variables which are declared within the
bounds of a function or block are referred as local
variables or internal variables.
int a = 3;
void customFunct(){
printf(“customFunct a: %d\n”,a);
}
void main(){
clrscr();
printf(“main a: %d\n”,a);
customFunct();
getch();
}
main a: 3
customFunct a: 3
_
RECURSIVE
FUNCTIONS
RECURSIVE FUNCTIONS
▪ It is possible to call a function inside the same
function, this process is called recursion or
recursive call of a function.
void getNumber(){
int num;
printf(“Enter a number: ”);
scanf(“%d”,&num);
if(num != 0)
getNumber();
}
void main(){
clrscr();
getNumber();
getch();
}
Enter a number: 2
Enter a number: 5
Enter a number: 1
Enter a number: 0
_
RECURSIVE FUNCTIONS
▪ By using recursion in returning functions, it is
necessary to use multiple return statements for
different outcomes.
void main(){
clrscr();
int num = 5;
printf(“Result: %d\n“,sumOf(num));
getch();
}
Result: 15
_
THANK
YOU!