4. “
“Functions are "self contained" modules of
code that accomplish a specific task.
Functions usually "take in" data, process it,
and "return" a result. Once a function is
written, it can be used over and over and
over again. Functions can be "called" from
the inside of other functions.”
5. What we will do:
◎Talk about Function
◎Compare functions (in programming) with
functions (in math)
◎See some examples
◎Try to establish that definition
6. Function In Math….
A block of equation who..
◎ Takes value(s)
◎ Processes the
equation with that
value(s)
◎ Finds a value(s)
◎ Gives us that value(s)
Example: F(x)=2x+1
◎F(3)=2x+1
◎F(3)=2*3+1
◎F(3)=7
◎F(x)=7 (where x=3)
7. Function In Programming….
A block of Code who…
◎ Takes value(s)
◎ Processes the
statement(s) with
that value(s)
◎ Finds a output
◎ Returns us that
output
Example: int add(int a, int b){
int result;
result=a+b;
return result;
}
◎ add(2,3); //also can take variables
◎ result= 2+3; //in compiler this
happens
◎ result=5; //in compiler this happens
◎ return result;//returns 5
8. Math:
◎ There is a function f(x)//
◎ F(x)=2x+1
◎ F(3)=2x+1
◎ F(3)=2*3+1
◎ F(3)=7
◎ F(x)=7 (where x=3)
Lets see them side by side:
Programming:
◎ int add(int a, int b);
◎ int add(int a, int b){
int result;
result=a+b;
return result;
}
◎ add(2,3);
◎ result=2+3;
◎ result=5;
◎ Return result;
9. Math:
◎ There is a function f(x)//declaration
◎ F(x)=2x+1 //definition
◎ F(3)=2x+1//calling
◎ F(3)=2*3+1 //processing
◎ F(3)=7 //output
◎ F(x)=7 (where x=3)//return output
Lets see them side by side more deeply:
Programming:
◎ int add(int a, int b); //declaration
◎ int add(int a, int b){//definition
int result;
result=a+b;
return result;
}
◎ add(2,3); //calling
◎ result=2+3; //processing
◎ result=5; //output
◎ Return result; //return output
10. So……..
Both programming and
math function matches the
definition.
Functions:
1. accomplish a specific
task
2. take in data
3. process data
4. return a result
5. can be used over and
over again
6. can be called
11. Examples:
◎Write A function that squares the input. Like
if I give 2 it will return 4:
#include<stdio.h>
float square ( float x );
int main( )
{
float m, n ;
printf ( "Enter some number for finding square: n");
scanf ( "%f", &m ) ;
n = square ( m ) ;
printf ( “ n Square of the given number %f is %f",m,n );
}
float square ( float x ) {
float p ;
p = x * x ;
return p ;
}
12. Examples:
◎Write A function that divides a input with
another input. Like if I give 4 and 2 it will return 2:
#include <stdio.h>
int divide( int x, int y );
int main() {
int x, y;
printf( "Please input two numbers to be divided:");
scanf( "%d %d", &x , &y );
printf( "The Division of your two numbers is %dn", divide( x, y ) );
}
int divide(int x, int y)
{
return x / y;
}
13. Problems:
Try to solve the followings:
1. Write a function find the modulus of two input.
2. Write a function to solve the equation: 2x+5y-3.
3. Write a function to solve the equation: x2+2xy+y2
4. Write a function to solve the equation: x2-2xy+y2
5. Write a function to the find the distance a car will travel when
initial velocity = x ms-1, acceleration= y ms-2 and time = t second.