scilab function
DESCRIPTION
CS10LTRANSCRIPT
Function- is a subprogram that acts on data and returns a value.
Function Call by Value
Function Declaration – It is also called function prototype. It tells the compiler the name, return type, and type of the parameters of the function. The declaration of the function is called its prototype.
Function Definition – tells the compiler how the function works.
Syntax: Return_Type Function_Name(Argument Lists);
1.) Function with void as return type and without argument listInput Process Output
User-Defined Function
User-Defined Function
User-Defined Function
Example: void highest();
2.) Function with void as return type and with argument listInput Process Outputmain
FunctionUser-Defined
FunctionUser-Defined
Function
Example: void highest(int num1, int num2);
3.) Function with other data type as return type and without argument listInput Process Output
User-Defined Function
User-Defined Function
main Function
Example: int highest();
4.) Function with other data type as return type and with argument listInput Process Outputmain
FunctionUser-Defined
Functionmain
Function
Example: int highest(int num1, int num2);
Sample Program: Create a C++ program that will input two numbers and output the highest number. Implement this using function.
//This program will use function declaration 1#include<iostream.h>
void highest();
int main(){ highest(); return 0;}void highest(){ int num1, num2, high;
cout<<"Input two numbers: "; cin>>num1>>num2;
if(num1>num2) high=num1;
else high=num2;
cout<<"Highest number = "<<high<<endl;}
Function Declaration
Function Call
Function Definition
//This program will use function declaration 2#include<iostream.h>
void highest(int num1, int num2);
int main(){ int num1, num2; cout<<"Input two numbers: "; cin>>num1>>num2; highest(num1, num2); return 0;}void highest(int num1, int num2){ int high;
if(num1>num2) high=num1;
else high=num2;
cout<<"Highest number = "<<high<<endl;}
Function Declaration
Function Call
Function Definition
//This program will use function declaration 3#include<iostream.h>
int highest();
int main(){ int high; high=highest(); cout<<"Highest number = "<<high<<endl; return 0;}int highest(){ int num1, num2, high; cout<<"Input two numbers: "; cin>>num1>>num2;
if(num1>num2) high=num1;
else high=num2;
return (high);}
Function Declaration
Function Call
Function Definition
//This program will use function declaration 4#include<iostream.h>
int highest(int num1, int num2);
int main(){ int high, num1, num2; cout<<"Input two numbers: "; cin>>num1>>num2; high=highest(num1, num2); cout<<"Highest number = "<<high<<endl; return 0;}int highest(int num1, int num2){ int high; if(num1>num2)
high=num1; else
high=num2; return (high);}
Function Declaration
Function Call
Function Definition
Function Call by Reference
Remember in the previous discussion that using function declaration void return type and with argument list that Input should be in main function, process and output should be in user defined function; Using Function Call by Reference Input is still in main, Process is in user defined function, but this time output is in the main function.
Note that in the following example, there is no Function declaration since the function definition is written before the main function. In such case, no need to write the function declaration.
Example Number1:
#include<iostream.h>
void byReference(int &num1,int &num2){ num1 = num1 + 1; cout<< "num1 call by reference"<<num1<<endl; num2 = num2 + 1; cout<<"num2 call by reference"<<num2<<endl;}
int main(){ int num1, num2;
num1 = 5; num2 = 6;
cout<< "num1 before function call = "<<num1<<endl; cout<<"num2 before function = "<<num2<<endl; byReference(num1, num2); cout<< "num1 after function call = "<<num1<<endl; cout<<"num2 after function call = "<<num2<<endl; return 0;}
Example Number2:
/*This program will also prompt the user to input two numbers and output he highest number. Notice that it uses function declaration 2 but the Output is in the main function.*/#include<iostream.h>
void byReference(int num1,int num2, int &high){ if(num1 > num2)
high=num1; else
high=num2;}
int main(){ int num1, num2, high=0; cout<<"Input two numbers: "; cin>>num1>>num2; byReference(num1, num2, high); cout<<"highest number = "<<high<<endl; return 0;}
/* This is a program that will prompt the user to input ten numbers and output how many odd and even number were entered. It uses function declaration with void as return type, and with three argument lists, first argument is the number entered, second argument is passed by reference, third argument is also passed by reference*/
#include<iostream.h>
void oddeven(int num,int &ctrodd, int &ctreven){ if (num % 2 == 0)
ctreven++; else
ctrodd++;}
int main(){ int num, ctr, ctreven=0, ctrodd=0;
cout<<"Input ten numbers: "<<endl; for(ctr=1;ctr<=10;ctr++) { cin>>num; oddeven(num, ctrodd, ctreven); } cout<<"ctrodd = "<<ctrodd<<endl; cout<<"ctreven = "<<ctreven<<endl; return 0;}
/* This program will input values for rows and columns to be able to produce a multiplication table. Use function declaration with void as return type and two parameters row and column which is passed by value.*/
#include<iostream.h>#include<iomanip.h>#include<ctype.h>
void multtable(int row, int column){ int ans;
for(int c=1;c<=column;c++) cout<<"\t"<<c;
cout<<endl;
for(int r=1;r<=row;r++) { cout<<r;
for(int c=1;c<=column;c++){ ans=r*c;
cout<<"\t"<<ans;}
cout<<endl; }}int main(){ int row, column; char again;
do{ do{
cout<<"Input rows and columns: "<<endl;cin>>row>>column;
if(column<1 || row<1)cout<<"Invalid input!"<<endl<<endl;
}while(column<1 || row<1);
multtable(row, column);
cout<<"Press Y/y to try again. "<<endl;cin>>again;again = tolower(again);
}while (again == 'y'); return 0;}