learning to write a function

whats wrong with this program... and can we have different variables in function prototype and function declaration????

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
using namespace std;
int main()
{
void print(char a[]) ;


char a[20];
cin>>a;

cout<<void print(char a[20]);
return 0;
}
void print(char a[20])
{
long t;
int i;



for(i=0;a[i]!='\0';i++)
{
cout<<a[i];
for(t=1;t<210;t++)
{
cout<<"";
}
}
}

and also i m not much confident about how i have declared char a[].. is that syntax right for a string???
this code is absolutely wrong kik me : wu_lebanon to help you out
Yes that code need some fixes, but probably they can be done here on the forum.

@programmer007
Take a look at this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
using namespace std;

// function prototype BEFORE the main,
// NOT INSIDE!
void print(char a[]);

int main()
{
    char a[20];
    cout << "Enter a word: ";
    cin>>a;

    cout << "This is the word: ";
    // call to the function
    print(a);
    
    return 0;
}

// definition of the function
void print(char a[20])
{
    for(int i=0; a[i]!='\0'; i++)
    {
        cout<<a[i];
    }
}
Last edited on
a few questions.......

1. When does the function need return value?
2. can we define the function anywhere in the program?
also i m not getting the typer effect which i would get if i ignore the functions and just give the for loop
For your two previous questions... you should probably read a book about programming.
For the typer effect: what do you mean? A certain delay between the moment you print a letter and the moment you print the following one? To do this you can use one of the sleep functions, but I would not do it with a "useless" for-loop.
1. When does the function need return value?

In C++, you give the type of the return value and the arguments when you define the function - when you tell the compiler what the function does. When you call the function, you give the actual arguments that you want to pass to the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Define a function called myFunc.
// It takes two arguments: an int and a string
int myFunc(int num, string str)
{
    cout << num << " " << str << endl;
    return num+1
};

int main()
{
    int i = 0;
    // call MyFunc, passing 1 and "hello" as the arguments
    i = myFunc(1, "hello");
    i = myFunc(2, "world");
    cout << i << endl;
}


So what was going on with your original code? In addition to defining and calling a function, you can also declare a function. This tells the compiler that the function exists, what it takes as arguments, and what it reutrns as a value. You do this just like a definition, but leaving out the code:
1
2
// Declare myFunc, but don't define it.
int myFunc(int num, string str);

There are several reasons to do this, mostly when the function is defined in another file. The problem is that you can put this inside your code:

1
2
3
4
5
6
7
int main()
{
    int i;
    string str;
    int someFunc(int i, string str);   // this is a declaration it does NOT call someFunc()
    myFunc(i, str);  /// this is a call to myFunc
}
thankyou all..... got it :D
Topic archived. No new replies allowed.