10 ES26 Lab - Functions
10 ES26 Lab - Functions
10
Introduction to Programming
Top-Down Design
Introduction to Programming
Functions in C
Introduction to Programming
Functions in C
Introduction to Programming
Why use function subprograms?
• Top-Down Design
– easier and simpler
– Subprograms make it easier to divide
programming tasks
• Reuse of Code
– Functions can be executed more than
once
– Easier to alter code
Introduction to Programming
Why use function subprograms?
• Procedural abstraction
– Procedures – functions in C
– A programming technique in which
we use function sub-programs to
allow us to remove from the main
function the code that provides
detailed solution to a sub-problem
Introduction to Programming
Why use function subprograms?
• Protection of Data
– Centers around the concept of
local data – data available only to the
function wherein it is declared and only
when the function is executing
Note:
When the function is done, data is gone
Introduction to Programming
Function Declaration
Introduction to Programming
Function Definition
Introduction to Programming
Function Header
Introduction to Programming
Function Body
Introduction to Programming
Function Call
Introduction to Programming
Function Call
• Void functions
– functions that don’t return a value
– Cannot be part of an expression
– Can only be used as a stand-alone
statement
• All other functions return a value
– Can be used either a part of an
expression or a stand-alone statement
Introduction to Programming
#include <stdio.h>
void prn_message(void);
int main()
{
prn_message();
return 0;
}
void prn_message(void)
{
printf("\nMessage for you: ");
printf("Have a nice day! \n");
}
Introduction to Programming
#include <stdio.h>
void prn_message(int k)
{
int i;
int main()
{
int n;
printf("Input a small positive integer: ");
scanf("%d",&n);
prn_message(n);
return 0;
}
Introduction to Programming
The Rules
Introduction to Programming
Writing a Function - Example
int print_table(double start, double end,
double step)
{
double d;
int lines = 1;
printf(“Celcius\tFarenheit\n”);
for(d =start; d <=end; d+=step, lines++)
printf(“%.1lf\t%.1lf\n”,d, d*1.8+32);
return lines;
}
Introduction to Programming
Writing a Function - Example
#include <stdio.h>
int print_table(double, double, double);
int main()
{
int how_many;
double end = 100.0;
• Call by value
– Transfers only the value
– Any changes made will not be
reflected after the function terminates
• Call by reference
– Pointer to the actual parameter
– Any changes made will remain after
the function terminates
Introduction to Programming
Scope
Introduction to Programming
Scope
2 Concepts to consider
• Block- one or more statements
enclosed in a set of curly brackets
• Global area – consists of statements
that are outside the functions
Note: an object’s scope extends from where it is
declared until the end of its block or functions.
Introduction to Programming
Scope
• The following code will NOT compile:
#include <stdio.h>
int main()
{
{
int seventy_eight = 78;
printf("seventy_eight = %d", seventy_eight);
}
Introduction to Programming
Scope Rules
• 1. An identifier may only be used within the
function in which it is declared.
– The same identifier may be reused on a
different block or scope.
– Declaring two variables with the same identifier
within the same block is prohibited
• 2. In case that when an identifier is declared more
than once within the same program, an identifier
reference always refers to the definition in the
inner most block defining the identifier.
Introduction to Programming
Variables
Introduction to Programming
Scope Rules
#include <stdio.h>
int main()
{
/* Local variables*/
int num1 = 3, num2 = 4;
Output:
if (num1 < num2){
int num2 = 1; /*num2 redeclared*/ Labas!
Outside num2 = 4;
if(num1 < num2) {
printf("Pasok!\n");
}
else printf("Labas!\n");
}
int main()
{
int num1 = 3, num2 = 4;
int num2 = 1; /*num2 redeclared*/
else printf("Labas!\n");
}
Introduction to Programming
Standard Mathematical
Functions
...
float a, b, c;
float d, r1, r2;
printf(“Enter 3 numbers: “);
scanf(“%f %f %f”, &a, &b, &c);
...
d = (b*b) – (4.0 * a * c);
if(d< 0)
printf(“Imaginary roots”);
else{
r1 = (-b + sqrt(d))/(2.0 * a);
r2 = (-b - sqrt(d))/(2.0 * a);
printf(“%f %f”,r1,r2);
}
... Introduction to Programming
Standard Mathematical
Functions
cos(x) Cosine of the angle x, expressed in
radians
sin(x) Sine of the angle x, expressed in
radians
tan(x) tangent of the angle x, expressed in
radians
abs(x) The absolute value of x, sometimes
written as |x|
log(x) Natural logarithm of x ( to the base e)
Introduction to Programming