Thread: Help with functions!!!

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    8

    Help with functions!!!

    I'm having trouble returning a value from a function and passing it to another function!! I need to return n from cmat and pass it to printmat

    Code:
    void cmat(float mat[MAX_MAT_SIZE][MAX_MAT_SIZE]
    Code:
    {
      int i,j,n;
      printf("\nEnter the order of matrix: ");
      scanf("%d",&n);
      printf("\nEnter the elements of augmented matrix:\n\n");
      for(i=1; i<=n; i++)
      {
        for(j=1; j<=(n+1); j++)
        {
          printf("A[%d][%d] : ", i,j);
          scanf("%f",&mat[i][j]);
        }
      }
    }
    
    void printmat(float mat[MAX_MAT_SIZE][MAX_MAT_SIZE])
    {
       inti,j,n;    for(i=0; i<n; i++)
      {
        for(j=0; j<(n+1); j++)
        {
          printf("%f : ", mat[i][j]);
          printf("\n");
    }
      }}
    
    }
    

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, if you want to return a value from cmat, you should declare cmat as having a return type corresponding to the type of the value that you want to return, and then have a return statement to return the value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    8
    would you please show me how to do that, I am super confused

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Here is an example program of a function foo that returns a value and a function print_foo that prints the value that foo returns:
    Code:
    #include <stdio.h>
    
    int foo(void)
    {
        return 123;
    }
    
    void print_foo(void)
    {
        printf("%d\n", foo());
    }
    
    int main(void)
    {
        print_foo();
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-27-2013, 06:43 PM
  2. WinAPI functions - similar functions as part of VS C++
    By jlewand in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2012, 08:54 AM
  3. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

Tags for this Thread