[go: up one dir, main page]

0% found this document useful (0 votes)
13 views74 pages

VI. Functions (1)

Uploaded by

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

VI. Functions (1)

Uploaded by

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

FUNCTIONS

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.

▪ By using that identifier in another part of the


program, you can execute the statements contained in
the function. This is known as calling the function.

▪ A function can be called from within another


function.
WHAT IS A FUNCTION?
▪ A function is independent. A function can perform
its task without interference from or interfering
with other parts of the program.

▪ A function performs a specific task. A task is a


discrete job that your program must perform as part
of its overall operation, like sending a line of
text to a printer.
WHAT IS A FUNCTION?
▪ A function can return a value to the calling
statement. When your statement calls a function, the
statements of a function contains are executed. If
you want them to, these statements can pass
information back to the calling program.
FUNCTIONS
▪ There are two types of function in C Programming

▪ Standard Library functions (built-in)

▪ User-defined functions (custom)


STANDARD LIBRARY
▪ The standard library functions are built-in
functions in C programming.

▪ These functions are defined in header files.

▪ The printf() and scanf() functions are defined in


the stdio.h header file.

▪ The sqrt() function is defined in the math.h header


file.
USER-DEFINED
▪ Functions that are created by the user are also
known as user-defined functions.

▪ We could create our own functions depending on


what we need in the program.
WHY USE FUNCTION?
▪ Function code is reusable.

▪ Functions make program smaller.

▪ Functions make program easier to write.

▪ Functions lead to improved commenting and


readability.

▪ Functions make program easier to maintain.


CREATING & CALLING
FUNCTIONS
FUNCTIONS
▪ Syntax:
[data_type] function_name([data_type param,…]){
data_type local_variable;

program_statement;

[return expression;]
}
FUNCTIONS
▪ The general format of a function is presented. The
first line of a function is called function
prototype declaration. It tells the compiler four
things about the function:

▪ Its data type


▪ Its name
▪ The argument it takes
▪ Who can call it
FUNCTIONS
▪ Like an operand and expression, a function also has
data type associated with it.

▪ The data type of a function is the data type of the


value it returns.

▪ If the data type is omitted, it defaults to int.

▪ Every function must have a name. Following the rules


of identifiers.
FUNCTIONS
▪ There can be only one function named main in a
program.

▪ Other functions may be named according to the rules


used in naming identifiers.

▪ Following the function’s name, enclosed in


parenthesis, is the list of arguments, along with
their data types, that are passed into the function.
FUNCTIONS
▪ Following the function prototype declaration is the
body of the function surrounded by curly braces.

▪ One of the statements it may contain is the return


statement (return is a C keyword).

▪ The expression following return is evaluated and its


value becomes the return value of the function.
#include <stdio.h>
#include <conio.h>

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 double_it(float input, int number){


for(int i = 0; i < number; ++i){
input *= 2.0;
printf(“The result = %lf\n”,input);
}
}

void main(){
clrscr();

float data = 1.5;


double_it(data,3);

getch();
}
The result = 3.000000
The result = 6.000000
The result = 12.000000
_}
PASSING ARGUMENTS
▪ As seen, the function double_it accepts two
arguments.

▪ The value passed by the function call statement is


called arguments.

▪ The arguments passed into a function are known as


the function’s parameters.
PASSING ARGUMENTS
▪ Notice that the name of the variable data in the
main function is not the same name as its
corresponding parameter double_it.

▪ This is perfectly valid, as it is the variable’s


value that is being passed, not its name.
RETURNING
VALUES
RETURNING VALUES
▪ The C language provides a convenient mechanism
whereby the results of a function may be returned to
the calling routine. The syntax of this construct
is:

return expression;
RETURNING VALUES
▪ This statement indicates that the function is to
return the value of expression back to the calling
function.

▪ The function prototype declaration must reflect the


data type of the return value.

▪ The previous program examples did not return a


value. That is why the keyword void was used.
#include <stdio.h>
#include <conio.h>

float divide(int op1, int op2){


float result;
result = (float) op1 / op2;
return result;
}

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:

▪ They are known generically as “call by value” and


“call by reference”.

▪ In C, all arguments are passed “call by value”. This


means that each argument passed to a function is
evaluated, and its value is passed into the
function.
#include <stdio.h>
#include <conio.h>

void double_it(float input, int number){


for(int i = 0; i < number; ++i)
input *= 2.0;
printf(“double_it: the result = %lf\n”,input);
}

void main(){
clrscr();

float data = 1.5;


double_it(data,3);
printf(“main: data = %lf\n”,data);

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.

▪ This means that the function’s formal parameter


refers to the same memory location as the
corresponding variable in calling function.
#include <stdio.h>
#include <conio.h>

void double_it(float *input, int number){


for(int i = 0; i < number; ++i)
*input *= 2.0;
printf(“double_it: the result = %lf\n”,*input);
}

void main(){
clrscr();

float data = 1.5;


double_it(&data,3);
printf(“main: data = %lf\n”,data);

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:

▪ Non-returning functions without parameters

▪ Non-returning functions with parameters

▪ Returning functions without parameters

▪ Returning functions with parameters


NON-RETURNING FUNCTIONS
▪ Non-returning functions without parameters.

▪ 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 function_name(data_type param1,…){


statement1;
statement2;

}
#include <stdio.h>
#include <conio.h>

void hello_world(int x){


printf(“Hello World!\n“);
printf(“This is the value of param x: %d\n“,x);
}

void main(){
clrscr();

hello_world(1);

getch();
}
Hello World!
This is the value of param x: 1
_}
#include <stdio.h>
#include <conio.h>

void hello_world(int x, double y, char z){


printf(“Hello World!\n“);
printf(“This is the value of param x: %d\n“,x);
printf(“This is the value of param y: %lf\n“,y);
printf(“This is the value of param z: %c\n“,z);
}

void main(){
clrscr();

hello_world(1, 2.50, ’A’);

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:

data_type function_name(data_type param1,…){


statement1;
statement2;
return expression;
}
#include <stdio.h>
#include <conio.h>

double computeAverage(double math, double science, double english){


double average;
average = (math + science + english) / 3;
return average;
}

void main(){
clrscr();
double mathGrade = 90, sciGrade = 85, engGrade = 98, result;

result = computeAverage(mathGrade, sciGrade, engGrade);


printf(“Average grade is %.2lf“,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.

▪ If an existing variable name is redefined in an


inner block, or a new variable name is defined in
an inner block, the variable is valid only within
that block, and is “hidden” from outer block.
FUNCTION VARIABLES
▪ Function’s local variables can only be accessed
from within the function in which they are defined;
they can not be accessed by any other function
(even if another function uses the same variable
name).

▪ Thus the scope of a local variable is: the function


in which it is defined.
#include <stdio.h>
#include <conio.h>

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.

▪ The scope of internal variable is the function or


block in which they reside.
FUNCTION VARIABLES
▪ Variables may also be declared outside of any
function block, in the same source file.

▪ They are referred to as global variables or


external variables.

▪ A global variable can be accessed by any function


and is available to use throughout the entire
program.
#include <stdio.h>
#include <conio.h>

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.

▪ Using recursion, the code stays simpler and shorter


compared to an iterative statement (looping.)
#include <stdio.h>
#include <conio.h>

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.

▪ Be careful in using recursion, like iterative


statements you could write a program that never
terminates using recursion.
#include <stdio.h>
#include <conio.h>

int sumOf(int val){


if(val > 0)
return val + sumOf(val - 1);
else
return 0;
}

void main(){
clrscr();
int num = 5;
printf(“Result: %d\n“,sumOf(num));
getch();
}
Result: 15
_
THANK
YOU!

You might also like