Jump to content

Functions help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
I am making a calculator but i dont know how to use if statements in a single function check out my code

#include<iostream.h>
#include<conio.h>

float cal(int num1,int num2 , char op)
  {
   int a,s,m;
   if(op=='+')
   a=num1+num2;
   return a; 
//   if(op=='-')
  // s=num1-num2;
 // return s;
 //  if(op=='*')
   //m=num1*num2;
  // return m;
   
}

  int main()
	{
	int a,b;
	char op;
	cout<<"\n 1st Number : ";
	cin>>a;
	cout<<"\n 2nd Number : ";
	cin>>b;
	cout<<"\n Operator which you want to perform on number : ";
	cin>>op;
	cout<<cal(a,b,op);
	getch();
  return 0;
  }

The compiler always gives unreachable code if i use more then 2 'if' statements, so help me out!!

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Try using curly braces to avoid confusion. Using a return causes the function to return.

#3
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thanks curly braces thingy worked :D

#4
telboon

telboon

    Newbie

  • Members
  • PipPip
  • 26 posts
it should be:
#include<iostream.h>

#include<conio.h>


float cal(int num1,int num2 , char op)

  {

   int a,s,m;

[COLOR="Blue"]   if(op=='+')

   {

      a=num1+num2;

      return a; 

   }[/COLOR]

   if(op=='-')

[COLOR="Blue"]   {

      s=num1-num2;

      return s;

   }[/COLOR]

   if(op=='*')

   //m=num1*num2;

  // return m;

   

}


  int main()

	{

	int a,b;

	char op;

	cout<<"\n 1st Number : ";

	cin>>a;

	cout<<"\n 2nd Number : ";

	cin>>b;

	cout<<"\n Operator which you want to perform on number : ";

	cin>>op;

	cout<<cal(a,b,op);

	getch();

  return 0;

  }
so on and so forth..

#5
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
Why are you using conio.h with iostream?
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#6
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
for getch and clrscr

Ok I have got some problem with passing pointers and arrays in functions , can anyone please write little easy tutorial with example so that I can understand easily?or any book which is good I can read from it?

#7
lucuis

lucuis

    Newbie

  • Members
  • PipPip
  • 21 posts
What concrete would you like to know? You can pass arrays along with their size and work with them inside function.

#8
CPD

CPD

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
Your cal function doesn't need all of those variables. It takes operands and an operator, then returns one result, so you only need to store the result:

double cal(double num1, double num2, char op)

{

  double result;


  if (op == '+')

    result = num1 + num2;

  else if (op == '-')

    result = num1 - num2;

  else if (op == '*')

    result = num1 * num2;

  else

    throw runtime_error("Unrecognized operator");


  return result;

}

Since op is a char you can also use a switch instead of a bunch of if statements:

double cal(double num1, double num2, char op)

{

  double result;


  switch (op) {

    case '+':

      result = num1 + num2;

      break;

    case '-':

      result = num1 - num2;

      break;

    case '*':

      result = num1 * num2;

      break;

    default:

      throw runtime_error("Unrecognized operator");

  }


  return result;

}

And you can tighten up the switch by returning directly and removing the result variable completely. :)

double cal(double num1, double num2, char op)

{

  switch (op) {

    case '+':

      return num1 + num2;

    case '-':

      return num1 - num2;

    case '*':

      return num1 * num2;

    default:

      throw runtime_error("Unrecognized operator");

  }

}

Quote

I have got some problem with passing pointers and arrays in functions , can anyone please write little easy tutorial with example so that I can understand easily?
When you pass an array to a function, it always decays into a pointer to the first element. An array of ints becomes a pointer to int and the address is &array[0]. An array of arrays of int--a 2D array--becomes a pointer to an array of int and the address is &array[0].

Because it's a pointer, you can change the elements and the array you passed will change too. You also don't know the size of the array because it's a pointer, so you need to pass the size. That's all there is to it:

#include <iostream>


using namespace std;


void function(int *oned, int n, int (*twod)[5], int x, int y)

{

  for (int i = 0; i < n; i++)

    oned[i] *= oned[i];


  for (int i = 0; i < x; i++) {

    for (int j = 0; j < y; j++)

      twod[i][j] *= 2;

  }

}


int main()

{

  int array1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

  int array2[2][5] = {

    {0, 1, 2, 3, 4},

    {5, 6, 7, 8, 9}

  };


  cout << "Before calling function:\n";


  for (int i = 0; i < 10; i++)

    cout << array1[i] << ' ';

  cout << '\n';


  for (int i = 0; i < 2; i++) {

    for (int j = 0; j < 5; j++)

      cout << array2[i][j] << ' ';

    cout << '\n';

  }


  function(array1, 10, array2, 2, 5);

  cout << "\nAfter calling function:\n";


  for (int i = 0; i < 10; i++)

    cout << array1[i] << ' ';

  cout << '\n';


  for (int i = 0; i < 2; i++) {

    for (int j = 0; j < 5; j++)

      cout << array2[i][j] << ' ';

    cout << '\n';

  }

}

If you don't like the pointer notation in function parameters, you can use array notation too. The first dimension isn't required because the size is irrelevant, it's a pointer after all:

void function(int oned[], int n, int twod[][5], int x, int y)

This is exactly the same as the example. Your compiler will turn it into this:

void function(int *oned, int n, int (*twod)[5], int x, int y)